关于数据库点击增加什么都没输入就提示输入字符串的格式不正确

qq_40532477 2018-01-22 04:17:29
private void btnAdd_Click(object sender, EventArgs e)
{
if (btnAdd.Text == "新增")
{
txtName.ReadOnly = true ;
txtPrice.ReadOnly = true;
txtName.Text = "";
txtPrice.Text = "";
btnAdd.Text = "提交新增";
}
else
{
OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(adapter);
adapter.InsertCommand = commandBuilder.GetInsertCommand();
DataRow dr = ds.Tables["Food"].NewRow();
dr[0] = txtName.Text;
dr[1] = Convert.ToInt32(txtPrice.Text);
ds.Tables["Food"].Rows.Add(dr);
adapter.Update(ds, "Food");
ds.AcceptChanges();
}
}
我只点击了一下提交 就报错说 输入字符串的格式不正确。

请问要怎么处理啊 然后如果能增加了之后 datagridview会立即更新并显示新增项吗
...全文
287 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 2018-01-23
  • 打赏
  • 举报
回复
这种问题,你学一点点调试基础,就能自己解决
  • 打赏
  • 举报
回复
别“抠字眼儿”,要理解。人家.net 提示你“输入字符串格式不正确”那只是 Convert.Toxxxx 函数自己报告的“输入”参数的意思,它又不认识你的什么 TextBox,不是指你的控件。 要深入一步去理解,要从人家的系统机制去理解额,不要自顾自地抠字眼儿来理解技术。
秋的红果实 2018-01-22
  • 打赏
  • 举报
回复
引用 4 楼 qq_40532477 的回复:
我照着你这个 dr[1] =txtPrice.Text!=""? Convert.ToInt32(txtPrice.Text):DBNull.Value; 错误 1 无法确定条件表达式的类型,因为“int”和“System.DBNull”之间没有隐式转换
dr[1] =txtPrice.Text!=""? Convert.ToInt32(txtPrice.Text):(object)DBNull.Value; txtPrice保持空白是什么意思?你的设计意图 不需要更新,还是0 ? 也可以 dr[1] =txtPrice.Text!=""? Convert.ToInt32(txtPrice.Text):0;
qq_40532477 2018-01-22
  • 打赏
  • 举报
回复
引用 2 楼 xomix 的回复:
请告诉我,空字符串的int应该是几。 如果你知道答案,你就知道该怎么做。
问题是我要的效果是点了提交然后再输入 我现在只点了输入 并没有开始输入 他就开始报错了
qq_40532477 2018-01-22
  • 打赏
  • 举报
回复
我照着你这个 dr[1] =txtPrice.Text!=""? Convert.ToInt32(txtPrice.Text):DBNull.Value; 错误 1 无法确定条件表达式的类型,因为“int”和“System.DBNull”之间没有隐式转换
引用 3 楼 From_TaiWan 的回复:
dr[1] =txtPrice.Text!=""? Convert.ToInt32(txtPrice.Text):DBNull.Value;
  • 打赏
  • 举报
回复
请告诉我,空字符串的int应该是几。 如果你知道答案,你就知道该怎么做。
秋的红果实 2018-01-22
  • 打赏
  • 举报
回复
dr[1] =txtPrice.Text!=""? Convert.ToInt32(txtPrice.Text):DBNull.Value;
秋的红果实 2018-01-22
  • 打赏
  • 举报
回复
dr[1] = Convert.ToInt32(txtPrice.Text); ==> txtPrice.Text是空白,Convert.ToInt32转换自然失败

110,535

社区成员

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

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

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