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
Community
  • 1
  • 1
Wielder
  • 156
  • 2
  • 15
  • 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? – Soner Gönül Jan 09 '15 at 09:42
  • Have you used SQL profiler? Does command really sent to server? – Andrey Korneyev Jan 09 '15 at 09:48
  • And it doesn't throw any exceptions? Are you sure you are sending different stage value than what's already in database? – Piotr Perak Jan 09 '15 at 09:48
  • The update isn't relevant. That's the table schema, not an error message. – Panagiotis Kanavos Jan 09 '15 at 09:48
  • there are no exceptions, and yes i am sending diffent value, the stage variable is 2 – Wielder Jan 09 '15 at 09:50
  • 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*. – Panagiotis Kanavos Jan 09 '15 at 09:50
  • So many unknown things so it should be something irrelevant to question. Where you checking DB changes? In your app or sql manager? Are you refreshing data after changes? – Renatas M. Jan 09 '15 at 09:51
  • Yes i am refreshing, btw, i am using built in database in c# – Wielder Jan 09 '15 at 09:52
  • Also make sure you try the query with the same values - that is, make sure you *don't* have extra spaces, newlines etc. What is the *exact* value of `@name` in both cases? Not what you *see* in the textbox but what is the value you pass as a parameter? Are the lengths the same in both cases? – Panagiotis Kanavos Jan 09 '15 at 09:52
  • don't mind the @name i try this query to see if my error is in my parameter `update tbl_userdata set stage=10` – Wielder Jan 09 '15 at 09:53
  • Only video will help. Btw you dont need stringbuilder and string.format – Renatas M. Jan 09 '15 at 09:53
  • @Reniuz, I disagree. Two unit tests with the same values should convince the poster that there's no problem with ExecuteNonQuery – Panagiotis Kanavos Jan 09 '15 at 09:55
  • 1
    Another possibility is that a transaction is opened in another part of the code but never commited – Panagiotis Kanavos Jan 09 '15 at 09:55
  • What is result of ExecuteNonQuery? It returns effected row count. If 0 - no changes where made. – Renatas M. Jan 09 '15 at 09:59
  • Do you have a try/catch block around that code? Can you show the entire method? – Lasse V. Karlsen Jan 09 '15 at 10:14
  • There is no such thing as 'built in database in C#". Where do you try to update data and how you verify it was not? – Piotr Perak Jan 09 '15 at 11:02
  • 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. – alternatefaraz Jan 09 '15 at 12:14

1 Answers1

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.