Friday, September 10, 2010

Gridview control with Edit Delete and Update in ASP.NET using C#

 Gridview control with Edit Delete and Update in ASP.NET  
 Gridview Edit/Delete/Update using sqlhelper class and 3 layer architecture.  
 In this article I have tried to make the simple Add , Edit, Update and Delete functions in ASP.Net GridView.  
 Feature of this GridView  
 1.This example used three tier architecture.  
 2.Add new record using footer.  
 3.Update and delete record.  
 4.For edit and delete i have used image button for nice look.  
 5.If table is empty(No record in table) a blank dynamic Gridview display for with Add New buttion.  
 6.On Click Edit or Add New cursor focus to Textbox.  
 7.Auto Generated Serial Number In Gridview Control.  
 I have used property layer but i didnt write here,i think you can implement it according to your logic.  
 MySample.aspx page  
 <asp:GridView ID="GridViewEmpSkill" runat="server" AllowPaging="True" AutoGenerateColumns="False"  
 PageSize="12" OnPageIndexChanging="GridViewEmpSkill_PageIndexChanging" OnRowDataBound="GridViewEmpSkill_RowDataBound"  
 TabIndex="2" Width="697px" OnRowCancelingEdit="GridViewEmpSkill_RowCancelingEdit" OnRowEditing="GridViewEmpSkill_RowEditing"  
 OnRowUpdating="GridViewEmpSkill_RowUpdating" ShowFooter="True" OnRowCommand="GridViewEmpSkill_RowCommand"  
 OnRowDeleting="GridViewEmpSkill_RowDeleting" DataKeyNames="EmpSkillId">  
      <Columns>  
           <asp:TemplateField HeaderText="Sr No">  
                <ItemTemplate>  
                <asp:Label ID="lblSrNo" runat="server"></asp:Label>  
                </ItemTemplate>  
                <FooterTemplate>  
                <asp:Label ID="lblNewSrNo" runat="server"></asp:Label>  
                </FooterTemplate>  
                <ItemStyle HorizontalAlign="Center" Width="50px" />  
           </asp:TemplateField>  
           <asp:TemplateField HeaderText="EmpSkillId" Visible="False">  
                <ItemTemplate>  
                <asp:Label ID="lblEmpSkillId" runat="server" Text='<%# Bind("EmpSkillId") %>'></asp:Label>  
                </ItemTemplate>  
           </asp:TemplateField>  
           <asp:TemplateField HeaderText="EmpSkill">  
                <EditItemTemplate>  
                <asp:TextBox ID="txtEmpSkill" runat="server" Width="493px" BorderColor="White" BorderWidth="0px"  
                Height="14px" Text='<%# Bind("EmpSkill") %>'></asp:TextBox>  
                </EditItemTemplate>  
                <FooterTemplate>  
                <asp:TextBox ID="txtNewEmpSkill" runat="server" Width="493px" BorderColor="White" BorderWidth="0px"  
                Height="14px" Visible="false"></asp:TextBox>  
                </FooterTemplate>  
                <ItemTemplate>  
                <asp:Label ID="lblEmpSkill" runat="server" Text='<%# Bind("EmpSkill") %>'></asp:Label>  
                </ItemTemplate>  
                <FooterStyle HorizontalAlign="Left" />  
                <ItemStyle HorizontalAlign="Left" />  
           </asp:TemplateField>  
           <asp:TemplateField HeaderText="Activity">  
                <ItemTemplate>  
                <asp:ImageButton ID="imgEdit" runat="server" ImageUrl="~/Image/img_edit.png" CommandName="Edit" ToolTip="Edit" />  
                <asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/Image/img_delete.png" ToolTip="Delete"  
                CommandName="Delete" OnClientClick="return ConfirmDelete();" />  
                </ItemTemplate>  
                <EditItemTemplate>  
                <asp:ImageButton ID="imgUpdate" runat="server" CausesValidation="True" CommandName="Update" ToolTip="Update"  
                ImageUrl="~/Image/update.png" Text="Update" />  
                <asp:ImageButton ID="imgCancel" runat="server" CausesValidation="False" CommandName="Cancel" ToolTip="Cancel"  
                ImageUrl="~/Image/cancel.png" Text="Cancel" />  
                </EditItemTemplate>  
                <FooterTemplate>  
                <asp:ImageButton ID="imgNewAdd" runat="server" ImageUrl="~/Image/row_add.png"  
                CommandName="AddNew" ToolTip="Add New" />  
                <asp:ImageButton ID="imgbtnNewInsert" runat="server" CausesValidation="True" CommandName="Insert" ToolTip="Save"  
                ImageUrl="~/Image/add.png" Visible="false" Text="Add" />  
                <asp:ImageButton ID="imgNewCancel" runat="server" CausesValidation="False" CommandName="Cancel" ToolTip="Cancel"  
                ImageUrl="~/Image/cancel.png" Visible="false" Text="Cancel" />  
                </FooterTemplate>  
                <FooterStyle HorizontalAlign="Center" Width="140px" />  
                <ItemStyle HorizontalAlign="Center" Width="140px" />  
           </asp:TemplateField>  
      </Columns>  
 </asp:GridView>  
 ===========  
 MySample.cs page  
 protected void GridViewEmployeeSkill()  
 {  
      try  
      {  
           Ds = SkillBLLobj.GetEmpSkillData();  
           if (Ds.Tables[0].Rows.Count > 0)  
           {  
                GridViewEmpSkill.DataSource = Ds;  
                GridViewEmpSkill.DataBind();  
           }  
           else  
           {  
                ShowNoResultFound(Ds,GridViewEmpSkill);  
                SrNoSkill = 1;  
           }  
      }  
      catch (Exception ex)  
      {  
           msgEmpSkill.Text = ex.Message;  
      }  
 }  
 protected void GridViewEmpSkill_RowEditing(object sender, GridViewEditEventArgs e)  
 {  
      GridViewEmpSkill.EditIndex = e.NewEditIndex;  
      GridViewEmpSkill();  
      GridViewEmpSkill.Rows[e.NewEditIndex].FindControl("txtEmpSkill").Focus();  
 }  
 protected void GridViewEmpSkill_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)  
 {  
      GridViewEmpSkill.EditIndex = -1;  
      GridViewSkill();  
 }  
 protected void GridViewEmpSkill_PageIndexChanging(object sender, GridViewPageEventArgs e)  
 {  
      GridViewEmpSkill.PageIndex = e.NewPageIndex;  
      GridViewSkill();  
 }  
 protected void GridViewEmpSkill_RowDataBound(object sender, GridViewRowEventArgs e)  
 {  
      if (e.Row.RowType == DataControlRowType.DataRow)  
      {  
           Label lblSrNo = (Label)e.Row.FindControl("lblSrNo");  
           lblSrNo.Text = SrNo.ToString();  
           SrNo++;   
      }  
 }  
 protected void GridViewEmpSkill_RowUpdating(object sender, GridViewUpdateEventArgs e)  
 {  
      try  
      {   
           SkillClsobj.SkillId = Convert.ToInt32(((Label)GridViewEmpSkill.Rows[e.RowIndex].FindControl("lblEmpSkillId")).Text);  
           SkillClsobj.Skill = Convert.ToString(((TextBox)GridViewEmpSkill.Rows[e.RowIndex].FindControl("txtEmpSkill")).Text);  
           int Update = SkillBLLobj.UpdateEmpSkill(SkillClsobj);  
           if (Update > 0)  
           {  
                msgEmpSkill.Text = "Record Updated Successfully !";  
                GridViewEmpSkill.EditIndex = -1;  
                GridViewSkill();  
           }  
      }  
      catch (Exception ex)  
      {  
           msgEmpSkill.Text = ex.Message.ToString();  
      }  
 }  
 protected void GridViewEmpSkill_RowCommand(object sender, GridViewCommandEventArgs e)  
 {  
      try  
      {  
      if (e.CommandName.Equals("AddNew") || e.CommandName.Equals("Insert"))  
      {  
      if (e.CommandName.Equals("AddNew"))  
      {  
      GridViewEmpSkill.EditIndex = -1;  
      GridViewSkill();   
      Label lblNewSrNo = (Label)GridViewEmpSkill.FooterRow.FindControl("lblNewSrNo");  
      lblNewSrNo.Text = SrNo.ToString();  
      ImageButton imgNewInsert = (ImageButton)GridViewEmpSkill.FooterRow.FindControl("imgNewInsert");  
      ImageButton imgNewCancel = (ImageButton)GridViewEmpSkill.FooterRow.FindControl("imgNewCancel");  
      ImageButton imgNewAdd = (ImageButton)GridViewEmpSkill.FooterRow.FindControl("imgNewAdd");  
      imgNewInsert.Visible = true;  
      imgNewCancel.Visible = true;  
      imgNewAdd.Visible = false;  
      TextBox txtNewEmpSkill = (TextBox)GridViewEmpSkill.FooterRow.FindControl("txtNewEmpSkill");  
      txtNewEmpSkill.Visible = true;  
      txtNewEmpSkill.Focus();  
      }  
      }  
      }  
      catch(Exception ex)  
      {  
      throw ex;  
      }  
      try  
      {  
           if (e.CommandName.Equals("Insert"))  
           {  
                ImageButton imgNewInsert = (ImageButton)GridViewEmpSkill.FooterRow.FindControl("imgNewInsert");  
                ImageButton imgNewCancel = (ImageButton)GridViewEmpSkill.FooterRow.FindControl("imgNewCancel");  
                ImageButton imgNewAdd = (ImageButton)GridViewEmpSkill.FooterRow.FindControl("imgNewAdd");  
                imgNewInsert.Visible = false;  
                imgNewCancel.Visible = false;  
                imgNewAdd.Visible = true;  
                TextBox txtNewSkill = (TextBox)GridViewEmpSkill.FooterRow.FindControl("txtNewEmpSkill");  
                txtNewEmpSkill.Visible = true;  
                txtNewEmpSkill.Focus();  
                SkillClsobj.Skill = Convert.ToString(((TextBox)GridViewEmpSkill.FooterRow.FindControl("txtNewSkill")).Text);  
                try  
                {  
                     if (SkillClsobj.Skill == "")  
                     {  
                          msgEmpSkill.Text = "Pleae Enter !";  
                     }  
                     else  
                     {  
                          int intResult= SkillBLLobj.InsertEmpSkill(SkillClsobj);  
                          if (intResult> 0)  
                          {  
                               msgEmpSkill.Text = "Record Added !";  
                               GridViewEmpSkill.EditIndex = -1;  
                               GridViewSkill();  
                          }  
                     }  
                }  
                catch (Exception ee)  
                {  
                     msgEmpSkill.Text = ee.Message.ToString();  
                }  
                finally  
                {  
                     SkillBLLobj = null;  
                }  
           }  
      }  
      catch(Exception ex)  
      {  
      throw ex;  
      }  
 }  
 protected void GridViewEmpSkill_RowDeleting(object sender, GridViewDeleteEventArgs e)  
 {  
      try  
      {  
           SkillClsobj.SkillId = (int)GridViewEmpSkill.DataKeys[e.RowIndex].Value;  
           int result = SkillBLLobj.DeleteSkill(SkillClsobj);  
           if (result > 0)  
           {  
                msgEmpSkill.Text = "Record Deleted Successfully !";  
                GridViewEmpSkill.EditIndex = -1;  
                GridViewSkill();  
           }  
      }  
      catch (Exception ex)  
      {  
           msgEmpSkill.Text = ex.Message.ToString();  
      }  
 }  
 private void ShowNoResultFound(DataSet ds,GridView gv)  
 {  
      DataTable dt = (DataTable)ds.Tables[0];  
      dt.Rows.Add(dt.NewRow());  
      gv.DataSource = dt;  
      gv.DataBind();  
      int TotalColumns = gv.Rows[0].Cells.Count;  
      gv.Rows[0].Cells.Clear();  
      gv.Rows[0].Cells.Add(new TableCell());  
      gv.Rows[0].Height = 0;  
      gv.Rows[0].Visible = false;  
 }  
 ======================  
 business logic layer  
 public class EmpSkillBLL  
 {  
      EmpSkillDAL SkillDALobj = new EmpSkillDAL();  
      public DataSet GetEmpSkillData()  
      {  
           try  
           {  
                return SkillDALobj.GetEmpSkillData();  
           }  
           catch  
           {  
                throw;  
           }  
      }  
      public int InsertSkill(EmpSkillCls objSkillCls)  
      {  
           try  
           {  
                return SkillDALobj.InsertEmpSkill(objSkillCls);  
           }  
           catch  
           {  
                throw;  
           }  
      }  
      public int UpdateSkill(EmpSkillCls objSkillCls)  
      {  
           try  
           {  
                return SkillDALobj.UpdateEmpSkill(objSkillCls);  
           }  
           catch  
           {  
                throw;  
           }  
      }  
      public int DeleteSkill(EmpSkillCls objSkillCls)  
      {  
           try  
           {  
                return objSkillDAL.DeleteEmpSkill(objSkillCls);  
           }  
           catch  
           {  
                throw;  
           }  
      }  
 }  
 ===========  
 data acess layer  
 public class EmpSkillDAL  
 {  
      SqlConnection conn = new SqlConnection("your connection string");  
      DataSet Ds = new DataSet();  
      public DataSet GetEmpSkillData()  
      {  
           Ds = SqlHelper.ExecuteDataset(conn, "sp_SelectDelete");  
           return Ds;  
      }  
      public int InsertEmpSkill(EmpSkillCls Emp)  
      {  
           try  
           {  
                int Insert = SqlHelper.ExecuteNonQuery(conn, "usp_EmpSkillMasterAddEdit", Emp.SkillId, Emp.Skill);  
                return Insert;  
           }  
           catch  
           {  
                throw;  
           }  
           finally  
           {  
                conn.Close();  
           }  
      }  
      public int UpdateEmpSkill(EmpSkillCls emp)  
      {  
           try  
           {  
                int Result = SqlHelper.ExecuteNonQuery(conn, "usp_EmpSkillMaster_AddEdit", Emp.SkillId, Emp.Skill);  
                return Result;  
           }  
           catch  
           {  
                throw;  
           }  
           finally  
           {  
                conn.Close();  
           }  
      }  
      public int DeleteEmpSkill(EmpSkillCls emp)  
      {  
           try  
           {  
                int result = SqlHelper.ExecuteNonQuery(conn, "usp_EmpSkillMasterSelect", emp.SkillId);  
                return result;  
           }  
           catch  
           {  
                throw;  
           }  
           finally  
           {  
                conn.Close();  
           }  
      }  
 }  

1 comment:

  1. I have been searching for a useful post like this on salesforce course details, it is highly helpful for me and I have a great experience with this Salesforce Training who are providing certification and job assistance. Salesforce project based training in Gurgaon 

    ReplyDelete