当我在文本框中输入大于15的值时,计算不起作用

时间:2017-06-29 10:11:32

标签: html asp.net sql-server

--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(price)中输入一个大于15的值时,我的计算不起作用且值没有保存在数据库中......帮我解决这个问题。

我已经在sql中将所有列定义为十进制数据类型。这有什么问题吗?

1 个答案:

答案 0 :(得分: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") + ")";