(回帖给分)在将 varchar 值 转换成数据类型 int 时失败。 在将 varchar 值 'System.Web.UI.HtmlControls.HtmlInputText' 转换成数据类型 int 时失败。我用datalist写了一个添加的代码数据表里有字段 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "','" + ddlType.Text + "','" + txtWrite + "','" + txtChuban.Value + "','" + txtBookPrice + "'," +txtBookCount.Value + ")";红的部分对应你数据库字段都是整型,也要进行类型转换改成这样试试(先判断这些值ddlType.Text不为空)string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "','" + Convert.ToInt32(ddlType.SelectedValue.Trim()) + "','" + txtWrite + "','" + txtChuban.Value + "','" + Convert.ToInt32(txtBookPrice.Value.Trim()) + "'," +Convert.ToInt32(txtBookCount.Value.Trim()) + ")"; 1楼正解。从页面读到的数字是string类型的,要强制转换一下,用int.Parse()也可以 string类型转换成int类型 使用int.Parse() 你的bookprice把不是int类型吗? 先检查一下你输入的是不是int型 string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "','" + ddlType.Text + "','" + txtWrite + "','" + txtChuban.Value + "','" + txtBookPrice + "'," +txtBookCount.Value + ")";是你 写错了吧 txtWrite是控件的ID把 string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "','" + ddlType.Text + "','" + txtWrite.Value+ "','" + txtChuban.Value + "','" + txtBookPrice.Value + "'," +txtBookCount.Value + ")"; //C#向数据库提供数据时 整型数据不需要加单引号 string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "'," + Convert.ToInt32(ddlType.SelectedValue.Trim()) + ",'" + txtWrite + "','" + txtChuban.Value + "'," + Convert.ToInt32(txtBookPrice.Value.Trim()) + "," +Convert.ToInt32(txtBookCount.Value.Trim()) + ")"; string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "'," + ddlType.Text + ",'" + txtWrite + "','" + txtChuban.Value + "'," + txtBookPrice + "," +txtBookCount.Value + ")";首先你要检查下输入的数据是否是数值类型,然后把sql语句改成上面的再试下, string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "'," + ddlType.Text + ",'" + txtWrite + "','" + txtChuban.Value + "'," + txtBookPrice + "," +txtBookCount.Value + ")";首先你要检查下输入的数据是否是数值类型,然后把sql语句改成上面的再试下, string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "'," + ddlType.Text + ",'" + txtWrite + "','" + txtChuban.Value + "'," + txtBookPrice + "," +txtBookCount.Value + ")";首先你要检查下输入的数据是否是数值类型,然后把sql语句改成上面的再试下, 先对输入的数据进行判断,包括非法值的判断如输入的不是数字 private static int ConvertStringToInteger(string s) { int result = 0; int.TryParse(s, out result); return result; }再根据实际情况判断0可不可以输入,这样输入什么都不会报错 请问如何实现"图层"? 如何用正则匹配 与或非?? 谢谢 按天生成的编号应该怎么写? 【散!散!散!散!散!散!散!散!散!散!】 搜索引擎的四种解决方案 同一套WEB程序和数据库,部署在远程两台服务器。如何让这两台服务器的数据库,传递数据? 一个逻辑问题 像csdn上左边是树形菜单,右边是网页内容,是怎么实现的? !OLEDB异常 mvc ActionFilters验证问题 代码实现将aspx页面添加到Web工程中 各位朋友 帮忙看段代码,
红的部分对应你数据库字段都是整型,也要进行类型转换改成这样试试(先判断这些值ddlType.Text不为空)string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "','" + Convert.ToInt32(ddlType.SelectedValue.Trim()) + "','" + txtWrite + "','" + txtChuban.Value + "','" + Convert.ToInt32(txtBookPrice.Value.Trim()) + "'," +Convert.ToInt32(txtBookCount.Value.Trim()) + ")";
string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "','" + ddlType.Text + "','" + txtWrite.Value+ "','" + txtChuban.Value + "','" + txtBookPrice.Value + "'," +txtBookCount.Value + ")";
//C#向数据库提供数据时 整型数据不需要加单引号
string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "'," + Convert.ToInt32(ddlType.SelectedValue.Trim()) + ",'" + txtWrite + "','" + txtChuban.Value + "'," + Convert.ToInt32(txtBookPrice.Value.Trim()) + "," +Convert.ToInt32(txtBookCount.Value.Trim()) + ")";
string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "'," + ddlType.Text + ",'" + txtWrite + "','" + txtChuban.Value + "'," + txtBookPrice + "," +txtBookCount.Value + ")";首先你要检查下输入的数据是否是数值类型,然后把sql语句改成上面的再试下,
string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "'," + ddlType.Text + ",'" + txtWrite + "','" + txtChuban.Value + "'," + txtBookPrice + "," +txtBookCount.Value + ")";首先你要检查下输入的数据是否是数值类型,然后把sql语句改成上面的再试下,
{
int result = 0;
int.TryParse(s, out result);
return result;
}再根据实际情况判断0可不可以输入,这样输入什么都不会报错