0

Im trying to insert some text that a Label has, and it wont insert it for some reason.

this is my code :

cmd = new SqlCommand(sqlquery1, conn);
cmd.Parameters.AddWithValue("Status", UserNameOrGuest.Text);
ErrorLabel.Text = "Movie rental succeeded!";

the sqlquery is : string sqlquery1 = "INSERT INTO Movies (Status) VALUES (@Status)";

Thanks for the help

2
  • 1
    how do you create and open the connection? when do you call cmd.ExecuteNonQuery ?? can you show more code in your question? Commented Dec 20, 2011 at 13:27
  • 1
    So where is the cmd.ExceuteNonQuery? What exception do you get? Commented Dec 20, 2011 at 13:27

3 Answers 3

2

You created the SqlCommand object but you are not executing the command to perform insert operation. use cmd.ExecuteNonQuery() to execute the command.

cmd = new SqlCommand(sqlquery1, conn); 
cmd.Parameters.AddWithValue("Status", UserNameOrGuest.Text); 
cmd.ExecuteNonQuery();
ErrorLabel.Text = "Movie rental succeeded!";

Check example here : SqlCommand.ExecuteNonQuery Method and SqlCommand.ExecuteScalar Method

create a SqlCommand and then executes it using ExecuteNonQuery/ExecuteScaler.

Sign up to request clarification or add additional context in comments.

Comments

2

Is this your entire code? you seem to miss a call which executes the query?

cmd.ExecuteNonQuery();

Comments

2

You forgot a statment?

cmd.ExecuteNonQuery();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.