locked
I got error in c# code can any one solve this please ? RRS feed

  • Question

  • User-1682137340 posted
    int total=0;
    int total2;
    if (lblCounter.Text == null)
    {
        lblCounter2.Text = total.ToString();
    }
    else
    {
       total2 = Int32.Parse(lblCounter2.Text);
        total2++;
        lblCounter2.Text = total2.ToString();
    }

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

    Line 26: else
    Line 27: {
    Line 28:    total2 = Int32.Parse(lblCounter2.Text);
    Line 29:     total2++;
    Line 30:     lblCounter2.Text = total2.ToString();
    Saturday, May 24, 2014 2:48 AM

Answers

  • User-1716253493 posted

    Try this

    int total=0;
    int total2;
    if (lblCounter.Text.Trim() == string.Empty)
    {
        lblCounter2.Text = total.ToString();
    }
    else
    {
       total2 = Int32.Parse(lblCounter2.Text);
        total2++;
        lblCounter2.Text = total2.ToString();
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 24, 2014 3:05 AM

All replies

  • User-1716253493 posted

    Try this

    int total=0;
    int total2;
    if (lblCounter.Text.Trim() == string.Empty)
    {
        lblCounter2.Text = total.ToString();
    }
    else
    {
       total2 = Int32.Parse(lblCounter2.Text);
        total2++;
        lblCounter2.Text = total2.ToString();
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 24, 2014 3:05 AM
  • User1508394307 posted

    The error says it all - the value of lblCounter2.Text is not int32.

    To avoid an error you can do like this

    bool ok = int.TryParse(lblCounter2.Text, out total2);
    if (!ok)
    {
        // String is not a number.
    }  

    Saturday, May 24, 2014 3:07 AM
  • User-1534620960 posted

    I think to tackle this problem in a best manner is to use JavaScript validations and also Server side validations.

    Tuesday, May 27, 2014 6:44 AM
  • User156620310 posted

    This happens because the value of lblCounter2.Text can be empty string. Try this

    int total=0;
    int total2;
    if (lblCounter.Text == null)
    {
        lblCounter2.Text = total.ToString();
    }
    else
    {
       total2 = !String.IsNullOrWhiteSpace(lblCounter2.Text) ? Int32.Parse(lblCounter2.Text) : 0;
        total2++;
        lblCounter2.Text = total2.ToString();
    }
    Tuesday, May 27, 2014 7:38 AM
  • User1508394307 posted

    lblCounter2.Text can be empty string. Try this

    ...
    if (lblCounter.Text == null)

    FYI,

    1) an empty string is not the same as null
    2) lblCounter.Text should never be null unless the control was added dynamically  

    Tuesday, May 27, 2014 9:40 AM
  • User1241621633 posted

    int total=0;
    int total2;
    if (lblCounter.Text == null)
    {
        lblCounter2.Text = total.ToString();
    }
    else
    {
       total2 = Int32.Parse(lblCounter2.Text);
        total2++;
        lblCounter2.Text = total2.ToString();
    }

    i think you should use

    total2 = Convert.ToInt32(lblCounter2.Text);

    Tuesday, May 27, 2014 11:34 AM