Add Details of an Employee With Image and Display it Into a Gridview

In this article, we will know how to add details of an employee with an image and display it into a GridView. Here an image will be stored in the application folder. We can also edit, update, delete, and cancel the details of the employee in the GridView.
 
Table Creation
 
sattu1.gif
 
Stored procedure
  1. CREATE PROCEDURE insert_employee  
  2.     (  
  3.         @name varchar(50),  
  4.         @address varchar(50),  
  5.         @image varchar(50)  
  6.     )  
  7. AS  
  8. Insert into employee values(@name, @address, @image)  
  9.   
  10. CREATE PROCEDURE update_employee  
  11.     (  
  12.         @id int,  
  13.         @name varchar(50),  
  14.         @address varchar(50)  
  15.     )  
  16. AS  
  17. Update employee set name = @name, address = @address where id = @id  
  18.   
  19. CREATE PROCEDURE delete_employee  
  20.     (@id int)  
  21. AS  
  22. Delete from employee where id = @id 
Default.aspx Code
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Add_update_delete_gridview._Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head id="Head1" runat="server">  
  5.     <title>Untitled Page</title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.         <table>  
  11.             <tr>  
  12.                 <td>  
  13.                     Name  
  14.                 </td>  
  15.                 <td>  
  16.                     <asp:textbox id="txt_name" runat="server">  
  17.                     </asp:textbox>  
  18.                     <asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" controltovalidate="txt_name"  
  19.                         errormessage="Please enter the name">  
  20.                     </asp:requiredfieldvalidator>  
  21.                 </td>  
  22.             </tr>  
  23.             <tr>  
  24.                 <td>  
  25.                     Address  
  26.                 </td>  
  27.                 <td>  
  28.                     <asp:textbox id="txt_address" runat="server">  
  29.                     </asp:textbox>  
  30.                     <asp:requiredfieldvalidator id="RequiredFieldValidator2" runat="server" controltovalidate="txt_address"  
  31.                         errormessage="Please enter the address">  
  32.                     </asp:requiredfieldvalidator>  
  33.                 </td>  
  34.             </tr>  
  35.             <tr>  
  36.                 <td>  
  37.                     Image  
  38.                 </td>  
  39.                 <td>  
  40.                     <asp:fileupload id="FileUpload1" runat="server" />  
  41.                     <asp:requiredfieldvalidator id="RequiredFieldValidator3" runat="server" controltovalidate="FileUpload1"  
  42.                         errormessage="Please browse the image">  
  43.                     </asp:requiredfieldvalidator>  
  44.                 </td>  
  45.             </tr>  
  46.             <tr>  
  47.                 <td>  
  48.                 </td>  
  49.                 <td>  
  50.                     <asp:button id="btn_insert" runat="server" onclick="btn_insert_Click" text="Insert" />  
  51.                 </td>  
  52.             </tr>  
  53.             <tr>  
  54.                 <td colspan="2">  
  55.                     <asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" onrowcancelingedit="GridView1_RowCancelingEdit"  
  56.                         onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"  
  57.                         onselectedindexchanging="GridView1_SelectedIndexChanging" backcolor="#CC3300"  
  58.                         forecolor="Black">  
  59.                         <columns>  
  60.                         <asp:TemplateField HeaderText="Name">  
  61.                         <EditItemTemplate>  
  62.                         <asp:TextBox ID="txt_name" runat="server"  
  63.                         Text='<%# Eval("name") %>'></asp:TextBox>  
  64.                         <asp:Label ID="Label4" runat="server" Text='<%# Eval("id") %>' Visible="False"></asp:Label>  
  65.                         </EditItemTemplate>  
  66.                         <ItemTemplate>  
  67.                         <asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label>  
  68.                         <asp:Label ID="Label2" runat="server" Text='<%# Eval("id") %>' Visible="False"></asp:Label>  
  69.                         </ItemTemplate>  
  70.                         </asp:TemplateField>  
  71.                         <asp:TemplateField HeaderText="Address">  
  72.                         <EditItemTemplate>  
  73.                         <asp:TextBox ID="txt_address" runat="server" Text='<%# Eval("address") %>'></asp:TextBox>  
  74.                         </EditItemTemplate>  
  75.                         <ItemTemplate>  
  76.                         <asp:Label ID="Label3" runat="server" Text='<%# Eval("address") %>'></asp:Label>  
  77.                         </ItemTemplate>  
  78.                         </asp:TemplateField>  
  79.                         <asp:TemplateField HeaderText="Image">  
  80.                         <ItemTemplate>  
  81.                         <img alt ="" src ='images/<%#Eval("image") %>' height="50px" width="50px"/>  
  82.                         </ItemTemplate>  
  83.                         </asp:TemplateField>  
  84.                         <asp:TemplateField HeaderText="Edit">  
  85.                         <EditItemTemplate>  
  86.                         <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False"  
  87.                         CommandName="Update">Update</asp:LinkButton>  
  88.                         <asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="False"  
  89.                         CommandName="Cancel">Cancel</asp:LinkButton>  
  90.                         </EditItemTemplate>  
  91.                         <ItemTemplate>  
  92.                         <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"  
  93.                         CommandName="Edit">Edit</asp:LinkButton>  
  94.                         </ItemTemplate>  
  95.                         </asp:TemplateField>  
  96.                         <asp:TemplateField HeaderText="Delete">  
  97.                         <ItemTemplate>  
  98.                         <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"  
  99.                         CommandName="Delete"  
  100.                         onclientclick="return confirm('Are you sure you want to delete this record')">Delete</asp:LinkButton>  
  101.                         </ItemTemplate>  
  102.                         </asp:TemplateField>  
  103.                         </columns>  
  104.                         <headerstyle backcolor="#FF9933" />  
  105.                         <alternatingrowstyle backcolor="#FFCC00" />  
  106.                     </asp:gridview>  
  107.                 </td>  
  108.             </tr>  
  109.         </table>  
  110.     </div>  
  111.     </form>  
  112. </body>  
  113. </html> 
