"Input string was not in a correct format."

.net, c#, visual-studio-2010, winforms

Solution

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,

Problem

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 'modify' button, the text boxes above the list view load the question and its options. This means that when I select a row in the list view and click on the 'modify' button, the question loads itself into the text boxes. I edit the question there and click on 'save' to save changes, but I am not able to access the data in the text boxes. It says `{"Input string was not in a correct format."}`. My code of the form `frmFormWizard`'s 'edit' button is given below: frmFormWizard.cs Code: ``` using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.Sql; using System.Data.SqlClient; namespace SurveyBuilder { public partial class frmFormWizard : Form { int intPanelNumber = 1; Boolean blnCancel = false; //int intFlag = 1; public frmFormWizard() { InitializeComponent(); } ... private void btnEditTwoOrMoreOptions_Click(object sender, EventArgs e) { int QuestionID; string sql; QuestionID = Convert.ToInt32(lvTwoOrMoreOptions.SelectedItems[0].Text.ToString()); { SqlConnection cn = new SqlConnection(); SqlCommand rs = new SqlCommand(); SqlDataReader sdr = null; clsConnection clsCon = new clsConnection(); clsCon.fnc_ConnectToDB(ref cn); sql = ""; sql += "SELECT * FROM SurveyQuestionLog WHERE SurveyQuestionLog.QuestionLogID = "+ QuestionID +""; //sql += "SELECT * FROM SurveyQuestionLog"; rs.Connection = cn; rs.CommandText = sql; sdr = rs.ExecuteReader(); while (sdr.Read()) { txtTwoOrMoreQuestions.Text = (string)sdr["Question"]; txtOption1.Text = (string)sdr["Choice1"]; ... } sdr.Close(); rs = null; cn.Close(); } } ``` Whenever I try to compile the code it says `"{"Input string was not in a correct format."}"` and this error is shown on the following line: ``` QuestionID = Convert.ToInt32(lvTwoOrMoreOptions.SelectedItems[0].Text.ToString()); ``` Please let me know what I am doing wrong.

Original source