在将 varchar 值 'System.Web.UI.HtmlControls.HtmlInputText' 转换成数据类型 int 时失败。我用datalist写了一个添加的代码数据表里有字段

解决方案 »

  1.   

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

  2.   

    1楼正解。从页面读到的数字是string类型的,要强制转换一下,用int.Parse()也可以
      

  3.   

    string类型转换成int类型  使用int.Parse()
      

  4.   

    你的bookprice把不是int类型吗?
      

  5.   

    先检查一下你输入的是不是int型
      

  6.   

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

  7.   


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

  8.   


     string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "'," + ddlType.Text + ",'" + txtWrite + "','" + txtChuban.Value + "'," + txtBookPrice + "," +txtBookCount.Value + ")";首先你要检查下输入的数据是否是数值类型,然后把sql语句改成上面的再试下,
      

  9.   


     string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "'," + ddlType.Text + ",'" + txtWrite + "','" + txtChuban.Value + "'," + txtBookPrice + "," +txtBookCount.Value + ")";首先你要检查下输入的数据是否是数值类型,然后把sql语句改成上面的再试下,
      

  10.   

     string strSql = "insert into book (bookname,typeid,write,chubanshename,bookprice,xiancunnumber) values('" + txtBookName.Value + "'," + ddlType.Text + ",'" + txtWrite + "','" + txtChuban.Value + "'," + txtBookPrice + "," +txtBookCount.Value + ")";首先你要检查下输入的数据是否是数值类型,然后把sql语句改成上面的再试下,
      

  11.   

    先对输入的数据进行判断,包括非法值的判断如输入的不是数字 private static int ConvertStringToInteger(string s)
            {
                int result = 0;
                int.TryParse(s, out result);
                return result;
            }再根据实际情况判断0可不可以输入,这样输入什么都不会报错