Click here to Skip to main content
16,015,583 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
try
           {
               con = new OleDbConnection(s);
               con.Open();

               sq = "Update BillTable Set Quantity ="+ textBox5.Text + " where Id = " + textBox2.Text;

               cmd = new OleDbCommand(sq,con);
               cmd.ExecuteNonQuery();
               MessageBox.Show("row updated...");

               con.Close();
           }//end f try


           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }
Posted
Comments
Richard MacCutchan 26-Oct-14 4:31am    
What errors are you seeing? You should also use proper parameterized queries in your SQL, your code is open to SQL injection attack.

1 solution

Do not use string concatenation in query strings, as its highly unsafe.

Your issue in your case is the missing " symbol and the ; symbol for closing the statement as well as the query at then end of your query string.

C#
string sq = ("Update BillTable Set Quantity = " + textBox5.Text + " where Id = " + textBox2.Text + ";");

In MySQL, SQL, you can use Named parameters like @Yourname in your query. But in OleDB you need to specify your parameters in the order of your query and use ? instead of @. See below:
C#
//Opp being your table. ? is your target value parameters which acts as a placeholder. Conn is your connection.

    String dStr = "Two";
    String eStr = "Three";
    OleDbCommand cmdcom = new OleDbCommand("Insert Into Opp [Year] VALUES (?eNum);", Conn);
    cmdcom.Parameters.AddWithValue("?eNum", OledbType.VarChar).Value = dStr);

Parameters in OleDb queries are only assigned by position and the names are disregarded entirely. You can use named queries if added in an orderly way. Meaning; if your query has multiple placeholders ?, they will be called in the order they are wrote. Example:
C#
cmdcom.Parameters.AddWithValue("?dNum", OledbType.VarChar).Value = dStr);
cmdcom.Parameters.AddWithValue("?eNum", OledbType.VarChar).Value = eStr);
//dNum, then eNum etc...

Also, some words are Reserved Words In OleDB and are required to be wrapped in [] brackets. If Year were a reserved word (which it is), you would also wrap that in square brackets.
 
Share this answer
 
v2

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