Hi all,

I have a SQL table that has "RecID" as it's PK and identity column in the same time.

When I add a new record in the table directly, the RecID increments correctly. But when I try to add new record in data grid view (VB 2005 Express) and save, it gives me an error that RecID can't allow null values.

How can I make the DataGrid increments the RecID automatically?

How can I make the DataGrid increments the RecID automatically?

Since that is an identity column you shouldn't do anything as it's value should be generated (i.e. incremented) automatically. I would re-check that it's defined as: [RecID] [int] IDENTITY(1,1) NOT NULL
assuming its int-type field.

Thanks Teme64 for your reply,

The RecID definition is exactly as you said: int, identity(1,1), not null, PK.

Adding new record is doing well when adding directly in the table. The problem appears when trying to add using the datagrid. In both cases, I don't try to increment RecID myself at all.


Do you use databinding with the grid? If you do, this may solve the problem.

Yes, I use data binding, but sorry, I didn't get how this will solve the problem.

Here's the "Save Button" code:

Private Sub ItemsAdditionTempBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ItemsAdditionTempBindingNavigatorSaveItem.Click
    End Sub

Also, the same problem occurs when adding a record in the grid and move to another row (which means saving the new row without clicking Save).