Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,
i am newbie to SPs. i want to perform insert,update and delete operations in one stored procedure.but i don't know how to pass flag or something like that to SP that SP knows that this flag is for this particular operation.

here is my code and store procedure:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
   {
       con.Open();
       string id = grd.Rows[e.RowIndex].Cells[0].Text;
       //cust.deleteRecord(string id);
       //SqlCommand cmd = new SqlCommand("delete from tblEmp where id=@id", con);
       SqlCommand cmd = new SqlCommand("deleteRecord", con);
       cmd.CommandType = CommandType.StoredProcedure;
       cmd.Parameters.AddWithValue("@id", id);
       cmd.ExecuteNonQuery();
       grd.EditIndex=-1;
       con.Close();
       binddata();
   }


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        con.Open();
        string id = grd.Rows[e.RowIndex].Cells[0].Text;
        string Name = ((TextBox)grd.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string City = ((TextBox)grd.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        string State = ((TextBox)grd.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        string Salary = ((TextBox)grd.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
        //SqlCommand cmd = new SqlCommand("update tblEmp set Name=@Name,City=@City,State=@State,Salary=@Salary where id=@id", con);
        SqlCommand cmd = new SqlCommand("updateRecord", con);
        cmd.CommandType = CommandType.StoredProcedure;
        //cust.updateRecord(string id, string Name,string City,string State,string Salary);
        cmd.Parameters.AddWithValue("@id", id);
        cmd.Parameters.AddWithValue("@Name", Name);
        cmd.Parameters.AddWithValue("@City", City);
        cmd.Parameters.AddWithValue("@State", State);
        cmd.Parameters.AddWithValue("@Salary", Salary);
        cmd.ExecuteNonQuery();
        grd.EditIndex = -1;
        con.Close();
        binddata();
    }



ALTER PROCEDURE [dbo].[operations]
@Id int,
@Name varchar(20),
@City varchar(20),
@State varchar(20),
@Salary int,
@Mode tinyint
as
Begin
	If @Mode = 1
		Begin
			insert into tblEmp(Name,City,[State],Salary) values(@Name,@City,@State,@Salary) 
		End
	If @Mode = 2
		Begin 
			update tblEmp
			set Name=@Name,City=@City,[State]=@State,Salary=@Salary where id=@id
		End
	If @Mode = 3
		Begin 
			delete from tblEmp where id=@id
		End
END


kindly please help.

Thanks & Regards,
Krunal kakadiya


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 29-Jun-11 2:51am
v2

1 solution

1)modify parameters of store procedure as below

ALTER PROCEDURE [dbo].[operations]
@Id int,
@Name varchar(20)=Null,
@City varchar(20)=Null,
@State varchar(20)=Null,
@Salary int = 0,
@Mode tinyint=0


2)

add following for GridView1_RowInserting

cmd.Parameters.AddWithValue("@Mode", 1);
add following for GridView1_RowUpdating

cmd.Parameters.AddWithValue("@Mode", 2);

add following for GridView1_RowDeleting

cmd.Parameters.AddWithValue("@Mode", 3);
 
Share this answer
 

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