SQLDataAdapter Insert Primary Key Issue..

HI:
Can someone tell me HOW to insert a automatic number after my user inserts new data from text boxes into the dataset. I keep getting this error:

Cannot insert the value NULL into column 'id', table; column does not allow nulls. INSERT fails. (ID IS MY KEY)

I can update & delete fine, but when I try to insert, I cannot figure out how to get the primary key inserted. I'm new @ this & I think I've confused myself....
THANK YOU

Dim conn As SqlConnection = GetSQLConnection()

Try
Dim sql As String = "Select * from table"
Dim sa2 As SqlDataAdapter = New SqlDataAdapter(sql, conn)
Try
If dataset.HasChanges Then
sa2.Update(ds, "table")
dataset.AcceptChanges()
End If
Finally
sa2.Dispose()
End Try

Finally
conn.Close()
conn.Dispose()
End Try

Comments

  • : HI:
    : Can someone tell me HOW to insert a automatic number after my user
    : inserts new data from text boxes into the dataset. I keep getting
    : this error:
    :
    : Cannot insert the value NULL into column 'id', table; column does
    : not allow nulls. INSERT fails. (ID IS MY KEY)
    :
    : I can update & delete fine, but when I try to insert, I cannot
    : figure out how to get the primary key inserted. I'm new @ this & I
    : think I've confused myself....
    : THANK YOU
    :
    : Dim conn As SqlConnection = GetSQLConnection()
    :
    : Try
    : Dim sql As String = "Select * from table"
    : Dim sa2 As SqlDataAdapter = New SqlDataAdapter(sql, conn)
    : Try
    : If dataset.HasChanges Then
    : sa2.Update(ds, "table")
    : dataset.AcceptChanges()
    : End If
    : Finally
    : sa2.Dispose()
    : End Try
    :
    : Finally
    : conn.Close()
    : conn.Dispose()
    : End Try
    :
    You need to have the database create the automatic number (ID) for you. SQL will do this just like access does it. Then you only have to code the rest of the application.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion