转换时,我收到此错误“将varchar转换为数据类型int时转换失败." [英] whille casting i get this error "Conversion failed when converting the varchar to data type int."
本文介绍了转换时,我收到此错误“将varchar转换为数据类型int时转换失败."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void comboBoxCliNo_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
SqlCommand cmd = new SqlCommand("select * from client where clientId=''" + comboBoxCliNo.Text + "''" , con);
con.Close();
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.Read())
{
lblFirstName.Text = rdr[0].ToString();
lblAddress.Text = rdr[1].ToString();
lblPhone.Text = rdr[2].ToString();
}
else
{
lblFirstName.Text = "";
lblAddress.Text = "";
lblPhone.Text = "";
}
con.Close();
}
catch (Exception df)
{
MessageBox.Show(df.Message);
}
}
推荐答案
更改此行
SqlCommand cmd = new SqlCommand("select * from client where clientId=" + ConvertTo.Int32(comboBoxCliNo.Text) + "" , con);
您正在尝试将字符串与int进行比较.
You were trying to compare string with int.
SqlCommand cmd = new SqlCommand("select * from client where clientId=" + ConvertTo.Int32(comboBoxCliNo.Text) , con);
原因是您的clientId栏是整数.您是否正在尝试传递字符串.您也无需添加单引号.
The reason would be ur clientId column is integer. Nad you are trying to pass a string. you no need to include single quotes also.
这篇关于转换时,我收到此错误“将varchar转换为数据类型int时转换失败."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文