Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
pls help me in this issue,thanks in advance
How to execute update query using string bulider in asp .net c#
C#
StringBuilder str = new StringBuilder();
str.Append("update common");
str.Append("set receiptno=" + no + "");
str.Append("where accountid=" + Accountid + "and id=" + i + "");
MySqlCommand command = new MySqlCommand(str.ToString(), con, tran);
command.ExecuteNonQuery();
Posted
Updated 3-Sep-13 2:36am
v2
Comments
[no name] 3-Sep-13 8:34am    
Just like you are doing? Do you expect us to read your mind to know what errors you are getting? Don't you believe in space characters?
[no name] 4-Sep-13 4:34am    
what is your problem....can you describe...

Not entirely sure what exactly your problem is (you might want to describe that a next time), but StringBuilder does not add spaces, so you'll need to add those yourself. However, forget about using a StringBuilder to create your query. Using this technique makes your application vulnerable to SQL injection. Always use parameters to build your query. First of all, this is much easier and more readable, but most importantly, it's a lot safer! More info:

Parameterized Query for MySQL with C#[^]
Help me for MySqlCommand parameters in C#[^]
 
Share this answer
 
Comments
Simon_Whale 3-Sep-13 9:08am    
+5 for a parameterised query suggestion
You haven't stated what your problem actually is, but from this link MySqlCommand.ExecuteNonQuery[^]

I can say that your code is correct. But also I would follow ^Mo^'s suggestion as this will help you against SQL Injection


Updated

your for loop is creating one string that contains 161,953 update statements that are not terminated and then you try to execute it. Personally I would try something like this

C#
for (int i = 1; i <= 161953; i++)
{
 str.Append("update commonparmarth");
 str.Append("set receiptnoid=" + no + "");
 str.Append("where accountid=" + Accountid + "and commonparmarthid=" + i + "");
 no++;

 MySqlCommand command = new MySqlCommand(str.ToString(), con, tran);
 command.ExecuteNonQuery();

 str.clear();
}
 
Share this answer
 
v2
Comments
pankajgarg1986 4-Sep-13 0:11am    
here i am used for loop in it and
error is you have error in sql syntax check the manual that correspondence to your mysql server version to right syntax use near
for (int i = 1; i <= 161953; i++)
{

str.Append("update commonparmarth");
str.Append("set receiptnoid=" + no + "");
str.Append("where accountid=" + Accountid + "and commonparmarthid=" + i + "");
no++;

}
MySqlCommand command = new MySqlCommand(str.ToString(), con, tran);
command.ExecuteNonQuery();
pankajgarg1986 4-Sep-13 0:30am    
friends pls help me in this issue,thanks in advance
Simon_Whale 4-Sep-13 4:28am    
I have updated my answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900