Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
UGIcon.Open();
                    cmd = new SqlCommand("update admin set name='" + textBox5.Text + "', age='" + numericUpDown1.Value.ToString() + "', phone='" + numericUpDown2.Value.ToString() + "', question='" + comboBox1.SelectedItem.ToString()+"', answer='" + textBox4.Text + "')", UGIcon);
                    
                       SqlDataReader sd;
                    sd = cmd.ExecuteReader(); i m getting error here 

                    MessageBox.Show("Thank You!!! Your Details updated", "Updation Window", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    groupBox1.Visible = false;
                    UGIcon.Close();
Posted
Comments
[no name] 10-Dec-12 5:38am    
What is your condition ?? I'm talink about where clause and why do you need to close the brace in query ???
selva_1990 10-Dec-12 5:42am    
i need to update entire table so i didnt use where condition

SQL
mylib.cmd = new SqlCommand("Update BUHeadMstr set Eid =@Eid, Bid=@Bid,CorpId=@CorpId,CreatedBy=@UId,CreatedOn=@Date where Buheadid =@buheadId    ", mylib.objDBConn);
            mylib.cmd.CommandType = CommandType.Text;

mylib.cmd.Parameters.AddWithValue("@Eid", objAdmin.Eid);
int i = mylib.cmd.ExecuteNonQuery();
 
Share this answer
 
Please use paramterised queries when writing inline SQL such as yours, below is an example of how to do this.

C# SqlParameter Example[^]

using this approach reduces / removes the chance of SQL Injection (decribed in the following article)

How do Parameterised queries help against SQL Injection[^]

This code project article highlights the ways to insert, update and delete data from a database using ADO.NET. As you can see when you read it. It uses the executeNonQuery as this returns the number of rows that have been effected by your query.

Simple ADO.NET Database Read, Insert, Update and Delete using C#.[^]
 
Share this answer
 
Comments
__TR__ 10-Dec-12 6:27am    
My 5!
Simon_Whale 10-Dec-12 6:28am    
Thanks __TR__
Hello,

Why u r using ExecuteReader() instead of use cmd.ExecuteNonQuery() method.
 
Share this answer
 
v2
Comments
[no name] 10-Dec-12 5:40am    
post it as comment
Member 11388228 20-Jan-15 7:04am    
how to solve this problem
i will write this coding
string strQuery = "insert into Purchase values('"+dateTimepurchasedate.Text+"','"+cBoxbrandnamepurchase.Text+"','"+cBoxproductnamepurchase.Text+"','"+txtMRPpurchase.Text+"',"+txtquntypurchase.Text+","+txtpurchaseprice.Text+","+txtvat.Text+","+txttotal.Text+","+txtdiscount.Text+",'"+result+"',"+txtnettotal.Text+",'"+cBoxsuppliername.Text+"')"

it will show error in incorrect syntax error how to solve this
why to close the ')' in front of UGIcon(connection),remove the )...
and try this

cmd = new SqlCommand("update admin set name='" + textBox5.Text + "', age='" + numericUpDown1.Value.ToString() + "', phone='" + numericUpDown2.Value.ToString() + "', question='" + comboBox1.SelectedItem.ToString()+"',
answer='" + textBox4.Text + "'", UGIcon) and Give Where Clause...
 
Share this answer
 
v2
use it,
C#
UGIcon.Open();
                    cmd = new SqlCommand("update admin set name='" + textBox5.Text + "', age='" + numericUpDown1.Value.ToString() + "', phone='" + numericUpDown2.Value.ToString() + "', question='" + comboBox1.SelectedItem.ToString()+"', answer='" + textBox4.Text + "'    ", UGIcon); // error is at line wich is highlight by underscore at that place you have use closing bracket which cause error
                    
                       SqlDataReader sd;
                    sd = cmd.ExecuteReader(); i m getting error here 
 
                    MessageBox.Show("Thank You!!! Your Details updated", "Updation Window", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    groupBox1.Visible = false;
                    UGIcon.Close();

Happy Coding!
:)
 
Share this answer
 
I think your code should look like this:
C#
UGIcon.Open();
cmd = new SqlCommand("update admin set name='" + textBox5.Text + "', age='" + numericUpDown1.Value.ToString() + "', phone='" + numericUpDown2.Value.ToString() + "', question='" + comboBox1.SelectedItem.ToString()+"', answer='" + textBox4.Text + "'", UGIcon);
                    
SqlDataReader sd;
sd = cmd.ExecuteReader(); 
 
MessageBox.Show("Thank You!!! Your Details updated", "Updation Window", MessageBoxButtons.OK, MessageBoxIcon.Information);
groupBox1.Visible = false;
UGIcon.Close();


There is an unnecessary parenthesis after textbox4.Text.
 
Share this 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