简体   繁体   中英

Specified cast is not valid. SQL Parameter

I keep getting back specified cast is not valid on the int result = myDataReader.GetInt32(0); line when running the query using the parameter. The thing is if I replace @Reg with text 'WM07 OGR' it works fine. However the string reg returns this so why the error?

string reg = RadComboBox1.SelectedValue;

//prepare sql statements
Command = new OleDbCommand(@"SELECT MAX(Fuel.NO_ODOMETER_LAST) AS PrevMiles 
        FROM (Fuel INNER JOIN Vehicle ON Fuel.TX_VIN = Vehicle.TX_VIN)
        WHERE (Vehicle.TX_VEHNUMBER = '@Reg')", conn);
Command.Parameters.AddWithValue("@Reg", OleDbType.WChar);
Command.Parameters["@Reg"].Value = reg;

myDataReader = Command.ExecuteReader();

if (myDataReader.Read())
{
    int result = myDataReader.GetInt32(0);
    Prev_Mileage.Text = result.ToString();
}

//cleanup objects
myDataReader.Close();
conn.Close();

Try some thing like this.

Command = new OleDbCommand(@"SELECT MAX(Fuel.NO_ODOMETER_LAST) AS PrevMiles 
            FROM (Fuel INNER JOIN Vehicle ON Fuel.TX_VIN = Vehicle.TX_VIN)
            WHERE (Vehicle.TX_VEHNUMBER = @Reg)", conn);

Or

Command = new OleDbCommand(@"SELECT MAX(Fuel.NO_ODOMETER_LAST) AS PrevMiles 
            FROM (Fuel INNER JOIN Vehicle ON Fuel.TX_VIN = Vehicle.TX_VIN)
            WHERE (Vehicle.TX_VEHNUMBER = '?')", conn);

Or

Command = new OleDbCommand(@"SELECT MAX(Fuel.NO_ODOMETER_LAST) AS PrevMiles 
            FROM (Fuel INNER JOIN Vehicle ON Fuel.TX_VIN = Vehicle.TX_VIN)
            WHERE (Vehicle.TX_VEHNUMBER = ?)", conn);

For more reference see following links.

http://www.java2s.com/Code/CSharp/Database-ADO.net/PassparametertoOleDbCommand.htm

http://blogs.msdn.com/b/wriju/archive/2008/01/24/ado-net-oledbcommand-parameterized-query-sequence-rule.aspx

The thing is if I replace @Reg with text 'WM07 OGR' it works fine. However the string reg returns this so why the error?

It seems that you get the error if the query returns null because there is no matching TX_VEHNUMBER , then the cast to int fails.

So you have to check if it's null:

int result = 0; // insert default
if(!myDataReader.IsDbNull(0))
    result = myDataReader.GetInt32(0)

Apart from that it doesn't work because your parameter is interpreted as value, you have wrapped it in apostrophes here:

WHERE (Vehicle.TX_VEHNUMBER = '@Reg')

You just have to do this:

WHERE (Vehicle.TX_VEHNUMBER = @Reg)

Since it's getting into the below block...

if (myDataReader.Read())
        {
            int result = myDataReader.GetInt32(0);
            Prev_Mileage.Text = result.ToString();
        }

...I assume you have a record. I'd check the DataType of NO_ODOMETER_LAST , as it might be a varchar or something other than an int . If thats a case, you might need to use a TryParse .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM