Vyoms OneStopTesting.com - Testing EBooks, Tutorials, Articles, Jobs, Training Institutes etc.
OneStopGate.com - Gate EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopMBA.com - MBA EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopIAS.com - IAS EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopSAP.com - SAP EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopGRE.com - of GRE EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
Bookmark and Share Rss Feeds

Add Details of an Employee With Image and Display it Into a Gridview | Articles | Recent Articles | News Article | Interesting Articles | Technology Articles | Articles On Education | Articles On Corporate | Company Articles | College Articles | Articles on Recession
Sponsored Ads
Hot Jobs
Fresher Jobs
Experienced Jobs
Government Jobs
Walkin Jobs
Placement Section
Company Profiles
Interview Questions
Placement Papers
Resources @ VYOMS
Companies In India
Consultants In India
Colleges In India
Exams In India
Latest Results
Notifications In India
Call Centers In India
Training Institutes In India
Job Communities In India
Courses In India
Jobs by Keyskills
Jobs by Functional Areas
Learn @ VYOMS
GATE Preparation
GRE Preparation
GMAT Preparation
IAS Preparation
SAP Preparation
Testing Preparation
MBA Preparation
News @ VYOMS
Freshers News
Job Articles
Latest News
India News Network
Interview Ebook
Get 30,000+ Interview Questions & Answers in an eBook.
Interview Success Kit - Get Success in Job Interviews
  • 30,000+ Interview Questions
  • Most Questions Answered
  • 5 FREE Bonuses
  • Free Upgrades

VYOMS TOP EMPLOYERS

Wipro Technologies
Tata Consultancy Services
Accenture
IBM
Satyam
Genpact
Cognizant Technologies

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

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








Article Posted On Date : Wednesday, March 21, 2012


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

In this article we will know how to add details of an employee with image and display it into a GridView. Here an image will be stored into 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

CREATE PROCEDURE insert_employee
(      
@name varchar(50),
@address varchar(50),
@image varchar(50)
)
AS
Insert into employee values (@name,@address,@image)

CREATE PROCEDURE update_employee
(
@id int,
@name varchar(50),
@address varchar(50)
)
AS
Update employee set name=@name, address=@address where id=@id

CREATE PROCEDURE delete_employee
(@id int)
AS
Delete from employee where id=@id

Default.aspx Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Add_update_delete_gridview._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <table>

            <tr>

                <td>

                    Name

                </td>

                <td>

                    <asp:textbox id="txt_name" runat="server">

                    </asp:textbox>

                    <asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" controltovalidate="txt_name"

                        errormessage="Please enter the name">

                    </asp:requiredfieldvalidator>

                </td>

            </tr>

            <tr>

                <td>

                    Address

                </td>

                <td>

                    <asp:textbox id="txt_address" runat="server">

                    </asp:textbox>

                    <asp:requiredfieldvalidator id="RequiredFieldValidator2" runat="server" controltovalidate="txt_address"

                        errormessage="Please enter the address">

                    </asp:requiredfieldvalidator>

                </td>

            </tr>

            <tr>

                <td>

                    Image

                </td>

                <td>

                    <asp:fileupload id="FileUpload1" runat="server" />

                    <asp:requiredfieldvalidator id="RequiredFieldValidator3" runat="server" controltovalidate="FileUpload1"

                        errormessage="Please browse the image">

                    </asp:requiredfieldvalidator>

                </td>

            </tr>

            <tr>

                <td>

                </td>

                <td>

                    <asp:button id="btn_insert" runat="server" onclick="btn_insert_Click" text="Insert" />

                </td>

            </tr>

            <tr>

                <td colspan="2">

                    <asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" onrowcancelingedit="GridView1_RowCancelingEdit"

                        onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"

                        onselectedindexchanging="GridView1_SelectedIndexChanging" backcolor="#CC3300"

                        forecolor="Black">

                        <columns>

                        <asp:TemplateField HeaderText="Name">

                        <EditItemTemplate>

                        <asp:TextBox ID="txt_name" runat="server"

                        Text='<%# Eval("name") %>'></asp:TextBox>

                        <asp:Label ID="Label4" runat="server" Text='<%# Eval("id") %>' Visible="False"></asp:Label>

                        </EditItemTemplate>

                        <ItemTemplate>

                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>'></asp:Label>

                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("id") %>' Visible="False"></asp:Label>

                        </ItemTemplate>

                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="Address">

                        <EditItemTemplate>

                        <asp:TextBox ID="txt_address" runat="server" Text='<%# Eval("address") %>'></asp:TextBox>

                        </EditItemTemplate>

                        <ItemTemplate>

                        <asp:Label ID="Label3" runat="server" Text='<%# Eval("address") %>'></asp:Label>

                        </ItemTemplate>

                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="Image">

                        <ItemTemplate>

                        <img alt ="" src ='images/<%#Eval("image") %>' height="50px" width="50px"/>

                        </ItemTemplate>

                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="Edit">

                        <EditItemTemplate>

                        <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False"

                        CommandName="Update">Update</asp:LinkButton>

                        <asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="False"

                        CommandName="Cancel">Cancel</asp:LinkButton>

                        </EditItemTemplate>

                        <ItemTemplate>

                        <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"

                        CommandName="Edit">Edit</asp:LinkButton>

                        </ItemTemplate>

                        </asp:TemplateField>

                        <asp:TemplateField HeaderText="Delete">

                        <ItemTemplate>

                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"

                        CommandName="Delete"

                        onclientclick="return confirm('Are you sure you want to delete this record')">Delete</asp:LinkButton>

                        </ItemTemplate>

                        </asp:TemplateField>

                        </columns>

                        <headerstyle backcolor="#FF9933" />

                        <alternatingrowstyle backcolor="#FFCC00" />

                    </asp:gridview>

                </td>

            </tr>

        </table>

    </div>

    </form>

