Hi all,

Here i would like to share something about, how to handle images in the data base with a small asp.net application.





First to upload data into the data base we should have a column in the table with a data type of "Image" then only we can upload data into the data base other wise we can upload the data.

So for this create a data base with your own data base name. and create a table "EmployeeDetails" to store employee table.

Follow the below commands to create a data base and table with different data fields in the employee table.

create database Test

use Test

create table Employeedatails (EmpId int identity primary key, EmpName varchar(50), EmpImage image)


Now develop an application to upload an image to you data base using an upload control in asp.net.


Here i am providing you the code to upload an image to our employeedetails table.

In Default.aspx:
<asp:Label ID="lblEmpName" runat="server">Image name:</asp:Label>
&nbsp;&nbsp;
    <asp:TextBox ID="txtEname" runat="server" ValidationGroup="g1"></asp:TextBox>
&nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
        ControlToValidate="txtEname" Display="Dynamic" 
        ErrorMessage="Please Enter  file name" ValidationGroup="g1"></asp:RequiredFieldValidator>
    <br />
    <asp:Label ID="lblImage" runat="server" Text="Upload your image:"></asp:Label>
&nbsp;
    <asp:FileUpload ID="imageUpload" runat="server" style="margin-left: 0px" />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
        ControlToValidate="imageUpload" ErrorMessage="Please select your file" 
        ValidationGroup="g1"></asp:RequiredFieldValidator>
&nbsp;
    <br />
    <asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" 
        Text="Submit" ValidationGroup="g1" />
    <p>
        <asp:Label ID="lblResult" runat="server"></asp:Label>
    </p>

In Default.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
    {       
        conn = new SqlConnection("Data Source=RAJESH\\RAJESH;Initial Catalog=Test;Integrated Security=True");
        
        if (!Page.IsPostBack)
        {
            updateDataTable();
            DisplayingImages(first);
        }
    }

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        SqlCommand check = new SqlCommand("select empid from employeedetails where empname='"+txtEname.Text+"'", conn);
        conn.Open();
        SqlDataReader rdr = check.ExecuteReader();
        if (rdr.Read())
        {
            Response.Write("<script>alert('The image is already exist, Please choose another name')</script>");
            conn.Close();
            txtEname.Text = "";
            txtEname.Focus();
        }
        else
        {
        try
        {
            FileUpload img = (FileUpload)imageUpload;
            Byte[] imgbyte = null;
            if (img.HasFile && img.PostedFile != null)
            {
                string fname = imageUpload.FileName;
                string[] fileName = fname.Split('.');
                if (fileName[fileName.Length - 1] == "jpg" || fileName[fileName.Length - 1] == "gif" || fileName[fileName.Length - 1] == "png" || fileName[fileName.Length - 1] == "bmp" || fileName[fileName.Length - 1] == "ico")
                {
                    HttpPostedFile file = img.PostedFile;
                    imgbyte = new Byte[file.ContentLength];
                    file.InputStream.Read(imgbyte, 0, file.ContentLength);

                    SqlCommand MyInsertCmd = new SqlCommand("Insert into EmployeeDetails(EmpName,EmpImage) values(@enm,@eimg) ", conn);
                    MyInsertCmd.Parameters.AddWithValue("@enm", txtEname.Text);
                    MyInsertCmd.Parameters.AddWithValue("@eimg", imgbyte);
                    conn.Open();
                    int i = MyInsertCmd.ExecuteNonQuery();
                    if (i > 0)
                        lblResult.Text = "File uploaded";
                    conn.Close();
                    txtEname.Text = "";
                    updateDataTable();
                    lnkbtnLast_Click(sender, e);
                }
                else
                {                    
                    lblResult.Text = "Please choose jpg or gif or png or ico files only";
                }
            }
        }
           
        
        catch
        {
            lblResult.Text="There was an error";
            conn.Close();
        }
        }
    }
Here I am Inserting Employee details like name and the Image of the person in to the data base. So that we will have an Id for each employee automatically.

Here I used Generic Handler to Convert the byte code and display the image in the web page.


Here I am providing you the full source Code. To download the source code click on the link below.

Source Code: Download From Here.

Share it

0 comments:

Post a Comment