Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Input string was not in a correct format."

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.

like image 500
Ahsan Hussain Avatar asked Aug 20 '13 09:08

Ahsan Hussain


People also ask

How do you fix the input string was not in a correct format?

Input string was not in a correct format error occurs when you convert non-numeric data into an int or exceed the limit of the int data type. You should use Int. TryParse instead of Int32. Parse method to get rid of the said error.

What is an input string?

String input templates allow you to specify the type, order, and number of characters a user can enter in a data entry field. You can define string input templates only on single‑line character data entry fields.

What is string format in Java?

In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string. Syntax: There is two types of string format() method.


2 Answers

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,

like image 172
Sumeshk Avatar answered Nov 15 '22 14:11

Sumeshk


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.

like image 21
Ragesh S Avatar answered Nov 15 '22 14:11

Ragesh S