Default.aspx.cs Code:
  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13. using System.Data.SqlClient;  
  14. using System.IO;  
  15.   
  16. namespace Add_update_delete_gridview {  
  17.     public partial class _Default: System.Web.UI.Page {  
  18.         string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;  
  19.         string s1;  
  20.         string path;  
  21.         SqlConnection cnn = new SqlConnection();  
  22.         SqlCommand com = new SqlCommand();  
  23.         SqlDataAdapter sqlda;  
  24.         DataTable dt;  
  25.         int id;  
  26.         string name;  
  27.         string address;  
  28.         string image;  
  29.         protected void Page_Load(object sender, EventArgs e) {  
  30.             if (!IsPostBack) {  
  31.                 bindgrid();  
  32.             }  
  33.         }  
  34.         protected void btn_insert_Click(object sender, EventArgs e) {  
  35.             if (FileUpload1.PostedFile.ContentLength > 0) {  
  36.                 s1 = Path.GetFileName(FileUpload1.FileName);  
  37.                 path = Server.MapPath("images") + "/" + s1;  
  38.                 FileUpload1.SaveAs(path);  
  39.             }  
  40.             SqlConnection con = new SqlConnection(strConnString);  
  41.             con.Open();  
  42.             SqlCommand com = new SqlCommand("insert_employee", con);  
  43.             com.CommandType = CommandType.StoredProcedure;  
  44.             com.Connection = con;  
  45.             com.Parameters.AddWithValue("@name", txt_name.Text);  
  46.             com.Parameters.AddWithValue("@address", txt_address.Text);  
  47.             com.Parameters.AddWithValue("@image", s1);  
  48.             com.ExecuteNonQuery();  
  49.             com.Dispose();  
  50.             bindgrid();  
  51.             con.Close();  
  52.             clear();  
  53.         }  
  54.         private void clear() {  
  55.             txt_name.Text = "";  
  56.             txt_address.Text = "";  
  57.         }  
  58.         private void bindgrid() {  
  59.             SqlConnection con = new SqlConnection(strConnString);  
  60.             con.Open();  
  61.             sqlda = new SqlDataAdapter("SELECT * FROM employee ", con);  
  62.             dt = new DataTable();  
  63.             sqlda.Fill(dt);  
  64.             sqlda.Dispose();  
  65.             GridView1.DataSource = dt;  
  66.             GridView1.DataBind();  
  67.             con.Close();  
  68.         }  
  69.         protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) {  
  70.             SqlConnection con = new SqlConnection(strConnString);  
  71.             con.Open();  
  72.             try {  
  73.                 id = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("label2"))).Text);  
  74.                 SqlCommand com = new SqlCommand("delete_employee", con);  
  75.                 com.CommandType = CommandType.StoredProcedure;  
  76.                 com.Connection = con;  
  77.                 com.Parameters.Add("@id", SqlDbType.Int).Value = id;  
  78.                 SqlDataAdapter sqlda = new SqlDataAdapter("select * from employee where id=@id", con);  
  79.                 sqlda.SelectCommand.Parameters.Add("@id", SqlDbType.Int).Value = id;  
  80.                 DataSet ds = new DataSet();  
  81.                 sqlda.Fill(ds);  
  82.                 try {  
  83.                     image = Convert.ToString(ds.Tables[0].Rows[0]["image"]);  
  84.                     File.Delete(Server.MapPath("images") + "\\" + image);  
  85.                 } catch (Exception) {}  
  86.                 com.ExecuteNonQuery();  
  87.                 com.Dispose();  
  88.                 bindgrid();  
  89.             } catch (Exception) {}  
  90.         }  
  91.         protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {  
  92.             GridView1.EditIndex = -1;  
  93.             bindgrid();  
  94.         }  
  95.         protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) {  
  96.             GridView1.EditIndex = e.NewEditIndex;  
  97.             bindgrid();  
  98.         }  
  99.         protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {  
  100.             SqlConnection con = new SqlConnection(strConnString);  
  101.             con.Open();  
  102.             id = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("label4"))).Text);  
  103.             name = (((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txt_name"))).Text);  
  104.             address = (((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txt_address"))).Text);  
  105.             SqlCommand com = new SqlCommand("update_employee", con);  
  106.             com.CommandType = CommandType.StoredProcedure;  
  107.             com.Connection = con;  
  108.             com.Parameters.Add("@id", SqlDbType.Int).Value = id;  
  109.             com.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = name;  
  110.             com.Parameters.Add("@address", SqlDbType.VarChar, 50).Value = address;  
  111.             com.ExecuteNonQuery();  
  112.             com.Dispose();  
  113.             con.Close();  
  114.             GridView1.EditIndex = -1;  
  115.             bindgrid();  
  116.         }  
  117.         protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) {  
  118.             GridView1.PageIndex = e.NewSelectedIndex;  
  119.             bindgrid();  
  120.         }  
  121.     }  
Output: Here we will see the output which is given below.
 
sattu2.gif