This is the code I have made to update my table doctorss. The code has no errors and I have made sure that the table data types are ok; Doctor_ID data type is put to Autonumber and the others to Short Text.
Further I know this code will have impact on sql injections, and I should use parameters to do this, but as this way is possible and there's an error that I cannot solve, I need to know the solution for the error that I get when I update the table.
Error:
data type mismatch in criteria expression
I am using Access 2013 and Visual studio 2013.
private void button6_Click(object sender, EventArgs e)
{
try
{
OleDbConnection cone = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\dev\\assignt_soft\\healthline_\\healthline_\\healthline_db.accdb");
OleDbCommand come = new OleDbCommand("UPDATE doctorss set Doctor_Name='"+textBox3.Text+"', dspecial='"+comboBox4.Text+"', dday= '"+comboBox3.Text+"', dtime= '"+textBox2.Text+"' where Doctor_ID='" + textBox4.Text+"';");
come.Connection = cone;
cone.Open();
come.ExecuteNonQuery();
cone.Close();
MessageBox.Show("Doctor updated");
OleDbConnection cont = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\dev\\assignt_soft\\healthline_\\healthline_\\healthline_db.accdb");
OleDbCommand comt = new OleDbCommand("select * from doctorss", cont);
DataTable dte = new DataTable();
cont.Open();
dte.Load(comt.ExecuteReader());
dataGridView1.DataSource = dte;
cont.Close();
return;
}
catch (OleDbException expee)
{ MessageBox.Show(expee.Message); }
}
Doctor_IDis a number why do you have quotes around the value you are comparing it to? SQL parameters help you avoid this type of issue as well as sql injection. - juharrdtimefield being formatted incorrectly. - AlG