How to Display Binary Image in Gridview from Database in Asp .Net c#

As you know, the GridView command is a multifunctional command for displaying and editing data in web applications. This is a very popular control in ASP.Net for data manipulation. In this article we will learn how to display binary images in Gridview.

I contacted Google and found a lot of possibilities, but many developers suggested using a processor or converting an image to Base64 Image. So I decided to write both approaches in this article, and you can also download the source code at the end of the post.

So we take the file upload and the button in the Web and GridView forms to display the images. Click on Upload, we upload the image files to the database in binary format, then receive and display the uploaded images in ASP.Net GridView.

Display of GridView images (binary image) from SQL server by converting the image to Base64 imageData

For this purpose, I created a student table in an SQL database, in which information from our web application is stored.

Writing space:

CREATE TABLE [dbo].Students](
[Id] [int] IDENTITY(1,1) NIL,
[name] [nvarchar](150) NIL,
[city] [nvarchar](500) NIL,
[profile image] [varbinary](max) NIL,
[image name] [nvarchar](150) NIL,
CONSTRINT [PK_Students_1] PRIMARY KEY CLASSIFIED
(
[Id] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) TO [PRIMARY]
) TO [PRIMARY] TEXT IMAGE_ON [PRIMARY].

So let’s create an empty website and add a web form.

<%@ Language of page=C# AutoEventWireup=true CodeFile=GridView.aspx.cs Inherited=GridView %>

 

Top of Form

Add students

 

 

 

 

 

Bottom of Form

 

Code for

using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.Data.SqlClient;using System.IO;using System.Linq;using System.Text;using System.Web;using System.Web.UI;using System.Web.WebControls ;

public GridView subclass : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(Data Source=ADEQUATE-ASHOK\SQLEXPRESS01;Initial Catalog=DemoDataBase;User ID=adk;[email protected]);
secure void Page_Load(object sender, EventArgs e)
{
if(!)IsPostBack)
{
BindDatagridview() ;
}

}

null and void protected Button1_Click(object sender, EventArgs e){if (FileUpload1.HasFile){{{{string name = txtname.Text;string city = txtcity.Text;string FileName = path.GetFileName(FileUpload1.PostedFile.FileName);Bytes[] Byte;Usage (BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream).{Bytes = br.ReadBytes(FileUpload1.PostedFile).ContentLength);}String Request = insert into Student values (@Name,@City,@ProfileImage,@ImageName);Usage (SqlCommand cmd = new SqlCommand(Request)).{cmd.Connection = con;cmd.Parameters.AddWithValue(@Name, Name);cmd.Parameters.AddWithValue(@City, City);cmd.Parameters.AddWithValue(@ProfilePicture, Bytes);cmd.Parameters.AddWithValue(@Picture Name, FileUpload1.Filename);con.Open();int i = cmd.ExecuteNonQuery();con.Close();if (i > 0){BindDatagridview();}.further{BindDatagridview();}}}}.

}
}
}

public invalid BindDatagridview()
{
SqlCommand cmd = new SqlCommand(choose * from Students, con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new record();
adp.Fill(ds);
as (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}.
}

GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRowType)
{
DataRowView dr = (DataRowView)e.Row.DataItem;
//BLink an image to the base64 string
imageUrl = data:image/jpg;base64, + Convert.ToBase64String((byte[])dr[profile image]);
(e.Row.FindControl(Imagepath) as image).ImageUrl = imageUrl;
}.
}
}.

As you can see in the code above in the GridView1_RowDataBound event, we convert the image byte [] to Base64 to display the image.

Download source code

How do I display binary images with a raster administrator?

We can also use the manager to display images in grid view mode. Gridview ItemTemplate specifies the image control ImageUrl as src=~/PictureHandler.ashx? Id= + Id, where PictureHandler.ashx is your handler name, which returns MemoryStream((byte[])img) and which we can display in our application;

Suspension Code:

<%@ Page Language=C# AutoEventWireup=true CodeFile=GridView.aspx.cs Inherits=GridView %>

 

Top of Form

Add students

 

 

 

 

 

Bottom of Form

 

You can see the code in the above example of the TemplateField grid on the page where we show the images using the processing file PictureHandler.ashx

What is an HTTP : Every request addressed to our web application is processed by an HTTP manager. The HTTP manager is the most important component in the processing of ASP.NET requests and responses.

To add HTTPHandler to our project file

  • Right click on your project Add a new element > select a manager

Process code:

<%@Gandler’s Web Language=C# Class=Gandler’s Figure %>.

using System;
using System.Web;
using System.Data.SqlClient;
Open Class PictureHandler : IHttpHandler
.

public void ProcessRequest(HttpContext context)
{
string Id = context.Request.QueryString [Id];
SqlConnection con = new SqlConnection(Data source=ADEQUATE-ASHOK\SQLEXPRESS01;Initial Catalog=DemoDataBase;User-ID=adk;[email protected]);
con.Open();
SqlCommand cmd = new SqlCommand (select ProfilePicture of students where Id= + Id, con);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
context.BinaryWrite((byte[])dr[0]);
con.Close();
context.Response.End();
}.

the public sphere is reusable
{
gets
{
returns false;
}
}

}

Arrow code by code:

using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.Data.SqlClient;using System.IO;using System.Linq;using System.Text;using System.Web;using System.Web.UI;using System.Web.WebControls ;

public GridView subclass : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(Data Source=ADEQUATE-ASHOK\SQLEXPRESS01;Initial Catalog=DemoDataBase;User ID=adk;[email protected]);
secure void Page_Load(object sender, EventArgs e)
{
if(!)IsPostBack)
{
BindDatagridview() ;
}

}

null and void protected Button1_Click(object sender, EventArgs e){if (FileUpload1.HasFile){{{{string name = txtname.Text;string city = txtcity.Text;string FileName = path.GetFileName(FileUpload1.PostedFile.FileName);Bytes[] Byte;Usage (BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream).{Bytes = br.ReadBytes(FileUpload1.PostedFile).ContentLength);}String Request = insert into Student values (@Name,@City,@ProfileImage,@ImageName);Usage (SqlCommand cmd = new SqlCommand(Request)).{cmd.Connection = con;cmd.Parameters.AddWithValue(@Name, Name);cmd.Parameters.AddWithValue(@City, City);cmd.Parameters.AddWithValue(@ProfilePicture, Bytes);cmd.Parameters.AddWithValue(@Picture Name, FileUpload1.Filename);con.Open();int i = cmd.ExecuteNonQuery();con.Close();if (i > 0){BindDatagridview();}.further{BindDatagridview();}}}}.

}
}
}

public invalid BindDatagridview()
{
SqlCommand cmd = new SqlCommand(choose * from Students, con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new record();
adp.Fill(ds);
as (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}.
}
}.

Lower boat source code

Related Tags:

couchtuner2,1mycouchtuner,couchtuner reddit,couchtuner proxy,tvmuse not working,rainierland,vumoo,solarmovies,couchtuner alternatives reddit,watch episode,thewatchseries,couch tuner movie,new couchtuner,real couchtuner,couchtuner unblocked,free couchtuner,couchtuner alternatives,couchtuner site