[Solved] ASP.Net Error: Input string was not in a correct format

micah
 
on Nov 10, 2021 08:15 PM
832 Views

After putting the right format when inserting i still get this error message

Server Error in '/' Application.
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:


Line 313:       if (Convert.ToInt32(tqty.Text.Trim()) > 0)
Line 314:       {
Line 315:           int availableQuantity = Convert.ToInt32(GetAvailableQuantity(tqty.Text.Trim()));
Line 316:           if (Convert.ToInt32(tqty.Text.Trim()) > availableQuantity)
Line 317:           {

Code

protected void OnSave(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        if (txtrecipt.Text == "")
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('Enter Recipt ... !!')</script>", false);
        }
        else
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[] { new DataColumn("Item"), new DataColumn("Price"), new DataColumn("Quantity") });
            foreach (Control c in ph1.Controls)
            {
                if (c.GetType().Name.ToLower() == "usercontrol_ascx")
                {
                    UserControl uc = (UserControl)c;
 
                    TextBox tbItem = uc.FindControl("txtItem") as TextBox;
                    TextBox tbPrice = uc.FindControl("txtPrice") as TextBox;
                    TextBox tqty = uc.FindControl("txtQuantity") as TextBox;
                    if (!string.IsNullOrEmpty(tbItem.Text.Trim()) && !string.IsNullOrEmpty(tbPrice.Text.Trim()) && !string.IsNullOrEmpty(tqty.Text.Trim()))
                    {
                        dt.Rows.Add(tbItem.Text.Trim(), tbPrice.Text.Trim(), tqty.Text.Trim());
                        if (Convert.ToInt32(tqty.Text.Trim()) > 0)
                        {
                            int availableQuantity = Convert.ToInt32(GetAvailableQuantity(tbItem.Text.Trim()));
                            if (Convert.ToInt32(tqty.Text.Trim()) > availableQuantity)
                            {
                                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('No of Quantity entered is not available in Stock')", true);
                            }
                            else
                            {
                                int inserted = Insert(tbItem.Text.Trim(), tbPrice.Text.Trim(), tqty.Text.Trim());
                                if (inserted > 0)
                                {
                                    UpdateStock(tbItem.Text.Trim(), tqty.Text.Trim());
                                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Data Submitted Successfully ... !!')", true);
                                }
                            }
                        }
                    }
                }
            }
            // BindGridID();
            gvInsertedRecords.DataSource = dt;
            gvInsertedRecords.DataBind();
        }
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
micah
 
on Nov 20, 2021 06:45 AM

I did it this way the error stopped and its working now

Decimal availableQuantity = 0;
if (!string.IsNullOrEmpty(GetAvailableQuantity(tbItem.Text.Trim())))
{
    availableQuantity = Convert.ToDecimal(GetAvailableQuantity(tbItem.Text.Trim()));
}