This code is not submitting data when save button is clicked and the data submitted successully is not poping up after other conditions are met.
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;
if (!string.IsNullOrEmpty(tbItem.Text.Trim()) && !string.IsNullOrEmpty(tbPrice.Text.Trim()) && !string.IsNullOrEmpty(tqty.Text.Trim()))
{
int number;
bool isNumeric = int.TryParse(tqty.Text.Trim(), out number);
if (isNumeric)
{
if (Convert.ToInt32(number) > 0)
{
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);
}
gvInsertedRecords.DataSource = dt;
gvInsertedRecords.DataBind();
}
}
}
else
{
ScriptManager.RegisterClientScriptBlock(sender as Button, (sender as Button).GetType(), "number" + i.ToString(), "<script>alert('Enter Quantity in number format.')</script>", false);
}
}
}
i++;
}
}
}
}
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;
}
private void UpdateStock(string item, string qty)
{
string constr = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "UPDATE Store_Table SET Qty_Remaining = Qty_Remaining - @Quantity WHERE Item = @Item AND Store=@Store";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Item", item);
cmd.Parameters.AddWithValue("@Quantity", qty);
cmd.Parameters.AddWithValue("@Store", Department.SelectedItem.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}