Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i need to insert data from datagrid to sql server database. but my coding have a error."Incorrect syntax near 'nvarchar'. Must declare the scalar variable "@". in vb.error" This is my error...pls fix the problem....

And belove my coding showed..

Dim conn As New SqlConnection
Dim cmd As SqlCommand
Dim sqlstring As String
Dim paramdic As New Dictionary(Of String, Object)

conn.ConnectionString = "Data Source=ATHILINGAM\SQLEXPRESS;Initial Catalog=Attendance;Integrated Security=True;"

conn.Open()

paramdic.Add("@[Staff Id]", "value1")
paramdic.Add("@[Staff Name]", "value2")
sqlstring = "INSERT INTO dbo.Attend1(field1, field2) VALUES ([@Staff Id], @[Staff Name])"
cmd = New SqlCommand(sqlstring, conn)
For Each keyval As KeyValuePair(Of String, Object) In paramdic
cmd.Parameters.AddWithValue(keyval.Key, keyval.Value)
Next
cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Close()


what is the mistake on my coding....

What I have tried:

i need to insert data from datagrid to sql server database. but my coding have a error."Incorrect syntax near 'nvarchar'. Must declare the scalar variable "@". in vb.error" This is my error...pls fix the problem....
Posted
Updated 14-Apr-18 1:09am

1 solution

In SQL '@' says "this is a variable name", and variable sin SQL cannot contain spaces.
Try this:
VB
paramdic.Add("@StaffId", "value1")
paramdic.Add("@StaffName", "value2")
sqlstring = "INSERT INTO dbo.Attend1(field1, field2) VALUES (@StaffId, @StaffName)"
 
Share this answer
 
v2
Comments
Member 13738315 14-Apr-18 7:15am    
what is the different from my coding to your coding. it is same as my coding. I can't understand . pls tell me....
Member 13738315 14-Apr-18 7:16am    
do you represent space between variable names?
OriginalGriff 14-Apr-18 7:27am    
You cannot have spaces in variable names in SQL!
OriginalGriff 14-Apr-18 7:26am    
To do anything in computing you must pay attention to details!
Yours:
paramdic.Add("@[Staff Id]", "value1")
Mine:
paramdic.Add("@StaffId", "value1")
Yours:
paramdic.Add("@[Staff Name]", "value2")
Mine:
paramdic.Add("@StaffName", "value2")
Yours:
sqlstring = "INSERT INTO dbo.Attend1(field1, field2) VALUES ([@Staff Id], @[Staff Name])"
Mine:
sqlstring = "INSERT INTO dbo.Attend1(field1, field2) VALUES (@StaffId, @StaffName)"
Member 13738315 14-Apr-18 7:35am    
it is worked. But i mean "value1" is first column(StaffId) in the DataGridview data. I worked with Attendance System. so, 1)i need to select and display from the staff table in database. 2) with combobox, i marked present or obsent , staffName, StaffId and Present or obsent values stored in another table which is Attend1. First problem is solved. But second problem is sloved half and half. Because i represet 'Value1' is Staffid or first column of datgridview.pls guide me....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900