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#. Asp.Net Image Upload using 3 Tier Architecture with sql server database, file upload using n tier architecture
step 1 : Open Visual Studio and Create new web project
Step 2 : Create Database
Step 3 : Now create class data
{
public int id { get; set; }
public string photo { get; set; }}
Step 4 : Create Class BAL.cs This class called as Business layer.
{
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
SEE MORE
- Auto Refresh Partial View in ASP.NET MVC
- What is ASP.NET Core
- Difference between TempData keep() And Peek() in Asp.Net MVC
- Difference between viewbag,viewdata and tempdata in asp.net mvc
- ASP.NET MVC With AngularJS
- Retrieving Data Using Form Collection and Inserting Into ASP.Net MVC
- MVC CRUD Operations Using Entity Framework
- Search Functionality in ASP.NET MVC
- How to create a User Registration page using asp.net mvc
- Store Multiple Checkbox state from cookie using Jquery
- Cascading Dropdownlist using Ajax in Asp.Net Mvc with city state country
- Insert, Update, Delete In GridView Using ASP.Net C#
- Binding Dropdownlist With Database In Asp.Net MVC
- Search and Filter data in Gridview using Asp.net MVC
- Select Insert, Update And Delete With ASP.NET MVC
- Display Data in GridView Using Asp.net MVC
- Validation in ASP.NET MVC Razor view
- CRUD Operation Using 3-Tier Architecture In ASP.NET
- How to get Connection String from Web.Config in Asp.Net C#
- Login page using 3-Tier Architecture in ASP.Net
- Asp.Net Image Upload in 3-Tier Architecture and store in sql database
17 Comments