Passing an empty input for an int type makes an error - System.FormatException
![](https://techprofile.blob.core.windows.net/images/-RzygNUBs0uKo6sa27lNpg.png?8DAA11)
Hi, I'm using WebMethod in webService,
I want to sent to the SQL an empty input parameter for an int parameter.
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()