Load Empty Database table

Posted by john White on Stack Overflow See other posts from Stack Overflow or by john White
Published on 2012-06-04T03:26:12Z Indexed on 2012/06/04 4:40 UTC
Read the original article Hit count: 150

I am using SQLexpress and VS2008. I have a DB with a table named "A", which has an IdentitySpecification column named ID. The ID is auto-incremented. Even if the row is deleted, the ID still increases. After several data manipulation, the current ID has reached 15, for example.

When I run the application

  • if there's at least 1 row: if I add a new row, the new ID is 16. Everything is fine.

  • If the table is empty (no row): if I add a new row, the new ID is 0, which is an error (I think).

And further data manipulation (eg. delete or update) will result in an unhandled exception.

Has anyone encountered this?

PS. In my table definition, the ID has been selected as follow: Identity Increment = 1; Identity Seed =1;

The DB load code is:

dataSet = gcnew DataSet();

dataAdapter->Fill(dataSet,"A");

dataTable=dataSet->Tables["A"];

dbConnection->Open();

The Update button method

dataAdapter->Update(dataSet,"tblInFlow");

dataSet->AcceptChanges();

dataTable=dataSet->Tables["tblInFlow"];

dataGrid->DataSource=dataTable;

If I press Update:

  • if there's at least a row: the datagrid view updates and shows the table correctly.

  • if there's nothing in the table (no data row), the Add method will add a new row, but from ID 0. If I close the program and restart it again: the ID would be 16, which is correct.

This is the add method

row=dataTable->NewRow();

row["column1"]="something";

dataTable->Rows->Add(row);

dataAdapter->Update(dataSet,"A");

dataSet->AcceptChanges();

dataTable=dataSet->Tables["A"];

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about visual-studio