Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is coding for update data in access databese based on serial number.
In database it in integer datatype. Enter the serial number through textbox. It is in string datatype. I converted into integer data type and then check the where conditon in query string. Please do some help.



C#
protected void Button11_Click(object sender, EventArgs e)
   {
     int x;
     String s;
     s = TextBox1.Text;
     x=(Convert.ToInt32(s));
    cmd = new OleDbCommand("update userdata set firstname='" + TextBox2.Text + "', lastname='" + TextBox7.Text + "', department='" + TextBox3.Text + "', software='" + TextBox4.Text + "', hardware='" + TextBox5.Text + "' where sno='" + s +"'", con);
    cmd.ExecuteNonQuery();


Error:pre>Data type mismatch in criteria expression.



Thank you,
Posted
Comments
Muralikrishna8811 11-Oct-11 6:33am    
hi check your sno datatype in database if it is int just remove ' from query

Please, don't do it that way. That is an invitation to the accidental or deliberate destruction of your database.
Use Parametrised queries instead (and your problem will disapear as well).
C#
cmd = new OleDbCommand("UPDATE userdata SET firstname=@FN, lastname=@LN, department=@DP, software=@SW, hardware=@HW WHERE sno=@SN", con);
cmd.Parameters.AddWithValue("@FN", TextBox2.Text);
cmd.Parameters.AddWithValue("@LN", TextBox7.Text);
cmd.Parameters.AddWithValue("@DP", TextBox3.Text);
cmd.Parameters.AddWithValue("@SW", TextBox4.Text);
cmd.Parameters.AddWithValue("@HW", TextBox5.Text);
cmd.Parameters.AddWithValue("@SN", x);
cmd.ExecuteNonQuery()


Oh, and BTW: don't use the default name for controls. You may remember that TextBox5 has the Hardware today, but in six weeks time? No chance. If you call it tbHardware instead, then it is obvious when you write it, and when you look back on it for maintenance.
 
Share this answer
 
Comments
Uday P.Singh 11-Oct-11 6:35am    
Agree my 5
Try this

C#
cmd = new OleDbCommand("update userdata set firstname='" + TextBox2.Text + "', lastname='" + TextBox7.Text + "', department='" + TextBox3.Text + "', software='" + TextBox4.Text + "', hardware='" + TextBox5.Text + "' where sno=" + x +"", con);


But I recommend you to use parametrized query to avoid SQL injection attacks.

hope it helps :)
 
Share this answer
 
v3
Hello Friend

your are converting TextBox1.Text value to int variable named "x", and you pass your string value "s" to your query... and also cast your sno in ' ' so its become a string instead of int,
so try this query instead of yours


C#
protected void Button11_Click(object sender, EventArgs e)
{
     int x;
     string s;
     s = TextBox1.Text;
     x=(Convert.ToInt32(s));
     cmd = new OleDbCommand("update userdata set firstname='" + TextBox2.Text + "', lastname='" + TextBox7.Text + "', department='" + TextBox3.Text + "', software='" + TextBox4.Text + "', hardware='" + TextBox5.Text + "' where sno=" + x, con);
}
 cmd.ExecuteNonQuery();
 
Share this answer
 
Comments
OriginalGriff 11-Oct-11 6:36am    
You really should not recommend that: have a google for SQL Injection Attack and you will see just how dangerous that query is!

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