.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Retrieve Image from Sql & Display in Gird

Posted By: shahin     Posted Date: January 06, 2010    Points:2   Category :ASP.Net
Hi Friends,

How to retrieve the image which is stored in binary format in an SQL (image data type) Table into the Datagrid in Web Application.

Can any one tell me how to do this? (C#)

Thanks & Regards
Shahin

Responses
Author: Lalij Mer             
Posted Date: January 06, 2010     Points: 5   

Populating the GridView Control:

The next step is to populate the GridView control with data as well as images. Take a look at the code below which is used to populate the GridView.

private void BindData()

{

SqlConnection myConnection = new SqlConnection(ConnectionString);

SqlDataAdapter ad = new SqlDataAdapter("SELECT UserID, FirstName, LastName,Url FROM Users", myConnection);

DataSet ds = new DataSet();

ad.Fill(ds);

GridView1.DataSource = ds;

GridView1.DataBind();

}

As, you can see the above code is pretty straight forward and you might have done this thousand of times. Now, let's see how we can display the images into the GridView control.

Display Images into the GridView Control:

The first thing you need to do is to add a template column in the GridView control. Once, you have added the template column simply add an Image server control inside the template column. The HTML code of the GridView will look something like the following code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4">

<Columns>

<asp:BoundField DataField="UserID" HeaderText="UserID" />

<asp:BoundField DataField="FirstName" HeaderText="First Name" />

<asp:TemplateField HeaderText="Image">

<ItemTemplate>

<asp:Image ID="Image1" ImageUrl='<%# (string) FormatImageUrl( (string) Eval("Url")) %>' runat="server" />

</ItemTemplate>

</asp:TemplateField>

</Columns>

<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />

<RowStyle BackColor="White" ForeColor="#330099" />

<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />

<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />

<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />

</asp:GridView>



I have made the Image tag bold in the above code so you will easily identify it. Next thing we need to see is the purpose of the FormatImageUrl method which is used to assign the correct url to the ImageUrl property of the Image control.

protected string FormatImageUrl(string url)

{

if (url != null && url.Length > 0)

return ("~/" + url);

else return null;

}

The purpose of "~/" is to map the url relative to the root. This means that the Image control will look for the ImageUrl starting from the root of the website.

more details
---------
http://www.codeproject.com/KB/aspnet/Thumbnail_Images.aspx
http://www.dotnetspider.com/resources/21633-Displaying-Images-GridView.aspx

Please Check my answer if you help....
Thank You...

Lalit.
Author: Lalij Mer             
Posted Date: January 06, 2010     Points: 5   

To begin with this article, create new application in ASP.Net 2.0. Add a GridView control and bind the Image control using ImageUrl attribute with database. The code for the .aspx page is as follows:


<asp:GridView ID="GridView1" runat="server">

<Columns>

<asp:TemplateField HeaderText="Ticket Reserved">

<ItemTemplate>

<asp:Image ID="imgButton" runat="server" ImageUrl='<%# GetImage(Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "TicketReserved"))) %>' ></asp:Image>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>



Now on .cs page add the following property:

public string GetImage(bool allowed)

{

if (allowed)

return "~/Images/image1.gif";

else

return "~/Images/image2.gif";

}

Now on the page load bind the GridView control with database:


protected void Page_Load(object sender, EventArgs e)

{

SqlConnection objConn = new SqlConnection("Server=SERVER-981CB1DBE;Persist Security Info=False;User ID=userid;Password=password;Initial Catalog=TempDatabase");
objConn.Open();

SqlCommand objCmd = new SqlCommand("Select TicketReserverd From Ticket", objConn);

objCmd.CommandType = CommandType.Text;

objCmd.ExecuteNonQuery();

objConn.Close();


}


Please Check my answer if you help....
Thank You...

Lalit.
Author: Ashutosh Jha             
Posted Date: December 25, 2013     Points: 20   

Hi ,

Please follow the below link to get images in data grid which is saved in database in binary format.

http://www.aspdotnet-suresh.com/2011/01/how-to-insert-images-into-database-and.html

Regards,
Ashutosh Jha
ashutosh741@gmail.com

http://www.tricksroad.com


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend