C# “输入字符串的格式不正确。”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18331409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Input string was not in a correct format."
提问by Ahsan Hussain
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."}
.
我正在处理一个项目,在该项目中我有一个表单,通过该表单我可以编辑列表视图中可用的问题。每当我从列表视图中选择一行并单击“修改”按钮时,列表视图上方的文本框就会加载问题及其选项。这意味着当我在列表视图中选择一行并单击“修改”按钮时,问题会自动加载到文本框中。我在那里编辑问题并单击“保存”以保存更改,但我无法访问文本框中的数据。它说{"Input string was not in a correct format."}
。
My code of the form frmFormWizard
's 'edit' button is given below:
我的表单frmFormWizard
“编辑”按钮代码如下:
frmFormWizard.cs Code:
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();
}
}
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:
每当我尝试编译它说的代码时"{"Input string was not in a correct format."}"
,此错误显示在以下行中:
QuestionID = Convert.ToInt32(lvTwoOrMoreOptions.SelectedItems[0].Text.ToString());
Please let me know what I am doing wrong.
请让我知道我做错了什么。
采纳答案by Sumeshk
It looks like some space include in the text. Use
看起来文本中包含一些空格。用
lvTwoOrMoreOptions.SelectedItems[0].Text.ToString().Trim()
lvTwoOrMoreOptions.SelectedItems[0].Text.ToString().Trim()
and convert to int32.
并转换为 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,
如果您的 ListView 处于报告模式(即它看起来像一个网格),那么您将需要 SubItems 属性。lvTwoOrMoreOptions.SelectedItems
为您提供列表视图中的每个项目 - SubItems 为您提供列。因此lvTwoOrMoreOptions.SelectedItems[0].SubItems[0]
是第一列值,
回答by Kuzgun
It looks that whatever that text is containing some characters which cannot be converted to integer like space, letters, special characters etc. Check what is coming through dropdown as below
看起来无论该文本包含一些无法转换为整数的字符,如空格、字母、特殊字符等。检查通过下拉列表显示的内容,如下所示
lvTwoOrMoreOptions.SelectedItems[0].Text.ToString();
lvTwoOrMoreOptions.SelectedItems[0].Text.ToString();
and see if that is the case.
看看是不是这样。
回答by Ragesh S
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.
希望我会有所帮助。
回答by Ehsan
whenever i try to compile the code it says "{"Input string was not in a correct format."}"
每当我尝试编译代码时,它都会说“{”输入字符串的格式不正确。”}”
This error won't come on compiling.
编译时不会出现此错误。
Now the error comese because you are trying to parse an invalid string to integer. To do it in a safe manner, you should do it like this
现在出现错误是因为您试图将无效字符串解析为整数。要以安全的方式进行操作,您应该这样做
int questionID;
if(int.TryParse(vTwoOrMoreOptions.SelectedItems[0].Text.ToString(),out questionID))
{
//success code
}
else
{
//failure code
}
回答by Manish Badhan
You might be trying to access a control inside a control, maybe a GridView or DetailsView.
您可能正在尝试访问控件内的控件,可能是 GridView 或 DetailsView。
Try using something like this:
尝试使用这样的东西:
empsalary = Convert.ToInt32(((TextBox)DetailsView1.Rows[1].Cells[1].Controls[0]).Text);