Priyanka
Priyanka

Reputation: 75

DATA TYPE MISMATCH error IN CRITERIA EXPRESSION (ms Access)

please figure out the error in my code.it show

DATA TYPE MISMATCH error IN CRITERIA EXPRESSION .

OleDbCommand cmd = new OleDbCommand("DELETE tbbill.*, tbgrid.* FROM tbbill INNER JOIN tbgrid ON tbbill.invoice = tbgrid.ginovice WHERE tbbill.invoice ='" + Convert.ToInt32(txtinvoice.Text) + "'", con);
cmd.ExecuteNonQuery();
cmd.Dispose();

Upvotes: 1

Views: 187

Answers (2)

Shweta Pathak
Shweta Pathak

Reputation: 783

It has to be

OleDbCommand cmd = new OleDbCommand(
                "DELETE tbbill.*, tbgrid.* 
                 FROM tbbill 
                 INNER JOIN tbgrid 
                    ON tbbill.invoice = tbgrid.ginovice 
                 WHERE tbbill.invoice = " + Convert.ToInt32(txtinvoice.Text) , con);

I have removed '' from the invoice

Whereas you should always use a parameterized SQL to prevent SQL Injections

OleDbCommand cmd = new OleDbCommand(
                "DELETE tbbill.*, tbgrid.* 
                 FROM tbbill 
                 INNER JOIN tbgrid 
                    ON tbbill.invoice = tbgrid.ginovice 
                 WHERE tbbill.invoice = @invoice", con);

cmd.Parameters.Add("@invoice", Convert.ToInt32(txtinvoice.Text) );  
cmd.ExecuteNonQuery();

Upvotes: 1

Soner Gönül
Soner Gönül

Reputation: 98740

Single quotes are for characters, if your invoice is numeric typed, you need to delete these quotes like;

tbbill.invoice = " + Convert.ToInt32(txtinvoice.Text) + ...

But don't use this way.

Always use parameterized queries. This kind of string concatenations are open for SQL Injection attacks.

And use using statement to dispose your command and connections automatically instead of calling Dispose method manually.

using(OleDbConnection con = new OleDbConnection(conString))
using(OleDbCommand cmd = con.CreateCommand())
{
    cmd.CommandText = "DELETE tbbill.*, tbgrid.* FROM tbbill INNER JOIN tbgrid ON tbbill.invoice = tbgrid.ginovice WHERE tbbill.invoice = @invoice";
    cmd.Parameters.Add("@invoice", OleDbType.Integer).Value = Convert.ToInt32(txtinvoice.Text);
    // I used OleDbType.Integer in my example. You should use proper OleDbType for your column.
    con.Open();
    cmd.ExecuteNonQuery();
}

Upvotes: 0

Related Questions