Locked input string was not in correct format

  • Saturday, September 01, 2012 2:06 PM
     
      Has Code

    this is my program coding:

    public partial class New : System.Web.UI.Page
    {
        SqlConnection con;
        SqlCommand com;
        protected void Page_Load(object sender, EventArgs e)
        {
            con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["CommodityCS"].ConnectionString;
            con.Open();
            com = new SqlCommand();
            com.Connection = con;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            com.CommandType = CommandType.StoredProcedure;
            com.CommandText = "insertNewEntry";
            com.Parameters.AddWithValue("@DateOfEntry", Convert.ToDateTime(TextBox1.Text));
            com.Parameters.AddWithValue("@CollateralManager", TextBox2.Text);
            com.Parameters.AddWithValue("@BankName", TextBox3.Text);
            com.Parameters.AddWithValue("@NameOfDepositor", TextBox4.Text);
            com.Parameters.AddWithValue("@CommodityName", TextBox5.Text);
            com.Parameters.AddWithValue("@GodownName", TextBox6.Text);
            com.Parameters.AddWithValue("@GodownAddress", TextBox7.Text);
            com.Parameters.AddWithValue("@GodownType", RadioButtonList1.SelectedValue);
            com.Parameters.AddWithValue("@SRNo", TextBox8.Text);
            com.Parameters.AddWithValue("@DateOfSR", Convert.ToDateTime(TextBox9.Text));
            com.Parameters.AddWithValue("@DateOfExpiry", Convert.ToDateTime(TextBox10.Text));
            com.Parameters.AddWithValue("@RevalidationUpto", Convert.ToDateTime(TextBox11.Text));
            com.Parameters.AddWithValue("@NoOfBags", Convert.ToInt64(TextBox12.Text));
            com.Parameters.AddWithValue("@Weight", Convert.ToInt64(TextBox13.Text));
            com.Parameters.AddWithValue("@WtPerBag", Convert.ToInt64(TextBox14.Text));
            com.Parameters.AddWithValue("@RatePerBag", Convert.ToInt64(TextBox15.Text));
            com.Parameters.AddWithValue("@Value", Convert.ToInt64(TextBox16.Text));
            com.Parameters.AddWithValue("@EligibleFunding", Convert.ToInt64(TextBox17.Text));
            com.Parameters.AddWithValue("@FundingValue", Convert.ToInt64(TextBox18.Text));
            if (com.ExecuteNonQuery() > 0)
            {
                Label.Text = "Record Saved";
                TextBox1.Text = "";
                TextBox2.Text = "";
                TextBox3.Text = "";
                TextBox4.Text = "";
                TextBox5.Text = "";
                TextBox6.Text = "";
                TextBox7.Text = "";
                TextBox8.Text = "";
                TextBox9.Text = "";
                TextBox10.Text = "";
                TextBox11.Text = "";
                TextBox12.Text = "";
                TextBox13.Text = "";
                TextBox14.Text = "";
                TextBox15.Text = "";
                TextBox16.Text = "";
                TextBox17.Text = "";
                TextBox18.Text = "";
            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";
            TextBox6.Text = "";
            TextBox7.Text = "";
            TextBox8.Text = "";
            TextBox9.Text = "";
            TextBox10.Text = "";
            TextBox11.Text = "";
            TextBox12.Text = "";
            TextBox13.Text = "";
            TextBox14.Text = "";
            TextBox15.Text = "";
            TextBox16.Text = "";
            TextBox17.Text = "";
            TextBox18.Text = "";
        }
        protected void TextBox12_13_TextChanged(object sender, EventArgs e)
        {
            Int64 Weight = Convert.ToInt64(TextBox13.Text);
            Int64 NoofBags = Convert.ToInt64(TextBox12.Text);
            Int64 WeightperBag = Weight* 1000/NoofBags;
             TextBox14.Text = WeightperBag.ToString();

        }

    }

    n here is error which i get

    Server Error in '/Commodity' 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 91:     protected void TextBox12_13_TextChanged(object sender, EventArgs e)
    Line 92:     {
    Line 93:         Int64 Weight = Convert.ToInt64(TextBox13.Text);
    Line 94:         Int64 NoofBags = Convert.ToInt64(TextBox12.Text);
    Line 95:         Int64 WeightperBag = Weight* 1000/NoofBags;

    Source File: a:\Akanksha Documents\Visual Studio\Commodity\New.aspx.cs Line: 93

    Stack Trace:
    [FormatException: Input string was not in a correct format.]
       System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +9591147
       System.Number.ParseInt64(String value, NumberStyles options, NumberFormatInfo numfmt) +130
       System.Convert.ToInt64(String value) +50
       New.TextBox12_13_TextChanged(Object sender, EventArgs e) in a:\Akanksha Documents\Visual Studio\Commodity\New.aspx.cs:93
       System.Web.UI.WebControls.TextBox.OnTextChanged(EventArgs e) +118
       System.Web.UI.WebControls.TextBox.RaisePostDataChangedEvent() +143
       System.Web.UI.WebControls.TextBox.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent() +10
       System.Web.UI.Page.RaiseChangedEvents() +134
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5201
    


    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272 

    pls correct me...so that this program runs without error...

                

    Akanksha Singh

All Replies

  • Saturday, September 01, 2012 2:21 PM
     
     Answered
    Well, to run this program without error, do not input bad strings into TextBox13...
     
    You should use Integer.TryParse(TextBox13.Text, out Weight);.  The function returns true if the value was parsed, or false if it was a bad value/string.

    --
    Mike
  • Saturday, September 01, 2012 2:34 PM
     
     Answered Has Code

    You tryparse method to check if the new textBox value really is an integer:

        protected void TextBox12_13_TextChanged(object sender, EventArgs e)
        {
            Int64 Weight = 0;
            Int64 NoofBags = 0;        
            if(int.TryParse(TextBox13.Text, out Weight))
            {
                  if(int.TryParse(TextBox12.Text, out NoofBags))
                  {
                       TextBox14.Text = (Weight * 1000 / NoofBags).ToString();
                  }
                  else
                      MessageBox.Show("Noof bags are not in correct format.");
            }
            else
                MessageBox.Show("Weight is not in correct format.");
        }

    This is how you should have!

    Hope it helps,

    bye


    Mitja

  • Saturday, September 01, 2012 2:36 PM
     
     
    I think this is a web app, so a MessageBox is not a good idea.  Otherwise, your code is the correct solution.

    --
    Mike
  • Sunday, September 02, 2012 7:37 AM
     
     Answered Has Code

    >>Line 93: Int64 Weight = Convert.ToInt64(TextBox13.Text);

    Plz make sure that TextBox13.Text can be converted to numeric. Maybe u can use the RegularExpress to validate:

    Regex reg = new Regex("^[1-9][0-9]{0,}$");
    if(reg.Match(TextBox13.Text).Success)
    {
       //Continue to what u want……
    }

    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处

  • Wednesday, September 05, 2012 8:10 AM
    Moderator
     
     

    Hi Way2vps,

    You are more likely to get more efficient responses to ASP.NET issues at http://forums.asp.net where you can contact ASP.NET experts.
     


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

  • Wednesday, September 05, 2012 11:47 AM
     
     Answered
    My brothers here, probably have already solve your problem, just additionaly, i recommend you doesnt make your connection in Page_Load event, why don't you create a global.asax for your project then in Application_Start you make your connection for example? This way, you'll start your connection every time that your project runs, instead create and open the connection on each postback

    Web Developer

  • Thursday, September 06, 2012 4:51 AM
     
     

    Hi Way2vps,

    You are more likely to get more efficient responses to ASP.NET issues at http://forums.asp.net where you can contact ASP.NET experts.
     


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

    Sorry but I don't think that belongs to asp.net……If u take a close look at its question, u will find that the question is related to converting problem instead of a pure asp.net one……

    But, of course, I think the author can use CompareValidator to force the textbox's value must be type of numeric in asp.net……

    :D


    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处