Issue in saving correct value of last column inside Gridview
it is saving 0 for all the the last column of gridview whereas it must save the value 4, 8, 4.
how to get solution?
i used the jquery for row calculation such as I applied the following formula in the jquery
Quantity / Bag = 120/30 = 4
Quantity / Bag = 160/20 = 8
Quantity / Bag = 200/50 = 4
where is the error and how to fix???
i wrote the following jquery
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=txtQuantity]").val("0");
});
$("body").on("change keyup", "[id*=txtQuantity]", function () {
//Check whether Quantity value is valid Float number.
var quantity = parseFloat($.trim($(this).val()));
if (isNaN(quantity)) {
quantity = 0;
}
//Update the Quantity TextBox.
$(this).val(quantity);
//Calculate and update Row Total.
var row = $(this).closest("tr");
// $("[id*=lblTotal]", row).html(parseFloat($(".bag", row).html()) / parseFloat($(this).val()));
$("[id*=hdnfldBag]", row).html(parseFloat($(this).val()) / parseFloat($(".bag", row).html()));
//Calculate and update Grand Total.
var grandTotal = 0;
$("[id*=hdnfldBag]").each(function () {
grandTotal = grandTotal + parseFloat($(this).html());
});
$("[id*=lblGrandTotal]").html(grandTotal.toString());
});
</script>
now I want to save this gridview data in the database. for this i wrote the following code
<div class="row">
<div class="col-sm-12 ">
<div class="table-responsive">
<asp:GridView ID="GridView1" runat="server" AllowPaging="false" Class="table table-striped table-bordered table-hover"
AutoGenerateColumns="false" RowStyle-Wrap="false" HeaderStyle-Wrap="true">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="checkAll" runat="server" onclick="checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" onclick="Check_Click(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product ID" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="200px" DataField="P_Type" HeaderText="Product Category" />
<asp:BoundField ItemStyle-Width="200px" DataField="P_Name" HeaderText="Product Name" />
<asp:BoundField DataField="BSize" HeaderText="Bag Size" ItemStyle-CssClass="bag" />
<asp:TemplateField>
<HeaderTemplate>
Quantity</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Text="0" class="field1 form-control"
Width="100"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Rate</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtRate" runat="server" Text="0" class="field2 form-control" Width="100"
ItemStyle-CssClass="price"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Discount</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtDiscount" runat="server" Text="0" class="field3 form-control"
Width="100" ItemStyle-CssClass="discount"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Net Balance</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtNet" runat="server" Text="0" class="field4 form-control" Width="100"
ItemStyle-CssClass="pay"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Paid</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtPaid" runat="server" Text="0" class="field5 form-control" Width="100"
ItemStyle-CssClass="paid"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
RemBal</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtRem" runat="server" Text="0" class="field6 form-control" Width="100"
ItemStyle-CssClass="rem"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" Text="0" class="control-label"></asp:Label>
<asp:HiddenField ID="hdnfldBag" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
</div>
protected void btnShow_Click(object sender, EventArgs e)
{
int select = 0;
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
GridViewRow row = GridView1.Rows[i];
CheckBox Chbox = (CheckBox)row.FindControl("CheckBox1");
if (Chbox.Checked == true)
{
select++;
}
}
if (select == 0)
{
GridView1.Visible = true;
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Please Select Record to Save');", true);
}
foreach (GridViewRow gvrow in GridView1.Rows)
{
CheckBox chck = gvrow.FindControl("CheckBox1") as CheckBox;
if (chck.Checked)
{
con = new SqlDbConnect();
string Inv = txtInv.Text.Trim();
string Cat = ddlHead.SelectedValue.ToString();
string Ven = ddlVendor.SelectedValue.ToString();
string date = txtDate.Text.Trim();
string PId = (gvrow.FindControl("lblCode") as Label).Text;
string Bag = ((HiddenField)gvrow.FindControl("hdnfldBag")).Value;
string Qty = (gvrow.FindControl("txtQuantity") as TextBox).Text;
string Rate = (gvrow.FindControl("txtRate") as TextBox).Text;
string Discount = (gvrow.FindControl("txtDiscount") as TextBox).Text;
string Net = (gvrow.FindControl("txtNet") as TextBox).Text;
string Paid = (gvrow.FindControl("txtPaid") as TextBox).Text;
string Rem = (gvrow.FindControl("txtRem") as TextBox).Text;
con.SqlQuery("INSERT INTO tblPurchase (InvoiceNo,P_Date,VendorID,ProID,ProductID,Quantity,Price,Discount,NetBal,Paid,RemBal,BagNo) VALUES (@No,@Date,@VId,@PrId,@PId,@Qty,@Rate,@Dis,@Net,@Paid,@Rem,@BNo)");
con.Cmd.Parameters.AddWithValue("@No", Inv);
con.Cmd.Parameters.AddWithValue("@PrId", Cat);
con.Cmd.Parameters.AddWithValue("@VId", Ven);
con.Cmd.Parameters.AddWithValue("@Date", date.ToString());
con.Cmd.Parameters.AddWithValue("@PId", PId);
con.Cmd.Parameters.AddWithValue("@Qty", Qty);
con.Cmd.Parameters.AddWithValue("@Rate", Rate);
con.Cmd.Parameters.AddWithValue("@Dis", Discount);
con.Cmd.Parameters.AddWithValue("@Net", Net);
con.Cmd.Parameters.AddWithValue("@Paid", Paid);
con.Cmd.Parameters.AddWithValue("@Rem", Rem);
con.Cmd.Parameters.AddWithValue("@BNo", Bag.ToString()); //gvrow.Cells[2].Text
con.NonQueryEx();
con.conClose();
this.VoucherNo();
}
}
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Record Saved Successfully');", true);
}