Alfonsus Dhani Alfonsus Dhani - 4 days ago 4
C# Question

Data type mismatch in criteria expression excel C#

I got some problem when I run my code. It says


Data type mismatch in criteria expression


in


OleDbDataReader reader = command.ExecuteReader();


I don't know what the mistake is. Can anyone help me ?

private void JumlahLembar()
{


foreach(DataGridViewRow row in JadwalisiGV.Rows)
{
int lim30den50tot;

if (!row.IsNewRow)
{
OleDbConnection kon = new OleDbConnection(koneksi);
OleDbCommand command = kon.CreateCommand();

kon.Open();
command.CommandText = "select * from [StokLembar$] where [Limit] = '" + row.Cells["Limit"].Value + "'";
OleDbDataReader reader = command.ExecuteReader();

while (reader.Read())
{
int den50 = int.Parse(reader["Lembar Denom 50"].ToString());
int den100 = int.Parse(reader["Lembar Denom 100"].ToString());

if (row.Cells["Limit"].Value.ToString() == "30")
{
lim30++;
lim30den50tot = lim30 * lim30 * 2 * 1000000 * den50 * 50000;
TotalDen50Box.Text = lim30den50tot.ToString();

}
}
kon.Close();
}
}
}

Answer

You set Limit as text column, most probably it may be number. try below without quotes

command.CommandText = "select * from [StokLembar$] where [Limit] = " + row.Cells["Limit"].Value;

You better use parameters instead of concatenating SQL statement to avoid sql injection attacks.

Comments