Click here to Skip to main content
11,479,486 members (46,663 online)
Rate this: bad
good
Please Sign up or sign in to vote.
hi!!

i am getting exception for update statement..

Sometimes i am getting Syntax error or Datatype mismatch error...


the code updates values


string new_val = new_value_txt.Text.ToString();
 
int rowindex = dataGridView3.CurrentCell.RowIndex;
int row = dataGridView3.CurrentCell.RowIndex;
 
string row_index_value = dataGridView3["ID", row].Value.ToString();
 

if (String.IsNullOrEmpty(new_value_txt.Text.Trim()) && String.IsNullOrEmpty(new_value_txt.Text.Trim()))
{
 
    MessageBox.Show("Please Enter a New Value..", "Enter a Value", MessageBoxButtons.OKCancel);
 
}
 

OleDbCommand cmd = new OleDbCommand("update Purchases SET " + name_of_column + " ='" + new_val + "' WHERE " + name_of_column + " = '" + cellvalue + "' AND  ID  = '" + row_index_value + "'   ", con);
 
con.Open();
cmd.ExecuteNonQuery();
con.Close();
 
MessageBox.Show("updated Successfully!!");

}

can someone please help me with this error...

Thanks in Advance.. Smile | :)
Posted 4-Mar-13 3:04am
Comments
CHill60 at 4-Mar-13 8:23am
   
A lot will depend on the actual content of your variables - are all of your database columns string or dates for example. Using parameterised queries will help you overcome this problem - see http://msdn.microsoft.com/en-GB/library/system.data.oledb.oledbcommand.parameters.aspx and also help protect you from sql injection
ryanb31 at 4-Mar-13 14:27pm
   
Besides the likelyhood of SQL injections, this should be really easy to figure out. Just put a breakpoint and see what the actual SQL statement is.

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

  Print Answers RSS
0 OriginalGriff 255
1 Sergey Alexandrovich Kryukov 215
2 F-ES Sitecore 175
3 Richard MacCutchan 120
4 Frankie-C 104
0 Sergey Alexandrovich Kryukov 7,890
1 OriginalGriff 7,441
2 Sascha Lefèvre 3,064
3 Maciej Los 2,491
4 Richard Deeming 2,335


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 4 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100