Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed To Convert Parameter Value From A String To A Int32

I am currently trying to complete a transaction for a web based app, however;

Failed To Convert Parameter Value From A String To A Int32

Here is copy of the function.

public static void completeTransaction(string storeCode, string employeeId, DateTime Date, string itemListNoId)
{
    using (SqlConnection conn = new SqlConnection("Data Source = ; Initial Catalog =Business ; Integrated Security = true;"))
    {
        using (SqlCommand command = new SqlCommand("dbo.completeTransaction", conn))
        {
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@storeCode", SqlDbType.Int).Value = storeCode;
            command.Parameters.Add("@employeeId", SqlDbType.Int).Value = employeeId;
            command.Parameters.Add("@Date", SqlDbType.DateTime).Value = Date;
            command.Parameters.Add("@itemListNoId", SqlDbType.Int).Value = itemListNoId;
            conn.Open();

            command.ExecuteNonQuery();
            conn.Close();
        }
    }
}

My SQL Server table contains the following tables and types

storeCode INT
employee INT
Date DATETIME
itemListNoId INT

Any help would be appreciated.

like image 239
MasterP Avatar asked May 20 '12 18:05

MasterP


2 Answers

I belive the problem is in your first paramter (storeCode). You're trying to send a string as an int paramter.

That line should read like this:

command.Parameters.Add("@storeCode", SqlDbType.Int).Value = Convert.ToInt32(storeCode);

There's one more suspicious thing: the parameter's name is storeCode, which implies a varchar column. What's the value you're trying to pass as a storeCode? Are you sure it's an int?

like image 177
Marko Juvančič Avatar answered Oct 23 '22 02:10

Marko Juvančič


One of the inputs is a string, check the declarations for:

storeCode
employeeId
itemListNoId

I imagine storeCode is a string. You can fix this by parsing it as an Int:

command.Parameters.Add("@storeCode", SqlDbType.Int).Value = int.Parse(storeCode);

However this will cause problems if storeCode is ever no a parasable Int.

like image 31
Tom Gullen Avatar answered Oct 23 '22 02:10

Tom Gullen