I am making a web form which deletes the selected id from the database. But whenever I run my command I get the following error
NullReferenceRxception was unhandled by the user.
And I'm getting this where I'm using addwithvalues.
This is my code:
con.Open();
string query = "delete from [User] where id ='" + TextBox1.Text + "'";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@id", TextBox1.Text); //this is giving an error
int UserExist = (int)cmd.ExecuteScalar();
if (UserExist > 0)
{
cmd.ExecuteNonQuery();
con.Close();
}
else
{
Label1.Text = "No such id found ";
}
How can I fix this error?