1

First don't mark it as duplicate, because this

ExecutenonQuery not working

ExecutenonQuery not working

ExecuteNonQuery not working in C#

is not the solution i am looking, the syntax is correct, the connection also correct because the SqlDataReader is working, and i open the connection and called it in SqlCommand. Here is my code

            if (sql_con.State == ConnectionState.Closed)
            {
                sql_con.Open();
            }
            StringBuilder query = new StringBuilder();

            query.Append(String.Format("update tbl_userdata set stage=@stage where username=@name"));

            SqlCommand sql_command2 = new SqlCommand(query.ToString(), sql_con);
            sql_command2.Parameters.AddWithValue("@stage", stage);
            sql_command2.Parameters.AddWithValue("@name", lblName.Text.ToLower());
            sql_command2.ExecuteNonQuery();

And after debugging my query is update tbl_userdata set stage=@stage where username=@name

i don't know what's wrong here, i remove the where clause to see if thats the cause of the problem, but still error. I don't know what's wrong here

here is my connection

SqlConnection sql_con = new SqlConnection(Properties.Settings.Default.dbCon);

Sorry i forgot to put my error, the error is the stage is not updating, this is my table

tbl_userdata
+--------+-------+-------+
|Username|stage  | coins |
+--------+-------+-------+


Username = nvarchar(5), stage = smallint, coins = smallmoney
17
  • What is the error exactly? What are your column types and what is your parameter values? Is your update query works on your sql server? Did you debug your code? What is your query looks like when you add parameter values to your query? Commented Jan 9, 2015 at 9:42
  • Have you used SQL profiler? Does command really sent to server? Commented Jan 9, 2015 at 9:48
  • 1
    Instead of assuming some strange behaviour for ExecuteNonQuery (there isn't any) check your values and possible exceptions. The only difference between the two functions is that one returns the first available result value while the second does nothing after execution has finished. Commented Jan 9, 2015 at 9:50
  • 1
    Another possibility is that a transaction is opened in another part of the code but never commited Commented Jan 9, 2015 at 9:55
  • 1
    Try to run the sanme query in sql server to with the parameter you are giving to verify the update query. Secondly try to get the sp call from sql profiler to verify the query. I don't think execute non query is a problem. Commented Jan 9, 2015 at 12:14

1 Answer 1

1

You dont need to do as string builder and append in query just use string if values are good with where clause, my answer can be stupid but it is worth of try if you don't find any solution.

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

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.