Click here to Skip to main content
15,886,625 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 18-Apr-19 9:08am

Quote:
public void Product(int pid, string pname, string desc, int qnty, decimal pric)
...
bll.Product(txt_proName.Text, txt_ldesc.Text, Convert.ToInt32(txt_qnty.Text), Convert.ToDecimal(txt_price.Text));

Your Product method expects five parameters. You are only passing four parameters.
 
Share this answer
 
Comments
Member 14185275 18-Apr-19 15:12pm    
I am passing 4 parameters because id i cannot pass, it will be Auto generated for new records.
Richard Deeming 18-Apr-19 15:13pm    
And as I said, your method is declared as taking FIVE parameters.

You cannot call a method that expects FIVE parameters if you only pass FOUR parameters.

Based on your stored procedure, you need to pass 0 as the first parameter for an insert.
Member 14185275 18-Apr-19 15:17pm    
Thanx Richard, for ur response
I will pass zero in the place of id and check it is accepting or not
Member 14185275 19-Apr-19 2:43am    
Thank u Richard,
it worked for me, thanx a lot
CREATE PROC SPSAVEDATA
	@P_id int,
	@P_name varchar(50),
	@P_desc varchar(400),
	@P_qty int,
	@P_price money
AS 
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
	
	IF (@@ROWCOUNT = 0)
	BEGIN
		INSERT INTO Products1 (P_name,P_desc,P_qty,P_price) 
		VALUES (@P_name,@P_desc,@P_qty,@P_price) 
	END
END

I prefer to use this pattern because a record is only inserted once but could be updated many times therefore it makes sense to assume that you will be updating the record.

If after attempting the UPDATE operation the @@ROWCOUNT = 0 it means it could not find a record to update there it will perform an INSERT operation.
 
Share this answer
 
Comments
Member 14185275 18-Apr-19 15:14pm    
Hi Tony, tanx for the reply
I will check with this stored procedure once and I will update

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900