Hi
this code below is an example of customer item order which multiplies the price by the quantity of item purchased by customer.
But i want to add this, if 0.5 is inserted into the QTY let the resullt be multiplied by 2.
That is if $50(price) X 5(QTY) SUM = $250, what i want to add if the QTY is 0.5 multiply by 2
example $50(price) x QTY(0.5)= $25 X 2 =$50
So if the inserted QTY is 0.5 then multiply the result by 2 like the above example
HTML
<table>
<tr>
<td>Item</td>
<td>Price</td>
<td>Qty</td>
</tr>
<tr>
<td><asp:TextBox ID="txtItem" runat="server" CssClass="form-control" /></td>
<td><asp:TextBox ID="TextBox1" runat="server" CssClass="form-control" /></td>
<td><asp:TextBox ID="txtQuantity" runat="server" CssClass="form-control" /></td>
<td><asp:Button ID="btnRemove" runat="server" OnClick="btnRemove_Click" Text="Remove" CssClass="btn btn-warning" /></td>
</tr>
</table>
<asp:GridView runat="server" ID="gvInsertedRecords" CssClass="datatable-dashv1-list custom-datatable-overright table table-hover" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Item" SortExpression="Item">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Item") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Item") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price" SortExpression="Price">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Price") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity" SortExpression="Quantity">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Quantity") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Quantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<div class="">
</div>
<div>
<table>
<tr>
<td>
<asp:PlaceHolder ID="ph1" runat="server" />
<br />
<asp:Button ID="btnAdd" runat="server" Text="Add" CssClass="btn btn-primary" />
</td>
</tr>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<br />
<asp:Literal ID="ltlCount" runat="server" Text="0" Visible="false" />
<asp:Literal ID="ltlRemoved" runat="server" Visible="false" />
<asp:Label ID="Label8" runat="server" Text="" Font-Bold="True" Font-Size="X-Large" ForeColor="Red"></asp:Label>
<div class=" pull-right">
<asp:Button Text="Submit" runat="server" OnClick="OnSave" CssClass="btn btn-default" />
<asp:Button Text="Calculate" runat="server" OnClick="Calculate" CssClass="btn btn-info" />
</div>
<div class="clearfix"></div>
<hr />
Code
protected void OnSave(object sender, EventArgs e)
{
if (Page.IsValid)
{
if (txtrecipt.Text == "")
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('Enter Recipt ... !!')</script>", false);
}
else
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Item"), new DataColumn("Price"), new DataColumn("Quantity") });
int i = 1;
foreach (Control c in ph1.Controls)
{
if (c.GetType().Name.ToLower() == "usercontrol_ascx")
{
UserControl uc = (UserControl)c;
TextBox tbItem = uc.FindControl("txtItem") as TextBox;
TextBox tbPrice = uc.FindControl("txtPrice") as TextBox;
TextBox tqty = uc.FindControl("txtQuantity") as TextBox;
TextBox half = uc.FindControl("txthalf") as TextBox;
if (!string.IsNullOrEmpty(tbItem.Text.Trim()) && !string.IsNullOrEmpty(tbPrice.Text.Trim()) && !string.IsNullOrEmpty(tqty.Text.Trim()) && !string.IsNullOrEmpty(half.Text.Trim()))
{
//dt.Rows.Add(tbItem.Text.Trim(), tbPrice.Text.Trim(), tqty.Text.Trim());
int availableQuantity = 0;
if (!string.IsNullOrEmpty(GetAvailableQuantity(tbItem.Text.Trim())))
{
availableQuantity = Convert.ToInt32(GetAvailableQuantity(tbItem.Text.Trim()));
}
if (Convert.ToInt32(tqty.Text.Trim()) > availableQuantity)
{
ScriptManager.RegisterClientScriptBlock(sender as Button, (sender as Button).GetType(), "alert" + i.ToString(), "<script>alert('No of Quantity entered is not available in Stock')</script>", false);
}
else
{
dt.Rows.Add(tbItem.Text.Trim(), tbPrice.Text.Trim(), tqty.Text.Trim());
int inserted = Insert(tbItem.Text.Trim(), tbPrice.Text.Trim(), tqty.Text.Trim());
if (inserted > 0)
{
UpdateStock(tbItem.Text.Trim(), tqty.Text.Trim());
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Data Submitted Successfully ... !!')", true);
}
}
}
}
}
// BindGridID();
gvInsertedRecords.DataSource = dt;
gvInsertedRecords.DataBind();
}
}
}
private string GetAvailableQuantity(string item)
{
string availableQuantity = "";
string constr = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "SELECT Qty_Remaining FROM Store_Table WHERE Item = @Item AND Store=@Store";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Store", Department.SelectedItem.Text);
cmd.Parameters.AddWithValue("@Item", item);
con.Open();
availableQuantity = Convert.ToString(cmd.ExecuteScalar());
con.Close();
}
}
return availableQuantity;
}
private int Insert(string item, string price, string qty)
{
int i = 0;
string constr = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "INSERT INTO ItemOrdered (Store,SellsPerson,Receipt,Item,Price,Quantity,Sum) VALUES (@Store,@SellsPerson,@Receipt,@Item, @Price, @Quantity,@Sum)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Store", Department.SelectedItem.Text);
cmd.Parameters.AddWithValue("@SellsPerson", HttpContext.Current.User.Identity.Name);
cmd.Parameters.AddWithValue("@Receipt", txtrecipt.Text);
cmd.Parameters.AddWithValue("@Item", item);
cmd.Parameters.AddWithValue("@Price", price);
cmd.Parameters.AddWithValue("@Quantity", qty);
cmd.Parameters.AddWithValue("@Sum", Convert.ToDecimal(price) * Convert.ToDecimal(qty));
con.Open();
i = cmd.ExecuteNonQuery();
con.Close();
}
}
return i;
}