Asp.Net Image Upload in 3-Tier Architecture and store in sql database

Introduction : 

In  3-Tier Architecture for displaying image we have using here, asp.net gridview control and sql database connection,now you can follow the below step and display image using 3- tier architecture in asp.net c#.

step 1 : Open Visual Studio and Create new web project

Step 2 : Create Database

asp.net 3-Tier Architecture

 

Step 3 : Now create class data

public class DAL
{

public
int id { get; set; }
public string photo { get; set; }}

Step 4 : Create Class BAL.cs This class called as Business layer.

public class BAL
{
SqlConnection con = new SqlConnection(@”Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True“);
public void insert_data(DAL da)
{
con.Open();
SqlCommand cmd = new SqlCommand(“insert into user_master values(‘” + da.photo + “‘)”, con);
cmd.ExecuteNonQuery();
con.Close();
}
public DataTable Select_data(DAL da)
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(“select * from user_master“, con);
sda.Fill(dt);
return dt;
}
}

Step 5 : Now, add webform and give name web.aspx and add Gridview, in Gridview source code we can add following source

<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False”>
<Columns>
<asp:TemplateField HeaderText=”photo”>
<ItemTemplate>
<asp:image ID=”Image1″ runat=”server” Height=”70px” Width=”70px”
ImageUrl ='<%#Eval(“photo”)%>’></asp:image>
</ItemTemplate>

<EditItemTemplate>
<asp:image ID=”Image2″ runat=”server”  Height=”70″
Width=”70″ ImageUrl ='<%#Eval(“photo”)%>’></asp:image>
</EditItemTemplate>

</asp:TemplateField>

</Columns>
</asp:GridView>

 

Step 6:

Now you can add code Behind following here file_id is used as browse id using this id can select image from the folder. and we can add folder named as photo we can retrieve image from this folder by below code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
namespace _3_tier_Image
{
public partial class Web : System.Web.UI.Page
{
DAL da = new DAL();
BAL ba = new BAL();

protected void Page_Load(object sender, EventArgs e)
{
Grid();
}

public void Grid()
{
DataTable dt = new DataTable();
dt = ba.Select_data(da);
GridView1.DataSource = dt;
GridView1.DataBind();

}

protected void Button1_Click(object sender, EventArgs e)
{

if (file_id.HasFile)
{

string file = Path.GetFileName(file_id.FileName);
file_id.SaveAs(MapPath(“~/photo/”) + file);
da.photo = “~/photo/” + file;
}
ba.insert_data(da);
Response.Write(“Image Inserted………..“);
Grid();
}
}
}

Step 7: Run Project and Select Image from Folder

Asp.Net Image Upload in 3-Tier Architecture and store in sql database

 

SEE MORE