question

Elado avatar image
0 Votes"
Elado asked AgaveJoe commented

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

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()




dotnet-csharpdotnet-aspnet-generaldotnet-aspnet-core-mvc
empty-stock.png (3.4 KiB)
· 12
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


How do you get the numbers (stock, for example) before calling AddProduct?




0 Votes 0 ·

I inserted the value .
What do mean by "before calling AddProduct"?
Edit:
Look at the image its webMethod

0 Votes 0 ·

the standard pattern and practice is passing an "Product" object rather than individual types. As recommended several times now, see my dev branch based on your code. The ASMX framework will set default type values. An int defaults to zero. A nullable type defaults to null. This standard construct allows the object to be passed through logical layers without a lot of type checking.

Below is an example if a nullable type; int?. Still, I recommend passing an object which is standard.

   public static bool AddProduct(string productName, string categoryName, double price,
              int? stock, string productDescription,
                           string productOverview, string productImage)

See the C# nullable type if you are unfamiliar.


0 Votes 0 ·
Show more comments

0 Answers