SQLDataAdapter Insert Primary Key Issue..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • niccolem
    New Member
    • Sep 2007
    • 2

    #1

    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 = GetSQLConnectio n()

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

    Finally
    conn.Close()
    conn.Dispose()
    End Try
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Why not let sql server do this for you? Then you dont have to insert and avoids possibility of replication.

    btw - recommend never using id as a field, even if it doesnt give you an error poor practice.

    Comment

    Working...