I am trying to run a single command to insert rows into my SQL Server 2012 database, however my parameters are not passed to the code.
con.Open();
string query = @"INSERT INTO [dbo].[Users](Username, Password, Role)
VALUES (@u, @p, @r);";
var cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@u", username);
cmd.Parameters.AddWithValue("@p", password);
cmd.Parameters.AddWithValue("@r", role);
cmd.ExecuteNonQuery();
con.Close();
I get SqlException with the parameterized query
'(@u nvarchar(4000),@p nvarchar(4000),@r nvarchar(1))INSERT INTO ' expects the parameter '@u', which was not supplied.
The way I see it I am passing the parameters before executing the command. Can anyone help me to find out what is missing ?