11

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.

4

5 回答 5

5

看起来文本中包含一些空格。利用

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

并转换为 int32。

希望这段代码能解决你

来自评论

如果您的 ListView 处于报告模式(即它看起来像一个网格),那么您将需要 SubItems 属性。lvTwoOrMoreOptions.SelectedItems获取列表视图中的每个项目 - SubItems 获取列。lvTwoOrMoreOptions.SelectedItems[0].SubItems[0]第一列的值也是如此,

于 2013-08-20T09:32:46.383 回答
1

请更改您的代码,如下所示。

  int QuestionID;

  bool IsIntValue = Int32.TryParse("YOUR-VARIABLE", out QuestionID);

  if (IsIntValue)
  {
      // YOUR CODE HERE  
  }

希望我会有所帮助。

于 2013-08-20T09:32:13.783 回答
1

每当我尝试编译代码时,它都会显示“{”输入字符串的格式不正确。“}”

编译时不会出现此错误。

现在出现错误是因为您试图将无效字符串解析为整数。为了以安全的方式执行此操作,您应该这样做

int questionID;
if(int.TryParse(vTwoOrMoreOptions.SelectedItems[0].Text.ToString(),out questionID))
{
//success code
}
else
{
 //failure code
}
于 2013-08-20T09:33:37.683 回答
0

看起来无论该文本是否包含一些无法转换为整数的字符,例如空格、字母、特殊字符等。检查下拉列表中的内容,如下所示

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

看看是不是这样。

于 2013-08-20T09:25:54.477 回答
0

您可能正在尝试访问控件内的控件,可能是 GridView 或 DetailsView。

尝试使用这样的东西:

empsalary = Convert.ToInt32(((TextBox)DetailsView1.Rows[1].Cells[1].Controls[0]).Text);
于 2015-05-09T04:33:30.673 回答