SQL - INSERT with Scope_Identity() - getting the record id

后端 未结 2 1244
北海茫月
北海茫月 2021-01-14 15:31

I have an ASP.NET page written in VB.NET that gets the items into a GridView by using a SELECT statement with INNER JOIN and also allo

2条回答
  •  青春惊慌失措
    2021-01-14 16:13

    ItemId = AddNewItemSQL.ExecuteScalar()
    
            AddNewItemSQL.ExecuteNonQuery()
    

    These two rows next to each other will execute the command twice. You should remove the second one - ExecuteNonQuery. This will have your data inserted twice in the Items - two same rows but with different IDs.

    Since you only retrieve ItemID from the first row, that one should be inserted in project_items, but the other one that was last inserted in items will have no matching row.

    Also - complete section from beginning of button click method up before Dim AddNewItemComm As String - where you open and close DataReader and do nothing with it seems completely unnecessary.

提交回复
热议问题