Update record with Stored Procedure using ASP.Net GridView RowUpdating event in C# and VB.Net

Last Reply 3 months ago By dharmendr

Posted 3 months ago

Dear Sir, 

kindly find my c# code and help me to complate update function.

Store procedur of DB:-

CREATE PROC [dbo].[Sp_GridCrud2] ( 
	@EmplooyeId int=0,
	@FirstName varchar(50)=Null,
	@LastName varchar(50)=Null,
	@PhoneNumber nvarchar(15)=Null, 
	@EmailAddress nvarchar(50)=Null,
	@Gender varchar(50)=Null,
	@datepicker nvarchar(18)=Null ,
	@Designation nvarchar(18)=Null,
	@Grade nvarchar(50)=null,
	@Department nvarchar(50)=Null, 
	@Status nvarchar(50)=Null, 
	@Dojstn nvarchar(50)=Null, 
	@PAN nvarchar(50)=null,
	@Qualifiction varchar(50)=null,
	@Discipline varchar(50)=null,
	@DateOfMarrige nvarchar(50)=null,
	@BloodGroup varchar(50)=null,
	@DomicileStates varchar(50)=null,
	@EnteryMode varchar(50)=null,
	@DOEinpresentGrade nvarchar(50)=null,
	@PresentOccumodationReq varchar(50)=null, 
	@Event varchar(10) ) 
AS 
BEGIN 
	IF(@Event='Select') 
	BEGIN 
		SELECT * FROM Employee2 ORDER BY FirstName ASC; 
	END 
	ELSE IF(@Event='Add') 
	BEGIN 
		INSERT INTO Employee2 (EmplooyeId,FirstName,LastName,PhoneNumber,EmailAddress,Gender,datepicker,Designation,Grade,Department,Status,Dojstn,PAN,Qualifiction,Discipline,DateOfMarrige,BloodGroup,DomicileStates,EnteryMode,DOEinpresentGrade,PresentOccumodationReq) 
		VALUES(@EmplooyeId,@FirstName,@LastName,@PhoneNumber,@EmailAddress,@Gender,@datepicker,@Designation,@Grade,@Department,@Status,@Dojstn,@PAN,@Qualifiction,@Discipline,@DateOfMarrige,@BloodGroup,@DomicileStates,@EnteryMode,@DOEinpresentGrade,@PresentOccumodationReq) 
	END 
	ELSE IF(@Event='Update') 
	BEGIN 
		UPDATE Employee2 
		SET FirstName=@FirstName,LastName=@LastName,PhoneNumber=@PhoneNumber,EmailAddress=@EmailAddress,Gender=@Gender,datepicker=@datepicker,Designation=@Designation,@Grade=Grade,Department=@Department,Status=@Status,Dojstn=@Dojstn,PAN=@PAN,Qualifiction=@Qualifiction,Discipline=@Discipline,DateOfMarrige=@DateOfMarrige,BloodGroup=@BloodGroup,DomicileStates=@DomicileStates,EnteryMode=@EnteryMode,DOEinpresentGrade=@DOEinpresentGrade,PresentOccumodationReq=@PresentOccumodationReq 
		where EmplooyeId=@EmplooyeId; 
	END
	ELSE 
	BEGIN 
		DELETE FROM Employee2 WHERE EmplooyeId=@EmplooyeId; 
	END 
END

aspx.cs

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                CreateConnection();
                OpenConnection();
                _sqlCommand.CommandText = "Sp_GridCrud2";
                _sqlCommand.CommandType = CommandType.StoredProcedure;
                _sqlCommand.Parameters.AddWithValue("@Event", "Update");
                _sqlCommand.Parameters.AddWithValue("@EmplooyeId", Convert.ToString(Session["EmplooyeId"]));  
                _sqlCommand.Parameters.AddWithValue("@FirstName", Convert.ToString(txtFirstName.Text.Trim()));
                _sqlCommand.Parameters.AddWithValue("@LastName", Convert.ToString(txtLastName.Text.Trim()));
                _sqlCommand.Parameters.AddWithValue("@PhoneNumber", Convert.ToString(txtPhoneNumber.Text.Trim()));
                _sqlCommand.Parameters.AddWithValue("@EmailAddress", Convert.ToString(txtEmailAddress.Text.Trim()));
                _sqlCommand.Parameters.AddWithValue("@Gender", Convert.ToString(txtGender.Text));
                _sqlCommand.Parameters.AddWithValue("@datepicker", Convert.ToString(datepicker.Text));
                _sqlCommand.Parameters.AddWithValue("@Designation", Convert.ToString(txtDesignation.Text));
                _sqlCommand.Parameters.AddWithValue("@Grade", Convert.ToString(txtGrade.Text));
                _sqlCommand.Parameters.AddWithValue("@Department", Convert.ToString(txtDepartment.Text));
                _sqlCommand.Parameters.AddWithValue("@Status", Convert.ToString(txtStatus.Text));
                _sqlCommand.Parameters.AddWithValue("@Dojstn", Convert.ToString(txtDojstn.Text));
                _sqlCommand.Parameters.AddWithValue("@PAN", Convert.ToString(txtPAN.Text.Trim()));
                _sqlCommand.Parameters.AddWithValue("@Qualifiction", Convert.ToString(txtQualifiction.Text.Trim()));
                _sqlCommand.Parameters.AddWithValue("@Discipline", Convert.ToString(txtDiscipline.Text.Trim()));
                _sqlCommand.Parameters.AddWithValue("@DateOfMarrige", Convert.ToString(txtDateOfMarrige.Text.Trim()));
                _sqlCommand.Parameters.AddWithValue("@BloodGroup ", Convert.ToString(txtBloodGroup.Text));
                _sqlCommand.Parameters.AddWithValue("@DomicileStates", Convert.ToString(txtDomicileStates.Text));
                _sqlCommand.Parameters.AddWithValue("@EnteryMode", Convert.ToString(txtEnteryMode.Text));
                _sqlCommand.Parameters.AddWithValue("@DOEinpresentGrade  ", Convert.ToString(txtDOEinpresentGrade.Text));
                _sqlCommand.Parameters.AddWithValue("@PresentOccumodationReq", Convert.ToString(txtPresentOccumodationReq.Text));

                int result = Convert.ToInt32(_sqlCommand.ExecuteNonQuery());
                if (result > 0)
                {
                    ShowAlertMessage("Record Is Updated Successfully");
                    grvEmployee.EditIndex = -1;
                    BindEmployeeData();
                    ClearControls();
                }
                else
                {
                    ShowAlertMessage("Failed");
                }
            }

            catch (Exception ex)
            {
                ShowAlertMessage("Check your input data");
            }
            finally
            {
                CloseConnection();
                DisposeConnection();
            }
        }
You are viewing reply posted by: dharmendr 3 months ago.