2012-11-28 92 views
0

我begineer asp.net開發的, 網格視圖包含的GridView數據數據庫驗證

ProductID, ProductName, Price, Qty, Total 

默認設置五列

如果選擇產品名稱,然後價格顯示自動,但數量會被輸入用戶。 如果沒有輸入數量然後顯示消息, 如果任何一列完整填寫保存數據庫?

產品名稱是下拉列表,我需要服務器端代碼 在我的代碼

保護無效btnSave_Click(對象發件人,EventArgs的){

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; 

} 

回答

0

您可以添加asp.net必填字段驗證器gridview中的每個文本框。

<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

+0

requierfi​​eldvalidator所有txtQty,但依賴於選擇ProductName。我需要在c#中的服務器端代碼.net –

+0

如果您應用所需的字段驗證程序,它將適用於任何產品名稱或其他一些你有沒有嘗試過? – Dotnet

+0

如果您在行(itemtemplates)中有一個帶有輸入字段的gridview併爲其分配驗證程序,則每個元素都會單獨進行驗證,因爲每行都會生成一個元素和驗證程序。 – cbillowes

0

如果您正在使用默認設置中的所有五列,那麼你將不能夠使用的RequiredFieldValidator。嘗試regularexpressionvalidator。