foreach (GridViewRow itm in GVOutOfHouse.Rows)
        {
            Label lbRawID = ((Label)itm.FindControl("LBOTRawID"));
            TextBox txQuantity = ((TextBox)itm.FindControl("LBOTQuantity"));
            string sqlOutPut = "update " + hd + " set Amount = [Amount] - '"+ txQuantity.Text  +"'  where RawMaterialID = '" + lbRawID.Text + "'";
            conn.Open();
            SqlCommand cmd = new SqlCommand(sqlOutPut, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
        }string sqlOutPut = "update " + hd + " set Amount = [Amount] - '10'  where RawMaterialID = '" + lbRawID.Text + "'";
直接改成数字就可以
但是在textbox里,输入的 也是数值的噢,怎么换成变量 就无反应

解决方案 »

  1.   

     Amount = [Amount] - 10
      

  2.   

    替换成变量就不行 
    '"+ txQuantity.Text  +"'
      

  3.   

    '"+ txQuantity.Text +"'
    =>"+ txQuantity.Text +"
      

  4.   

    不行的  sql语法错误了  这样 
      

  5.   

    <asp:TextBox ID="LBOTQuantity" runat="server" Width="90%" Text='<%# DataBinder.Eval(Container.DataItem,"OTQuantity") %>'></asp:TextBox>
    HTML这个应该不受影响的
      

  6.   

    Convert.ToInt32()
    强制转换下
    你输入的数字,但是你获取的是字符串
      

  7.   

    TextBox txQuantity = Convert.ToInt32((TextBox)itm.FindControl("LBOTQuantity"));
    是这样转换么?
      

  8.   

    不用转换,去掉 [Amount] - 后面的单引号就行
      

  9.   


    好像是 int32的转换问题
      

  10.   

    但是去了 之后 提示 where 附近语法错误
     string sqlOutPut = "update " + hd + " set Amount = [Amount] - "+ txQuantity.Text  +"  where RawMaterialID = '" + lbRawID.Text + "'";
      

  11.   

    Amount = [Amount]
    =>
    Amount = Amount顺便加个断点 看下 sqlOutPut 的值 
      

  12.   

    最终生成的都是SQL 字符串不存在转换的问题,只是 输入的值是否是数字而已.
      

  13.   


    sqlOutPut 在哪看的噢
    我断点之后 鼠标移到'"+ txQuantity.Text  +"'
    显示 text   只有  ""
      

  14.   


    不过我在  Pageload里 
    dt.Columns.Add(new DataColumn("OTQuantity", typeof(int)));
    应该不会有影响吧
      

  15.   


    protected void BTSave_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ETHANConnectionString"].ConnectionString);
            SqlCommand comm = new SqlCommand();
            comm.Connection = conn;
            string hd = Request.QueryString["hd"];
            foreach (GridViewRow itm in GVOutOfHouse.Rows)
            {
                Label lbRawID = ((Label)itm.FindControl("LBOTRawID"));
                TextBox txQuantity = ((TextBox)itm.FindControl("LBOTQuantity"));
                string sqlOutPut = "update " + hd + " set Amount = Amount - '"+ txQuantity.Text +"'  where RawMaterialID = '" + lbRawID.Text + "'";
                conn.Open();
                SqlCommand cmd = new SqlCommand(sqlOutPut, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
            }
    这个是button的源代码