C# 输入字符串的格式不正确
任何人请帮忙 错误- 输入字符串的格式不正确 我正在使用下面的代码C# 输入字符串的格式不正确,c#,C#,任何人请帮忙 错误- 输入字符串的格式不正确 我正在使用下面的代码 private void Form2_Load(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(@"Data Source=MANISH-PC\SQLEXPRESS;Initial Catalog=test2.mdf;Integrated Security=True"); co
private void Form2_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=MANISH-PC\SQLEXPRESS;Initial Catalog=test2.mdf;Integrated Security=True");
conn.Open();
SqlCommand command = new SqlCommand("select * from table1", conn);
SqlDataAdapter adp = new SqlDataAdapter(command);
tbl = new DataTable();
adp.Fill(tbl);
comboBox1.DataSource = tbl;
comboBox1.ValueMember = tbl.Columns[0].ColumnName;
comboBox1.DisplayMember = tbl.Columns[0].ColumnName;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=MANISH-PC\SQLEXPRESS;Initial Catalog=test2.mdf;Integrated Security=True");
conn.Open();
SqlCommand command = new SqlCommand("select name from table1 where id=@id", conn);
int comboxchoice = Int32.Parse(comboBox1.SelectedValue.ToString());
command.Parameters.AddWithValue("@id",comboxchoice);
SqlDataAdapter adp = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adp.Fill(ds, "table1");
textBox1.Text = ds.Tables["table1"].Rows[0]["name"].ToString();
}
}
你不能去掉“ToString()”吗。我相信“SelectedValue”本身就是一个字符串值。我看不出您确实需要哪种方法,但您收到的错误是因为
SelectedValue
返回了DataView
(根据您的评论)
根据forDataView
,在ToString下
返回包含组件名称的字符串
作为一个开始,你可以尝试铸造
(DataView)comboBox.SelectedValue
但这应该包括额外的错误处理,以确保SelectedValue是有效的DataView
作为补充说明,您还可以通过更好的数字解析来避免这种情况
int comboChoice;
if(int.TryParse((DataView)comboBox.SelectedValue, out comboChoice))
//Do something
else
//Number invalid, do something else
因为
comboBox1.SelectedValue
未返回正确的整数值
您成功地将comboBox1.SelectedValue
更改为字符串,但该字符串无法转换为相应的整数,因为它不是整数
例如,下面的语句会触发相同的错误
//non-integer
string omg = "2313asd";
int convertedINT = Int32.Parse(omg.ToString());
从@Vignesh Kumar处获得想法,空字符串也不能识别为整数格式
//empty string
string omg = "";
int convertedINT = Int32.Parse(omg.ToString());
如果确认只有空字符串触发错误,则只使用string.IsNullOrEmpty()
如果要完全消除错误,请使用Int32.TryParse()
确认您的组合框1.SelectedValue
是正确的整数格式
或者,您可以显示字符串,并检查它是否是您想象中的整数值
MessageBox.Show(comboBox1.SelectedValue.ToString());
comboBox1.SelectedValue.ToString()的值到底是多少?调试并告诉我们。comboBox1的值是null
或者可能不是整数DataView={System.Data.DataView}@user3488317否!我假设您使用Visual Studio,在该行上放置一个断点,然后按F10
。错误很简单。它无法分析您给它的值,因为它不是一个完整的整数(数字)值。永远不要猜测用户输入的一切都会正常,在继续执行逻辑之前,请始终验证。否..SelectedValue仅为string Init32。仅解析接受字符串参数,而不是objectdebug此代码在出现一个错误后显示“not Integer value”textBox1.Text=ds.Tables[“table1”]。行[1][“name”]。ToString(); 错误-位置1处没有行。int-comboxchoice;if(Int32.TryParse(comboBox1.SelectedValue.ToString(),out comboxchoice)){MessageBox.Show(“整型值”);}else{MessageBox.Show(“非整数值…”;}表示ds.Tables[“table1”]。行[1]不存在?我的意思是它是null
还是DBNull.Value
?在转换之前,您必须确保您的内容。。。
int comboxchoice;
if (Int32.TryParse(comboBox1.SelectedValue.ToString(), out comboxchoice))
{
proceed by using comboxchoice as converted int value
}
else
{
//not integer value.....
}
MessageBox.Show(comboBox1.SelectedValue.ToString());