Wielder
Wielder

Reputation: 156

ExecuteNonQuery not working but the ExecuteReader is working

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

Upvotes: 1

Views: 788

Answers (1)

user4583471
user4583471

Reputation:

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.

Upvotes: 1

Related Questions