I am taking single button for insert records and also to update records.I am also taken hidden field, so like after clicking that button if hidden field is null, then record must be inserted with given new values and if hidden field is not null,means already id is there,then record must be updated with modified values.
What I have tried:
DAL:
public void save_Click(int pid, string pname, string desc, int qnty, decimal pric)
{
SqlCommand cmd = new SqlCommand("SPSAVEDATA", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@P_id", pid);
cmd.Parameters.AddWithValue("@P_name", pname);
cmd.Parameters.AddWithValue("@P_desc", desc);
cmd.Parameters.AddWithValue("@P_qty", qnty);
cmd.Parameters.AddWithValue("@P_price", pric);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
BAL:
public void Product(int pid, string pname, string desc, int qnty, decimal pric)
{
dll.save_Click(pid, pname, desc, qnty, pric );
}
CS:
protected void Btn_save_Click(object sender, EventArgs e)
{
if (HiddenField1.Value == "")
{
bll.Product(txt_proName.Text,txt_ldesc.Text,Convert.ToInt32(txt_qnty.Text), Convert.ToDecimal(txt_price.Text));
Response.Write("<script>alert('Product Added Successfully')</script>");
}
else
{
bll.Product(Convert.ToInt16(HiddenField1.Value), txt_proName.Text, txt_ldesc.Text, Convert.ToInt32(txt_qnty.Text), Convert.ToDecimal(txt_price.Text));
}
Response.Write("<script>alert('Product Updated Successfully')</script>");
}
GridView1.DataSource = bll.LoadProduct();
GridView1.DataBind();
}
Stored Procedure
CREATE PROC SPSAVEDATA
@P_id int,
@P_name varchar(50),
@P_desc varchar(400),
@P_qty int,
@P_price money
AS
BEGIN
if(@P_id=0)
BEGIN
insert into Products1 (P_name,P_desc,P_qty,P_price) values (@P_name,@P_desc,@P_qty,@P_price)
END
ELSE
BEGIN
update Products1 set P_name=@P_name,P_desc=@P_desc,P_qty=@P_qty,P_price=@P_price where P_id=@P_id
END
END
i have tried above code but unable to get expected results.