</body>

</html>

Default.aspx.cs Code:

 using System;
 using System.Collections;
 using System.Configuration;
 using System.Data;
 using System.Linq;
 using System.Web;
 using System.Web.Security;
 using System.Web.UI;
 using System.Web.UI.HtmlControls;
 using System.Web.UI.WebControls;
 using System.Web.UI.WebControls.WebParts;
 using System.Xml.Linq;
 using System.Data.SqlClient;
 using System.IO;
 namespace Add_update_delete_gridview
 {
     public partial class _Default : System.Web.UI.Page
     {
         string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
         string s1;
         string path;
         SqlConnection cnn = new SqlConnection();
         SqlCommand com = new SqlCommand();
         SqlDataAdapter sqlda;
         DataTable dt;
         int id;
         string name;
         string address;
         string image;
         protected void Page_Load(object sender, EventArgs e)
         {
             if (!IsPostBack)
             {
                 bindgrid();
             }
         }
         protected void btn_insert_Click(object sender, EventArgs e)
         {
             if (FileUpload1.PostedFile.ContentLength > 0)
             {
                 s1 = Path.GetFileName(FileUpload1.FileName);
                 path = Server.MapPath("images") + "/" + s1;
                 FileUpload1.SaveAs(path);
             }
             SqlConnection con = new SqlConnection(strConnString);
             con.Open();
             SqlCommand com = new SqlCommand("insert_employee", con);
             com.CommandType = CommandType.StoredProcedure;
             com.Connection = con;
             com.Parameters.AddWithValue("@name", txt_name.Text);
             com.Parameters.AddWithValue("@address", txt_address.Text);
             com.Parameters.AddWithValue("@image", s1);
             com.ExecuteNonQuery();
             com.Dispose();
             bindgrid();
             con.Close();
             clear();
         }
         private void clear()
         {
             txt_name.Text = "";
             txt_address.Text = "";
         }
         private void bindgrid()
         {
             SqlConnection con = new SqlConnection(strConnString);
             con.Open();
             sqlda = new SqlDataAdapter("SELECT * FROM employee ", con);
             dt = new DataTable();
             sqlda.Fill(dt);
             sqlda.Dispose();
             GridView1.DataSource = dt;
             GridView1.DataBind();
             con.Close();
         }
         protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
         {
             SqlConnection con = new SqlConnection(strConnString);
             con.Open();
             try
             {
                 id = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("label2"))).Text);
                 SqlCommand com = new SqlCommand("delete_employee", con);
                 com.CommandType = CommandType.StoredProcedure;
                 com.Connection = con;
                 com.Parameters.Add("@id", SqlDbType.Int).Value = id;
                 SqlDataAdapter sqlda = new SqlDataAdapter("select * from employee where id=@id", con);
                 sqlda.SelectCommand.Parameters.Add("@id", SqlDbType.Int).Value = id;
                 DataSet ds = new DataSet();
                 sqlda.Fill(ds);
                 try
                 {
                     image = Convert.ToString(ds.Tables[0].Rows[0]["image"]);
                     File.Delete(Server.MapPath("images") + "\" + image);
                 }
                 catch (Exception)
                 {
                 }
                 com.ExecuteNonQuery();
                 com.Dispose();
                 bindgrid();
             }
             catch (Exception)
             {
             }
         }
         protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
         {
             GridView1.EditIndex = -1;
             bindgrid();
         }
         protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
         {
             GridView1.EditIndex = e.NewEditIndex;
             bindgrid();
         }
         protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
         {
             SqlConnection con = new SqlConnection(strConnString);
             con.Open();
             id = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("label4"))).Text);
             name = (((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txt_name"))).Text);
             address = (((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txt_address"))).Text);
             SqlCommand com = new SqlCommand("update_employee", con);
             com.CommandType = CommandType.StoredProcedure;
             com.Connection = con;
             com.Parameters.Add("@id", SqlDbType.Int).Value = id;
             com.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = name;
             com.Parameters.Add("@address", SqlDbType.VarChar, 50).Value = address;
             com.ExecuteNonQuery();
             com.Dispose();
             con.Close();
             GridView1.EditIndex = -1;
             bindgrid();
         }
         protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
         {
             GridView1.PageIndex = e.NewSelectedIndex;
             bindgrid();
         }
     }
 }






Sponsored Ads



Interview Questions
HR Interview Questions
Testing Interview Questions
SAP Interview Questions
Business Intelligence Interview Questions
Call Center Interview Questions

Databases

Clipper Interview Questions
DBA Interview Questions
Firebird Interview Questions
Hierarchical Interview Questions
Informix Interview Questions
Microsoft Access Interview Questions
MS SqlServer Interview Questions
MYSQL Interview Questions
Network Interview Questions
Object Relational Interview Questions
PL/SQL Interview Questions
PostgreSQL Interview Questions
Progress Interview Questions
Relational Interview Questions
SQL Interview Questions
SQL Server Interview Questions
Stored Procedures Interview Questions
Sybase Interview Questions
Teradata Interview Questions

Microsof Technologies

.Net Database Interview Questions
.Net Deployement Interview Questions
ADO.NET Interview Questions
ADO.NET 2.0 Interview Questions
Architecture Interview Questions
ASP Interview Questions
ASP.NET Interview Questions
ASP.NET 2.0 Interview Questions
C# Interview Questions
Csharp Interview Questions
DataGrid Interview Questions
DotNet Interview Questions
Microsoft Basics Interview Questions
Microsoft.NET Interview Questions
Microsoft.NET 2.0 Interview Questions
Share Point Interview Questions
Silverlight Interview Questions
VB.NET Interview Questions
VC++ Interview Questions
Visual Basic Interview Questions

Java / J2EE

Applet Interview Questions
Core Java Interview Questions
Eclipse Interview Questions
EJB Interview Questions
Hibernate Interview Questions
J2ME Interview Questions
J2SE Interview Questions
Java Interview Questions
Java Beans Interview Questions
Java Patterns Interview Questions
Java Security Interview Questions
Java Swing Interview Questions
JBOSS Interview Questions
JDBC Interview Questions
JMS Interview Questions
JSF Interview Questions
JSP Interview Questions
RMI Interview Questions
Servlet Interview Questions
Socket Programming Interview Questions
Springs Interview Questions
Struts Interview Questions
Web Sphere Interview Questions

Programming Languages

C Interview Questions
C++ Interview Questions
CGI Interview Questions
Delphi Interview Questions
Fortran Interview Questions
ILU Interview Questions
LISP Interview Questions
Pascal Interview Questions
Perl Interview Questions
PHP Interview Questions
Ruby Interview Questions
Signature Interview Questions
UML Interview Questions
VBA Interview Questions
Windows Interview Questions
Mainframe Interview Questions


Copyright © 2001-2024 Vyoms.com. All Rights Reserved. Home | About Us | Advertise With Vyoms.com | Jobs | Contact Us | Feedback | Link to Us | Privacy Policy | Terms & Conditions
Placement Papers | Get Your Free Website | IAS Preparation | C++ Interview Questions | C Interview Questions | Report a Bug | Romantic Shayari | CAT 2024

Fresher Jobs | Experienced Jobs | Government Jobs | Walkin Jobs | Company Profiles | Interview Questions | Placement Papers | Companies In India | Consultants In India | Colleges In India | Exams In India | Latest Results | Notifications In India | Call Centers In India | Training Institutes In India | Job Communities In India | Courses In India | Jobs by Keyskills | Jobs by Functional Areas

Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes

Gate Articles | Gate Books | Gate Colleges | Gate Downloads | Gate Faqs | Gate Jobs | Gate News | Gate Sample Papers | Gate Training Institutes

MBA Articles | MBA Books | MBA Case Studies | MBA Business Schools | MBA Current Affairs | MBA Downloads | MBA Events | MBA Notifications | MBA FAQs | MBA Jobs
MBA Job Consultants | MBA News | MBA Results | MBA Courses | MBA Sample Papers | MBA Interview Questions | MBA Training Institutes

GRE Articles | GRE Books | GRE Colleges | GRE Downloads | GRE Events | GRE FAQs | GRE News | GRE Training Institutes | GRE Sample Papers

IAS Articles | IAS Books | IAS Current Affairs | IAS Downloads | IAS Events | IAS FAQs | IAS News | IAS Notifications | IAS UPSC Jobs | IAS Previous Question Papers
IAS Results | IAS Sample Papers | IAS Interview Questions | IAS Training Institutes | IAS Toppers Interview

SAP Articles | SAP Books | SAP Certifications | SAP Companies | SAP Study Materials | SAP Events | SAP FAQs | SAP Jobs | SAP Job Consultants
SAP Links | SAP News | SAP Sample Papers | SAP Interview Questions | SAP Training Institutes |


Copyright ©2001-2024 Vyoms.com, All Rights Reserved.
Disclaimer: VYOMS.com has taken all reasonable steps to ensure that information on this site is authentic. Applicants are advised to research bonafides of advertisers independently. VYOMS.com shall not have any responsibility in this regard.