views:

686

answers:

2
+2  Q: 

Reset primary key

Hi,

I've tried to find an answer to do this online but apparently it can't be done (I mean at the app level, not database). I have a need to clear out my dataset completely and reset the primary key at the same time. Any ideas?

Alternatively, one hack i can use is to reinitialize the dataset but that doesn't seem possible as well since the dataset is shared between different classes in the app (I'm creating the shared dataset in Program.cs).

Thanks

Farooq

Update:

ok i tried this:

MyDataSet sharedDS = new MyDataSet();

. . .

CleanDS()

{

    MyDataSet referenceDS = new MyDataSet(); 
    sharedDS.Table1.Reset(); 
    sharedDS.Merge(referenceDS);

}

My original problem is solved but now I get an System.ArgumentException for Column1 does not belong to Table1 where I can see the columns in the DataSet Viewer as well as see the populated rows. Also note that I can manually re-create the entire DataSet and I still get the same error. Any ideas?

A: 

Does the DataSet.Clear() method do what you want?

Edit

Because you say you want to reset the primary key, you must be talking about on the DataTables the DataSet holds. Have you tried calling DataSet.Tables["MyTable"].Reset(), which should reset the table to it's original state, rather than the whole DataSet?

Edit 2

If this is an autoincrementing column, have you tried resetting the DataColumn's AutoIncrementSeed property to 0?

scottm
clear only deletes all the data, if i insert a new record after that, the primary key will begin from: last record key + 1
Farooq
yes i tried the datatable.reset() method but it removes all columns and foreign key constraints.
Farooq
that would sound plausible but unfortunately that doesn't work. i think i got it to work through another way, ill post that as the answer after some testing...
Farooq
ok i tried this:MyDataSet sharedDS = new MyDataSet();...CleanDS(){ MyDataSet referenceDS = new MyDataSet(); sharedDS.Table1.Reset(); sharedDS.Merge(referenceDS);}My original problem is solved but now I get an System.ArgumentException for Column1 does not belong to Table1 where I can see the columns in the DataSet Viewer as well as see the populated rows. Also note that I can manually re-create the entire DataSet and I still get the same error. Any ideas?
Farooq
sorry, here's with formatting: MyDataSet sharedDS = new MyDataSet(); . . . CleanDS() { MyDataSet referenceDS = new MyDataSet(); sharedDS.Table1.Reset(); sharedDS.Merge(referenceDS); }
Farooq
@Farooq, how about editing your question?
scottm
+1  A: 

i tried it with the autoincrementseed and autoincrementstep and it finally works. here's for the reference of others:

sharedDS.Clear();
sharedDS.Table1.Columns[0].AutoIncrementStep = -1;
sharedDS.Table1.Columns[0].AutoIncrementSeed = -1;
sharedDS.Table1.Columns[0].AutoIncrementStep = 1;
sharedDS.Table1.Columns[0].AutoIncrementSeed = 1;

please see reasoning in this thread: http://www.eggheadcafe.com/community/aspnet/10/25407/autoincrementseed.aspx

and: http://msdn.microsoft.com/en-us/library/system.data.datacolumn.autoincrementseed(VS.85).aspx

thanks all for your help!

Farooq