I have a basic SQL table and trying to update records. I am using the following code:
I ran the following queries in SQL Server Management Studio.
I don't want the users able to update or add the ID. Just want it to increase by one with each record added and use it as an indentifier when updating records. When trying to update the database I get the error:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Must declare the scalar variable "@tid".
Code:
Private Sub updaterecord()
Dim Query As String
Dim con As SqlConnection = New SqlConnection("Data Source=PSV063U7041\SQLEXPRESS;Initial Catalog=Tooling Log;User ID=psvsqluser;Password=parker")
con.Open()
Query = "UPDATE [AT-Old Fixtures] SET [TempID] = @tid, [Tool #] = @tn, [Primary Part #] = @pn, [Part Description] = @pd, [Tool Description] = @td, [Where Used] = @where," _
& "[Date in Service] = @date, [Fixture Cost] = @cost, Vendor = @vendor, Comments = @comments WHERE [TempID] = @tid;"
Dim updatecmd As New SqlCommand(Query, con)
updatecmd.Parameters.AddWithValue("@tn", "Tool #")
updatecmd.Parameters("@tn").Value = txttoolnum.Text
updatecmd.Parameters.AddWithValue("@pn", "Primary Part #")
updatecmd.Parameters("@pn").Value = txtpartnum.Text
updatecmd.Parameters.AddWithValue("@td", "Tool Description")
updatecmd.Parameters("@td").Value = txttooldesript.Text
updatecmd.Parameters.AddWithValue("@where", "Where Used")
updatecmd.Parameters("@where").Value = txtwhereused.Text
updatecmd.Parameters.AddWithValue("@date", "Date in Service")
updatecmd.Parameters("@date").Value = txtdateservice.Text
updatecmd.Parameters.AddWithValue("@cost", "Fixture Cost")
updatecmd.Parameters("@cost").Value = txtcost.Text
updatecmd.Parameters.AddWithValue("@vendor", "Vendor")
updatecmd.Parameters("@vendor").Value = txtvendor.Text
updatecmd.Parameters.AddWithValue("@comments", "Comments")
updatecmd.Parameters("@comments").Value = txtcomments.Text
updatecmd.Parameters.AddWithValue("@pd", "Part Description")
updatecmd.Parameters("@pd").Value = txtpartdescrip.Text
updatecmd.ExecuteNonQuery()
con.Close()
refresh_data()
locktxtbox()
End Sub
Code:
ALTER TABLE [AT-Old Fixtures] ADD TempID int IDENTITY(1, 1);
Code:
ALTER TABLE [AT-Old Fixtures] ADD CONSTRAINT pk_tbl_admin PRIMARY KEY (TempId);
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Must declare the scalar variable "@tid".