I am using Visual Studio to create an asp.net web application in C# and am having issues trying to insert data from one of my pages into a table in a SQL Server database. I am getting the following error message:
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
Additional information: The variable name '@' has already been declared. Variable names must be unique within a query batch or stored procedure.
My code looks like this;
I have tried a few different things but I keep getting the same error and I've been on here and youtube for nearly an hour now and I can't find anything that relates to my issue. Any help would be greatly appreciated.
Thanks!
Edit: I should add that I got this code from youtube and in the video, it worked fine. The only real difference is that my version is within an 'if' statement. I commented the if statement out and received the same error message.
You code is wrong. First, your sql query is incomplete; you need to include the actual variable names as part of your INSERT script:
INSERT INTO parent(parentID) VALUES (@parentID)
Then in your C# code, you need to include the exact name of the parameter when adding it to the collection:
xp.Parameters.AddWithValue("@parentID", userBox.Text);
This is obviously for one parameter, you'll need to repeat the same pattern for the rest. Read more here.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments