我是 asp.net 开发者的初学者,网格视图包含
ProductID, ProductName, Price, Qty, Total
默认设置在五列
如果选择产品名称则价格自动显示,但数量将输入用户。如果不输入数量则显示消息,如果任何一列完成填写保存数据库?
productName 是下拉列表,我的代码中需要服务器端代码
受保护的无效btnSave_Click(对象发送者,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;
}