“Input string was not in a correct format.”

后端 未结 5 1952

I am working on a project in which I have a form through which I can edit a question available in a list view. Whenever I select a row from the list view and click on the \'

相关标签:
5条回答
  • 2020-12-11 02:45

    It looks like some space include in the text. Use

    lvTwoOrMoreOptions.SelectedItems[0].Text.ToString().Trim()

    and convert to int32.

    hope this code will solve you

    From comments

    if your ListView is in report mode (i.e. it looks like a grid) then you will need the SubItems property. lvTwoOrMoreOptions.SelectedItems gets you each items in the list view - SubItems gets you the columns. So lvTwoOrMoreOptions.SelectedItems[0].SubItems[0] is the first column value,

    0 讨论(0)
  • 2020-12-11 02:46

    whenever i try to compile the code it says "{"Input string was not in a correct format."}"

    This error won't come on compiling.

    Now the error comese because you are trying to parse an invalid string to integer. To do it in a safe manner, you should do it like this

    int questionID;
    if(int.TryParse(vTwoOrMoreOptions.SelectedItems[0].Text.ToString(),out questionID))
    {
    //success code
    }
    else
    {
     //failure code
    }
    
    0 讨论(0)
  • 2020-12-11 02:53

    It looks that whatever that text is containing some characters which cannot be converted to integer like space, letters, special characters etc. Check what is coming through dropdown as below

    lvTwoOrMoreOptions.SelectedItems[0].Text.ToString();

    and see if that is the case.

    0 讨论(0)
  • 2020-12-11 03:02

    Please change your code like below.

      int QuestionID;
    
      bool IsIntValue = Int32.TryParse("YOUR-VARIABLE", out QuestionID);
    
      if (IsIntValue)
      {
          // YOUR CODE HERE  
      }
    

    Hope i will be help.

    0 讨论(0)
  • 2020-12-11 03:06

    You might be trying to access a control inside a control, maybe a GridView or DetailsView.

    Try using something like this:

    empsalary = Convert.ToInt32(((TextBox)DetailsView1.Rows[1].Cells[1].Controls[0]).Text);
    
    0 讨论(0)
提交回复
热议问题