(回帖给分)在将 varchar 值 转换成数据类型 int 时失败。

qqq379271395 2012-05-20 04:53:54
在将 varchar 值 'System.Web.UI.HtmlControls.HtmlInputText' 转换成数据类型 int 时失败。

我用datalist写了一个添加的代码

数据表里有字段
DB.ExecuteNonQuery(strSql);
btnNew.Visible = false;
Panel1.Visible = false;
DataList1.Visible = true;
DataBindDataList();
initTable();

}


DBconn.cs文件 用于连接。。倒数第二句话 cmd.ExecuteNonQuery(); 报错 说 在将 varchar 值 'System.Web.UI.HtmlControls.HtmlInputText' 转换成数据类型 int 时失败。



public class DBconn
{
private string strConn;
private SqlConnection con;
private SqlCommand cmd;
private SqlDataAdapter sda;
private DataSet ds;




public DBconn()
{
strConn = CreateConnn();
con = new SqlConnection(strConn);
cmd = new SqlCommand();
sda = new SqlDataAdapter();
ds = new DataSet();


}

public string CreateConnn() {
return ConfigurationManager.ConnectionStrings["libraryConnectionString"].ConnectionString;
}
public DataSet Getds(string StrSql) {
con.Open();
sda = new SqlDataAdapter(StrSql, con);
sda.Fill(ds);
con.Close();
return ds;
}

public void ExecuteNonQuery(string strSql) {
con.Open();
cmd.CommandText = strSql;
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();

}

}

[img=http://fmn.rrimg.com/fmn059/20120520/1655/main_UtsS_2a4e0000011d118c.jpg" alt="" />


...全文
573 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
llkaximoduo 2012-05-21
  • 打赏
  • 举报
回复
先对输入的数据进行判断,包括非法值的判断如输入的不是数字

private static int ConvertStringToInteger(string s)
{
int result = 0;
int.TryParse(s, out result);
return result;
}



再根据实际情况判断0可不可以输入,这样输入什么都不会报错
格桑花 2012-05-21
  • 打赏
  • 举报
回复
string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "'," + ddlType.Text + ",'" + txtWrite + "','" + txtChuban.Value + "'," + txtBookPrice + "," +txtBookCount.Value + ")";

首先你要检查下输入的数据是否是数值类型,然后把sql语句改成上面的再试下,
格桑花 2012-05-21
  • 打赏
  • 举报
回复

string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "'," + ddlType.Text + ",'" + txtWrite + "','" + txtChuban.Value + "'," + txtBookPrice + "," +txtBookCount.Value + ")";

首先你要检查下输入的数据是否是数值类型,然后把sql语句改成上面的再试下,
格桑花 2012-05-21
  • 打赏
  • 举报
回复

string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "'," + ddlType.Text + ",'" + txtWrite + "','" + txtChuban.Value + "'," + txtBookPrice + "," +txtBookCount.Value + ")";

首先你要检查下输入的数据是否是数值类型,然后把sql语句改成上面的再试下,
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "','" + ddlType.Text + "','" + txtWrite + "','" + txtChuban.Value + "',……
[/Quote]



//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()) + ")";

风2013 2012-05-21
  • 打赏
  • 举报
回复
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 + ")";
ltcszk 2012-05-21
  • 打赏
  • 举报
回复
先检查一下你输入的是不是int型
anzhiqiang_touzi 2012-05-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "','" + ddlType.Text + "','" + txtWrite + "','" + txtChuban.Value + "','……
[/Quote]
AI观星台 2012-05-20
  • 打赏
  • 举报
回复
int.tryparse一下
  • 打赏
  • 举报
回复
你的bookprice把不是int类型吗?
21克sam 2012-05-20
  • 打赏
  • 举报
回复
string类型转换成int类型 使用int.Parse()
Leo2048 2012-05-20
  • 打赏
  • 举报
回复
1楼正解。从页面读到的数字是string类型的,要强制转换一下,用int.Parse()也可以
EnForGrass 2012-05-20
  • 打赏
  • 举报
回复
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()) + ")";

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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