在将 varchar 值 'System.Data.DataRowView' 转换成数据类型 int 时失败。

junlong3636 2011-05-28 11:27:12
private void button1_Click(object sender, EventArgs e)
{
SqlConnection sqlconn = new SqlConnection();
sqlconn.ConnectionString = "Data source=(local);Initial Catalog=StudentManger;Integrated Security=true";
sqlconn.Open();

if (this.text_Sno .Text .Trim ()== "" || this.textBox_age .Text .Trim ()== "" ||this.textBox_name.Text.Trim () == ""||this.textBox_year.Text .Trim ()=="")
{
MessageBox.Show("添加信息不完整!");
return;
}



SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from Student where Sno='" + this.text_Sno.Text.Trim () + "'";
cmd.Connection = sqlconn;
if (cmd.ExecuteScalar() != null)
MessageBox.Show("学生号重复,请重新输入!", "提示", 0);
else
{
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = sqlconn;
cmd1.CommandText = "insert into Student values('" + this.text_Sno.Text.Trim() + "','" + this.textBox_name.Text.Trim() + "','" + int.Parse(this.textBox_age.Text.Trim()) + "','" + (this.radioButtonMale.Checked ? "女" : "男").ToString() + "','" + this.comboBox_yx.SelectedValue + "','" + this.textBox_year.Text.Trim() + "')";
cmd1.ExecuteNonQuery();
MessageBox.Show("已添加成功!");
}
sqlconn.Close();

}



红色部分 '" + this.comboBox_yx.SelectedValue + "' 老是报错:在将 varchar 值 'System.Data.DataRowView' 转换成数据类型 int 时失败。

comboBox_yx 在窗体load事件中进行绑定:
this.comboBox_yx.DisplayMember = "DepartmentName";
this.comboBox_yx.ValueMember = "DepartmentId";//DepartmentId的类型为int
this.comboBox_yx.DataSource = ds.Tables[0].DefaultView;




...全文
739 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
菜鸟狂飞 2011-05-28
  • 打赏
  • 举报
回复
int.Parse(this.comboBox_yx.SelectedValue)

这样试试
菜鸟狂飞 2011-05-28
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 junlong3636 的回复:]

可是int.Parse()的参数要求是string,而this.comboBox_yx.SelectedValue 好像不是啊,试过了,语法出错引用 1 楼 xiang348352 的回复:
int.Parse(this.comboBox_yx.SelectedValue)

这样试试
[/Quote]

int.Parse(comboBox1.SelectedItem.ToString())
junlong3636 2011-05-28
  • 打赏
  • 举报
回复
不明白??[Quote=引用 6 楼 freeboy827 的回复:]
先判断是不是System.Data.DataRowView
如果是提示错误
[/Quote]
  • 打赏
  • 举报
回复
数据字段类型不匹配
junlong3636 2011-05-28
  • 打赏
  • 举报
回复
这个我也搜了,没看明白[Quote=引用 2 楼 q107770540 的回复:]
http://zhidao.baidu.com/question/182112961

单步调试 强制类型转换
[/Quote]
junlong3636 2011-05-28
  • 打赏
  • 举报
回复
可是int.Parse()的参数要求是string,而this.comboBox_yx.SelectedValue 好像不是啊,试过了,语法出错[Quote=引用 1 楼 xiang348352 的回复:]
int.Parse(this.comboBox_yx.SelectedValue)

这样试试
[/Quote]
madong871020 2011-05-28
  • 打赏
  • 举报
回复
应该强制转换类型啊
freeboy827 2011-05-28
  • 打赏
  • 举报
回复
先判断是不是System.Data.DataRowView
如果是提示错误
kingdom_0 2011-05-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 taomanman 的回复:]

'" + this.comboBox_yx.SelectedValue + "'

这个字段在数据库中是什么字段??感觉是int型的,既然是int型,两端就不应该加单引号,而且需要转型

" + int.Parse(this.comboBox_yx.SelectedValue.ToString().Trim()) + "
[/Quote]+1.
周工知道 2011-05-28
  • 打赏
  • 举报
回复
this.comboBox_yx.SelectedValue 是object的 应该ToString
暖枫无敌 2011-05-28
  • 打赏
  • 举报
回复
'" + this.comboBox_yx.SelectedValue + "'

这个字段在数据库中是什么字段??感觉是int型的,既然是int型,两端就不应该加单引号,而且需要转型

" + int.Parse(this.comboBox_yx.SelectedValue.ToString().Trim()) + "


q107770540 2011-05-28
  • 打赏
  • 举报
回复
http://zhidao.baidu.com/question/182112961

单步调试 强制类型转换

110,552

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