Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Hello, I have an MS ACCESS table with two columns named item_nm(text) and item_id(This column is autonumber). Whenever I"m trying to update table it's getting an error("Data type mismatch in criteria expression"). Please solve this quest. Thanks!!!!
 
private void button2_Click(object sender, EventArgs e)
        {
                cn.Open();
                OleDbCommand cm = new OleDbCommand("update tbl_purchaseitementry set item_nm='@item_nm' where item_id='@item_id'", cn);
                cm.Parameters.AddWithValue("@item_nm",txt_itemname.Text);
                cm.Parameters.AddWithValue("@item_id",txt_itemid.Text);
                OleDbDataAdapter da = new OleDbDataAdapter(cm);
                cm.ExecuteNonQuery();
                cn.Close();
        }
Posted 15-Feb-13 3:27am
Edited 15-Feb-13 3:35am
phil.o35.6K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Take off the quotes:
OleDbCommand cm = new OleDbCommand("update tbl_purchaseitementry set item_nm='@item_nm' where item_id='@item_id'", cn);
Becomes:
OleDbCommand cm = new OleDbCommand("update tbl_purchaseitementry set item_nm=@item_nm where item_id=@item_id", cn);
  Permalink  
Comments
phil.o at 15-Feb-13 9:38am
   
Damned, you were faster :)
My 5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

OleDbCommand cm = new OleDbCommand("update tbl_purchaseitementry set item_nm=@item_nm where item_id=@item_id", cn);
 
Don't enclose your variables (@item_nm and @item_id) between single quotes ; you don't need to.
  Permalink  

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

  Print Answers RSS
0 Richard MacCutchan 335
1 OriginalGriff 330
2 Sergey Alexandrovich Kryukov 259
3 Sharmanuj 146
4 Magic Wonder 129
0 Sergey Alexandrovich Kryukov 5,969
1 OriginalGriff 5,005
2 CPallini 2,473
3 Richard MacCutchan 1,547
4 Abhinav S 1,507


Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2014
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