我正在研究一个项目,其中我有一个表单,可以通过它编辑列表视图中的问题。每当我从列表视图中选择一行并单击“修改”按钮,列表视图上方的文本框就会加载问题及其选项。 这意味着当我在列表视图中选择一行并单击'修改'按钮时,问题将自动加载到文本框中。我在那里编辑问题,然后点击'保存'保存更改,但我无法访问文本框中的数据。它说{"Input string was not in a correct format."}
。“输入字符串格式不正确。”
我的形式frmFormWizard
的‘编辑’按钮的代码下面给出:
frmFormWizard.cs代码:
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();
}
}
每当我试图编译代码,它说"{"Input string was not in a correct format."}"
这错误显示在以下行上:
QuestionID = Convert.ToInt32(lvTwoOrMoreOptions.SelectedItems[0].Text.ToString());
请让我知道我做错了什么。
不要使用字符串连接生成SQL语句中使用参数,否则你把它开到SQL注入。 – Lloyd
什么是lvTwoOrMoreOptions.SelectedItems [0] .Text.ToString()的运行时间值?另外,不应该“文本”已经是一个字符串,使.ToString()多余? – ZombieSheep
@Lloyd说的是对的:http://xkcd.com/327/ – Nolonar