and i got an error message(Incorrect syntax near 'nvarchar'. Must declare the scalar variable "@")
i tried to update information but i got the same error, it seems the problem is with " cmd.Parameters.AddWithValue", because before adding this i had no problem.
Ahmad Azizov
Member
117 Points
98 Posts
Incorrect syntax near 'nvarchar'. Must declare the scalar variable "@"
Feb 29, 2012 11:00 PM|LINK
Hi all, i am using sql server to add data to my webpage, i was trying to add information by writing these codes
string connectionstring = "Data Source=localhost;" + "Initial Catalog=Registration;Integrated Security=SSPI";
string sqlstring = "INSERT INTO Reg(";
sqlstring += "[First Name], [Last Name], ID, [Email Address], ZIP, [Tel Number]) ";
sqlstring += "VALUES(@[First Name], @[Last Name], @ID, @ZIP, @[Tel Number]) ";
SqlConnection con = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand(sqlstring, con);
cmd.Parameters.AddWithValue("@[First Name]", Firstname.Text);
cmd.Parameters.AddWithValue("@[Last Name]", lastname.Text);
cmd.Parameters.AddWithValue("@ID", ID.Text);
cmd.Parameters.AddWithValue("@[Email Address]", eaddress.Text);
cmd.Parameters.AddWithValue("@ZIP", Zip.Text);
cmd.Parameters.AddWithValue("@[Tel Number]", telephone.Text);
int added = 0;
try
{
con.Open();
added = cmd.ExecuteNonQuery();
Label7.Text = added.ToString() + " " + "record(s) added";
}
catch (Exception err)
{
Label1.Text = "An error ocurred: ";
Label1.Text += err.Message;
}
finally
{
con.Close();
}
if (added > 0)
{
fillreglist();
}
and i got an error message(Incorrect syntax near 'nvarchar'. Must declare the scalar variable "@")
i tried to update information but i got the same error, it seems the problem is with " cmd.Parameters.AddWithValue", because before adding this i had no problem.
thanks for answering...
Ahmad Azizov
Member
117 Points
98 Posts
Re: Incorrect syntax near 'nvarchar'. Must declare the scalar variable "@"
Mar 01, 2012 01:10 AM|LINK
OK, i got it,i can not use @[Email Address], i should use EmailAddress instead
gavilanch
Member
173 Points
66 Posts
Re: Incorrect syntax near 'nvarchar'. Must declare the scalar variable "@"
Mar 01, 2012 01:13 AM|LINK
Hi,
If you look closely, you can see that you are adding a value to the @[Email Adress] variable, but you didn't declare it.
Good luck
Ahmad Azizov
Member
117 Points
98 Posts
Re: Incorrect syntax near 'nvarchar'. Must declare the scalar variable "@"
Mar 01, 2012 02:42 AM|LINK
yea thanks