GridView数据到数据库进行验证

时间:2012-11-28 09:38:05

标签: c# asp.net validation

我是asp.net开发者的初学者, 网格视图包含

ProductID, ProductName, Price, Qty, Total

默认设置在五列

如果选择productname然后自动显示价格,但是Qty将进入用户。 如果没有输入数量然后显示消息, 如果任何一列完成填充保存数据库?

productName是下拉列表,我需要服务器端代码 在我的代码中

protected void btnSave_Click(object sender,EventArgs e)     {

    SqlDataAdapter sda;
    SqlCommand cmd;
    DateTime savedate = DateTime.ParseExact(txtBillDate.Text.Trim() + " " + DateTime.Now.ToString("hh:mm:ss tt"), "dd/MM/yyyy hh:mm:ss tt", null);

    TextBox txtProductID, txtPrice, txtQty, txtTotal;
    DropDownList ddlProductName;
    DataTable mdt = new DataTable();
    Label lblGrandTotal;

   if (DataCheck())
    {
        if (txtMobileNumber.Text != "")
        {
            con.Open();
            cmd = new SqlCommand("insert into Billing(BillNumber,BillDate,CustomerName,CustomerMobile) values('" + txtBillNumber.Text + "','" + savedate + "','" + ddlCustomerName.SelectedItem.Text + "','" + txtMobileNumber.Text + "')", con);

            for (int i = 0;i< GridView1.Rows.Count ; i++)
            {

                txtProductID = (TextBox)(GridView1.Rows[i].FindControl("txtProductID"));
                ddlProductName = (DropDownList)(GridView1.Rows[i].FindControl("ddlProductName"));
                txtPrice = (TextBox)(GridView1.Rows[i].FindControl("txtPrice"));
                txtQty = (TextBox)(GridView1.Rows[i].FindControl("txtQty"));
                txtTotal = (TextBox)(GridView1.Rows[i].FindControl("txtTotal"));
                lblGrandTotal = (Label)(GridView1.Rows[i].FindControl("lblGrandTotal"));


                    sda = new SqlDataAdapter("insert into BillingChild(ProductID,ProductName,Price,Qty,Total,BillNumber,BillDate,CustomerName,MobileNumber,BillChildNumber) values('" + txtProductID.Text + "','" + ddlProductName.SelectedItem + "','" + Convert.ToDecimal(txtPrice.Text) + "','" + Convert.ToDecimal(txtQty.Text) + "','" + Convert.ToDecimal(txtTotal.Text) + "','" + txtBillNumber.Text + "','" + savedate + "','" + ddlCustomerName.SelectedItem + "','" + txtMobileNumber.Text + "','" + txtBillChildNumber.Text + "')", con);
                    sda.Fill(mdt);
                    cmd.ExecuteNonQuery();



            }

            con.Close();
        }
    }
   else
   {
       Response.Write("<Script>alert('plz enter Qty')</script>");
   }


 }







public bool DataCheck()
{
    //TextBox txtProductID = null, txtPrice = null, txtQty = null, txtTotal = null;
    //DropDownList ddlProductName = null;
    //Label lblGrandTotal = null;
    TextBox txtProductID, txtPrice, txtQty, txtTotal;
    DropDownList ddlProductName;
    Label lblGrandTotal;
    if (GridView1.Rows.Count != 0)
    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {

            txtProductID = (TextBox)(GridView1.Rows[i].FindControl("txtProductID"));
            ddlProductName = (DropDownList)(GridView1.Rows[i].FindControl("ddlProductName"));
            txtPrice = (TextBox)(GridView1.Rows[i].FindControl("txtPrice"));
            txtQty = (TextBox)(GridView1.Rows[i].FindControl("txtQty"));
            txtTotal = (TextBox)(GridView1.Rows[i].FindControl("txtTotal"));
            lblGrandTotal = (Label)(GridView1.Rows[i].FindControl("lblGrandTotal"));


            if (txtQty.Text != "")
            {
                continue;

            }

            else
            {
                return false;
            }
        }
    }

    return true;

}

2 个答案:

答案 0 :(得分:0)

您可以在gridview中的每个文本框中添加asp.net必填字段验证器。

<asp:TextBox id="txtQty" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="reqQty" ControlToValidate="txtQty" ErrorMessage="*"></asp:RequiredFieldValidator>

<强>精化

T-SQL

create table products
( 
    id int identity(1,1),
    name varchar(500),
    price decimal(18,2)
)

insert into products values ('Soap', 15.0)
insert into products values ('Provitas', 25.0)
insert into products values ('Paper', 10.0)
insert into products values ('Foam Bath', 150.0)

ASPX

    <asp:GridView ID="gvTest" runat="server" DataSourceID="SqlTest" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField HeaderText="ID" DataField="ID" />
            <asp:BoundField HeaderText="Name" DataField="Name" />
            <asp:BoundField HeaderText="Price" DataField="Price" />
            <asp:TemplateField HeaderText="Qty">
                <ItemTemplate>
                    <asp:TextBox ID="txtQty" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="reqQty" runat="server" ControlToValidate="txtQty" ErrorMessage="*"></asp:RequiredFieldValidator>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlTest" runat="server" ConnectionString="Data Source=.\SQLEXPRESS;Initial Catalog=play;Persist Security Info=True;User ID=user;Password=userpassword" SelectCommand="SELECT id, name, price FROM Products"></asp:SqlDataSource>
    <asp:Button ID="cmdTest" runat="server" Text="Submit" />

C#按钮单击处理程序以获取用户输入的数量

protected void cmdTest_Click(object sender, EventArgs e)
{
    for (int i = 0; i < gvTest.Rows.Count; i++)
    {
        int qty = Convert.ToInt32(((TextBox)gvTest.Rows[i].FindControl("txtQty")).Text);
        // code here
    }
}

验证结果

enter image description here

答案 1 :(得分:0)

如果您在所有五列中使用默认设置,那么您将无法使用requiredfieldvalidator。尝试使用regularexpressionvalidator。