Passing an empty input for an int type makes an error - System.FormatException

daowdos 281 Reputation points
2021-08-05T11:14:11.633+00:00

Hi, I'm using WebMethod in webService,
I want to sent to the SQL an empty input parameter for an int parameter.
120787-empty-stock.png

It can be null in the SQL table,
How can change my code to fix this error ?

 public static bool AddProduct(string productName, string categoryName, double price,  
            int stock, string productDescription,  
                         string productOverview, string productImage)  
        {  
            System.Data.SqlClient.SqlConnection conn = new SqlConnection(connetionString);  
            SqlDataAdapter adapter;  
            SqlCommand comm;  
            
                conn.Open();  
                comm = new SqlCommand("InsertProduct", conn);  
                comm.CommandType = CommandType.StoredProcedure;  
                comm.Parameters.Add(new SqlParameter("productName", productName));  
                comm.Parameters.Add(new SqlParameter("CategoryName", categoryName));  
                comm.Parameters.Add(new SqlParameter("Price", price));  
                comm.Parameters.Add(new SqlParameter("Stock", stock));  
                comm.Parameters.Add(new SqlParameter("ProductDescription", productDescription));  
                comm.Parameters.Add(new SqlParameter("ProductOverview", productOverview));  
                comm.Parameters.Add(new SqlParameter("ProductImage", productImage));  
  
                adapter = new SqlDataAdapter(comm);  
                DataSet dataset = new DataSet();  
                adapter.Fill(dataset, "products");  
                return true;  

BLL.cs

   public static string AddProduct(string productName, string categoryName, double price,  
            int stock, string productDescription,  
                         string productOverview, string productImage)  
        {  
            bool addProduct = DAL.AddProduct(productName, categoryName, price,  
             stock, productDescription,  
                          productOverview, productImage);  
            return new JavaScriptSerializer().Serialize(addProduct);  
        }  

System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) System.String.System.IConvertible.ToInt32(IFormatProvider provider) System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type) --- Finish tracking an internal anomaly cartridge --- System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type) System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection) System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request) System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,400 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,417 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes