DataGrid and Automated PK

G

Guest

Hello

I configured the MS SQL SERVER 2000 to generate the Primery Key automaticly,
now, the problem is that when i insert a new row using a DataGrid that
connected to the table with the automated PK it says that i can't leave the
PK NULL...

How can i tell the DataGrid to read the value generated by the server?

I am using the WinForm datagrid not the ASP.NET
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hello Udi,

Usually, the added rows are saved to the database only when you call the
Update method of the DataAdapter. Before that, the rows are just stored in
memory by the DataTable class with the status of "Added". There's a commonly
acceptable approach to generate PK values for such rows and at the same time
not to conflict with PK values generated by the SQL server. The trick is
that you configure the corresponding DataTable column to generate PK values,
starting from -1 and with increment by -1. This way, all the added rows will
have PKs such as -1, -2, -3, -4 and so on, while the rows already in the
database will never have negative PK values. When you save the added rows to
the database, you will have to update the PK value with the actual value
generated on the server side.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top