2017-06-29 36 views
-1
--source code-- 

<table style="border: thin ridge #000000; width: 72%; margin-top: 21px; margin-left: 243px;" 
      bgcolor="#CCCCFF"> 
      <tr align="center"> 
       <td class="style11"> 
        &nbsp; 
       </td> 
       <td class="style2"> 
        &nbsp;</td> 
       <td class="style4"> 
        &nbsp;</td> 
       <td class="style17"> 
        &nbsp; 
       </td> 
      </tr> 
      <tr> 
       <td class="style14"> 
        &nbsp; 
       </td> 
       <td class="style15" align="center"> 
        <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" Text="0.125" 
         oncheckedchanged="CheckBox1_CheckedChanged1" CausesValidation="True" /> 

       </td> 
       <td class="style16" align="center"> 

         <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" Text="0.25" 
         oncheckedchanged="CheckBox2_CheckedChanged" CausesValidation="True" /> 


         </td> 
       <td class="style18"> 
        &nbsp; 
       </td> 
      </tr> 
      <tr align="center" style="background-color:Fuchsia;"> 
       <td class="style11"> 
        L</td> 
       <td class="style2"> 
        B&nbsp; 
       </td> 
       <td class="style4"> 
        W</td> 
       <td class="style17"> 
        N</td> 
      </tr> 
      <tr align="center" style="background-color:Fuchsia;"> 
       <td class="style12"> 
        <asp:UpdatePanel ID="UpdatePanel4" runat="server"> 
        <ContentTemplate> 
        <asp:TextBox ID="TextBox1" runat="server" Width="115px" TabIndex="1" ></asp:TextBox> 

        </ContentTemplate> 
        </asp:UpdatePanel> 
       </td> 
       <td class="style7"> 
        <asp:UpdatePanel ID="UpdatePanel5" runat="server"> 
        <ContentTemplate> 
        <asp:TextBox ID="TextBox2" runat="server" Width="115px" TabIndex="2"></asp:TextBox> 

        </ContentTemplate> 
        </asp:UpdatePanel> 
       </td> 
       <td class="style9"> 
        <asp:UpdatePanel ID="UpdatePanel3" runat="server"> 
        <ContentTemplate> 
        <asp:TextBox ID="TextBox3" runat="server" Width="115px" AutoPostBack="True" 
         ontextchanged="TextBox3_TextChanged" TabIndex="3"></asp:TextBox> 
        </ContentTemplate> 
        </asp:UpdatePanel> 

       </td> 
       <td class="style19"> 
        <asp:UpdatePanel ID="UpdatePanel6" runat="server"> 
        <ContentTemplate> 
        <asp:TextBox ID="TextBox4" runat="server" Width="115px" TabIndex="4"></asp:TextBox> 

        </ContentTemplate> 
        </asp:UpdatePanel> 
       </td> 
      </tr> 
      <tr><td class="style13"></td><td class="style8"> 
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
         ControlToValidate="TextBox2"></asp:RequiredFieldValidator> 
       </td> 
      <td class="style10"> 
        &nbsp;</td><td class="style20"></td></tr> 
      <tr align="right" style="background-color:snow;"> 
       <td class="style3" colspan="2"> 
        <asp:TextBox ID="TextBox7" runat="server" Width="142px"></asp:TextBox> 
       </td> 
       <td align="left" class="style4"> 
        &nbsp;&nbsp; 
        Price</td> 
       <td class="style17"> 
        &nbsp;</td> 
      </tr> 
      <tr align="center"> 
       <td class="style3" colspan="2"> 
        &nbsp;</td> 
       <td align="left" class="style4"> 
        &nbsp;</td> 
       <td class="style17"> 
        &nbsp;</td> 
      </tr> 
      <tr align="center"> 
       <td class="style3" colspan="2" align="right"> 
        <asp:Button ID="Button3" runat="server" onclick="Button3_Click" 
         Text="Calculate" CausesValidation="False" /> 
       </td> 
       <td align="left" colspan="2"> 
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
         Text="Calculate" /> 
         <input type="hidden" runat="server" id="hid" /></td> 
      </tr> 
     </table> 

--code behind-- 

protected void Button1_Click(object sender, EventArgs e) 
    { 
     l = Convert.ToDecimal(TextBox1.Text); 
     b = Convert.ToDecimal(TextBox2.Text); 
     w = Convert.ToDecimal(TextBox3.Text); 
     n = Convert.ToDecimal(TextBox4.Text); 
     price = Convert.ToDecimal(TextBox7.Text); 

     avg = (l * b * w * n)/4; 
     tot = avg * price; 


     try 
     { 
      string ins = "insert into cal_tbl(l,b,w,n,price,average,total)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox7.Text + "','" + avg.ToString("#,0.00") + "','" + tot.ToString("#,0.00") + "')"; 
      SqlCommand cmd = new SqlCommand(ins, con); 
      con.Open(); 
      cmd.ExecuteNonQuery(); 
      con.Close(); 
      Bindgrid(); 
     } 
     catch (Exception ex) 
     { 
     } 
     clearcontrols(); 
    } 

现在的问题是,当我进入textbox7(价格)大于15的值我的计算是不工作和值不会在数据库中保存。帮助我解决这个问题。当我进入文本框计算值大于15不工作

我已经在sql.Is中将所有列定义为十进制数据类型那里有任何问题吗?

+0

你应该学习sql注入和如何防止是。由于你的代码现在是非常脆弱的。第二,如果某人在文本框之一中输入了不可转换的值(如字符串),则代码会抛出错误。 – VDWWD

回答

0

您应该删除查询中的所有'。因为所有列都是十进制的。并应该使用.ToString("0.00")

string ins = "insert into cal_tbl(l,b,w,n,price,average,total)values(" + TextBox1.Text + "," + TextBox2.Text + "," + TextBox3.Text + "," + TextBox4.Text + "," + TextBox7.Text + "," + avg.ToString("0.00") + "," + tot.ToString("0.00") + ")";