Hi,
Could anybody help me please and tell me what's wrong with my code? Here it is:
OleDbCommand command = new OleDbCommand();
command.Connection = conn;
command.CommandText = "Update Kazneti set Imeprezime=?, Adresa=?, Datumstar=? where Rbr=?";
command.Parameters.AddWithValue("@imeprez", txtImePrez.Text);
command.Parameters.AddWithValue("@adresa", txtAdresa.Text);
command.Parameters.AddWithValue("@datn", txtDatumnov.Text);
command.Parameters.AddWithValue("@rbr", comboBox1.SelectedValue);
try
{
conn.Open();
OleDbDataAdapter oleDBDataAdapter1 = new OleDbDataAdapter(new OleDbCommand("Select * from Kazneti order by Imeprezime asc", conn));
_dataset.Clear();
oleDBDataAdapter1.Fill(_dataset, "Kazneti");
command.ExecuteNonQuery();//I got en error "Data type mismatch in criteria expression." here
}
Thank you.
Loading

Hemant KumarRank #1,202 · 1.5K reputationPosted Oct 19, 2011, 11:17 PM EDT
You need to give the DBType also.Please check this Example
You are close with the rest of your connection and such, but as you note, doing it with parameterized queries is safer from SQL-Injection.
// Some engines used named parameters, others may not... The "?" // are "place-holders" for the ordinal position of parameters being added... String MyQuery = "Update MyTable set SomeField = ?, AnotherField = ? " + " where YourKeyField = ?"; OleDbCommand MyUpdate = new OleDbCommand( MyQuery, YourConnection ); // Now, add the parameters in the same order as the "place-holders" are in above command OleDbParameter NewParm = new OleDbParameter( "ParmForSomeField", NewValueForSomeField ); NewParm.DbType = DbType.Int32; // (or other data type, such as DbType.String, DbType.DateTime, etc) MyUpdate.Parameters.Add( NewParm ); // Now, on to the next set of parameters... NewParm = new OleDbParameter( "ParmForAnotherField", NewValueForAnotherField ); NewParm.DbType = DbType.String; MyUpdate.Parameters.Add( NewParm ); // finally the last one... NewParm = new OleDbParameter( "ParmForYourKeyField", CurrentKeyValue ); NewParm.DbType = DbType.Int32; MyUpdate.Parameters.Add( NewParm ); // Now, you can do you MyUpdate.ExecuteNonQuery();Prabhu RajaRank #861 · 2.6K reputationPosted Oct 19, 2011, 11:31 PM EDT
Try this one.
command.CommandText = "Update Kazneti set Imeprezime=@imeprez, Adresa=@adresa, Datumstar=@datn where Rbr=@rbr";
command.Parameters.AddWithValue("@imeprez", txtImePrez.Text);
command.Parameters.AddWithValue("@adresa", txtAdresa.Text);
command.Parameters.AddWithValue("@datn", txtDatumnov.Text);
command.Parameters.AddWithValue("@rbr", comboBox1.SelectedValue); // make sure it would be String or any other datatype
before condition check in where clause of Query. if any other data type , convert it from string to that datatype.
Satyapriya NayakRank #47 · 31.5K reputationPosted Oct 19, 2011, 11:48 PM EDT
Use
command.Parameters.AddWithValue("@rbr", comboBox1.SelectedItem);
instead of
command.Parameters.AddWithValue("@rbr", comboBox1.SelectedValue);
Thanks
NelRank #2,060 · 702 reputationPosted Oct 20, 2011, 12:14 AM EDT