Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

c# - "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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...