"输入字符串的不正确的格式&QUOT。 [英] "Input string was not in a correct format."

查看:237
本文介绍了"输入字符串的不正确的格式&QUOT。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个项目中,我有过,我可以编辑列表视图中提供一个问题的形式。每当我从列表视图中选择一列,然后点击修改按钮,文本框的列表视图上面加载的问题及其选项。 这意味着,当我在列表视图中选择一列,然后点击修改按钮,这个问题本身加载到文本框。我编辑的问题出现,并点击保存保存更改,但我不能够访问的数据在文本框中。它说 {输入字符串的不正确的格式。}

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."}.

形式我的code被 frmFormWizard 的编辑按钮,如下:

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();
            }
        }

每当我试图编译code,它说{输入字符串的不正确的格式。}键,此错误显示在下面一行:

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());

请让我知道我做错了。

推荐答案

它看起来像一些空间,包括在文本中。 使用

It looks like some space include in the text. Use

lvTwoOrMoreOptions.SelectedItems [0] .Text.ToString()。修剪()

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

和转换为INT32。

希望这code将解决你

hope this code will solve you

这是注释

如果你的ListView是在报告模式(也就是它看起来像一个网格),那么你需要的子项属性。 lvTwoOrMoreOptions.SelectedItems 让你的每个项目在列表视图 - 子项可以让你的列。因此, lvTwoOrMoreOptions.SelectedItems [0] .SubItems [0] 是第一列的值,

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,

这篇关于"输入字符串的不正确的格式&QUOT。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