Failed to convert parameter value from string to Int32

I am currently trying to complete a transaction for a web application;

Failed to convert parameter value from string to Int32

Here is a 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.

+4
source share
3 answers

I believe the problem is in your first parameter (storeCode). You are trying to send a string as an int parameter.

This line should look like this:

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

There is another suspicious thing: the parameter name is storeCode, which implies the varchar column. What value are you trying to pass as storeCode? Are you sure this is int?

+2
source

I would suggest you change the type of parameters in a method.

to

 public static void completeTransaction(int storeCode, int employeeId, DateTime Date, string itemListNoId) 

and convert the strings before passing the values ​​to the method.

+1
source

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

 storeCode employeeId itemListNoId 

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

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

However, this will cause problems if storeCode never a parasitic Int .

0
source

Source: https://habr.com/ru/post/1413416/


All Articles