Hello Forum,
When saving an invoice data into database table, not all data was inserted. Some rows are left empty; the values of “Total” and “GrandTotal” labels did not insert while in the “Amount” label, some values were inserted, BUT the last value did not insert. From the screenshot I shared below, I have highlighted the labels that did not insert in red line and also in the data table. The “Amount” column in the table shows that some data were inserted except for the last figure.
Please can anyone help me out?
Here is my Table Structure
Id
|
Item
|
Qty
|
Rate
|
Amount
|
Total
|
VAT
|
GrandTotal
|
HTML
<asp:GridView ID="Gridview1" runat="server" Font-Size="x-Small" ShowFooter="True" AutoGenerateColumns="False" OnRowCreated="Gridview1_RowCreated"
class="table table-striped table-condensed table-bordered" Style="max-width: 100%" HeaderStyle-ForeColor="#00003D" Height="50px" HeaderStyle-Height="10px" HeaderStyle-BackColor="#D3D9E5">
<Columns>
<asp:TemplateField HeaderText="ITEM DESCRIPTION" ItemStyle-Width="47%">
<ItemTemplate>
<asp:TextBox ID="textBox1" runat="server" Class="form-control" Width="100%" Font-Size="Small" TextMode="MultiLine" Style="overflow: hidden; resize: none;" oninput="Resize(this)" />
<script type="text/javascript">
function Resize(textbox) {
textbox.style.height = "";
textbox.style.height = Math.min(textbox.scrollHeight, 300) + "px";
}
</script>
</ItemTemplate>
<FooterStyle HorizontalAlign="Left" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Class="btn btn-primary" BackColor="SteelBlue" Font-Size="Small" Text="+ Item" OnClick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="QUANTITY" ItemStyle-Width="11%">
<ItemTemplate>
<asp:TextBox CssClass="form-control" ID="txtQuantity" Height="25" Font-Size="Small" runat="server" Width="100%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RATE" ItemStyle-Width="19%">
<ItemTemplate>
<span class="currency-symbol" style="font-weight: bolder">NGN</span>
<asp:TextBox ID="txtRate" Height="25" runat="server" Width="80%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AMOUNT" ItemStyle-Width="17%">
<ItemTemplate>
<span class="currency-symbol" style="font-weight: bolder">NGN</span>
<asp:Label ID="lblAmount" runat="server" Text="" Font-Bold="True"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" ForeColor="Red" runat="server" OnClick="LinkButton1_Click">
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/del6.png" Height="25" />
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle Height="10px" />
</asp:GridView>
<hr />
<br />
<div class="total">
<asp:Label ID="Label1" runat="server" Font-Bold="true" Text="TOTAL"></asp:Label>
<asp:Label ID="lblTotal" runat="server" Text=""></asp:Label>
<br />
<br />
<asp:Label ID="Label5" runat="server" Font-Bold="true" Text="VAT (%):"></asp:Label>
<asp:TextBox ID="txtVAT" runat="server" CssClass="form-control" Font-Size="Small" placeholder="Value Added Tax"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label6" runat="server" Font-Bold="true" Text="GRAND TOTAL:"></asp:Label>
<asp:Label ID="lblGrandTotal" runat="server" Text=""></asp:Label>
<br />
<br />
<br />
<br />
<br />
</div>
<br />
<br />
<br />
<br />
</div>
<br />
<br />
</div>
<br />
<div>
<asp:Button ID="Button1" runat="server" CssClass="btn btn-primary" Font-Size="Small" BackColor="SteelBlue" Text="Save" OnClick="Button1_Click" />
</div>
C#
private void InsertRecords(StringCollection sc)
{
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
StringBuilder sb = new StringBuilder(string.Empty);
string[] splitItems = null;
List<string> item = new List<string>();
List<string> quantity = new List<string>();
List<string> rate = new List<string>();
List<string> amount = new List<string>();
foreach (string Item in sc)
{
if (Item.Contains(","))
{
splitItems = Item.Split(",".ToCharArray());
item.Add(splitItems[0]);
quantity.Add(splitItems[1]);
rate.Add(splitItems[2]);
amount.Add(splitItems[3]);
}
}
try
{
string sqlStatement = "INSERT INTO Tblinvoice (Item,Qty,Rate,Amount,Total,VAT,GrandTotal) VALUES (@Item,@Qty,@Rate,@Amount,@Total,@VAT,@GrandTotal)";
con.Open();
SqlCommand cmd = new SqlCommand(sqlStatement, con);
cmd.Parameters.AddWithValue("@Item", string.Join(",", item.ToArray()));
cmd.Parameters.AddWithValue("@Qty", string.Join(",", quantity.ToArray()));
cmd.Parameters.AddWithValue("@Rate", string.Join(",", rate.ToArray()));
cmd.Parameters.AddWithValue("@Amount", string.Join(",", amount.ToArray()));
cmd.Parameters.AddWithValue("@Total", lblTotal.Text.ToString());
cmd.Parameters.AddWithValue("@VAT", txtVAT.Text.ToString());
cmd.Parameters.AddWithValue("@GrandTotal", lblGrandTotal.Text.ToString());
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Records Successfuly Saved!');", true);
}
catch (SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
con.Close();
}
//Response.Redirect("Default.aspx");
}