Help Needed
-
Hi, How to display images in the datagrid column i am using Asp.net using c#. I am looking code sample or article. Help will be greatly appriciated. Thanks
Hi there. Try looking up
TemplateColumn
in the .Net Documentation. You could use a simple <img> tag in a <TemplateColumn> to display images. If the image filename is coming from your data source, then look at the .Net documentation under "data binding expressions". You'll probably end up with something like this (assuming "imgFileName" is the field name for your image files in your datasource):asp:TemplateColumn
<ItemTemplate>
<img src='<%# DataBinder.Eval(Container.DataItem, "imgFileName") %>' />
</ItemTemplate>
</asp:TemplateColumn> -
Hi there. Try looking up
TemplateColumn
in the .Net Documentation. You could use a simple <img> tag in a <TemplateColumn> to display images. If the image filename is coming from your data source, then look at the .Net documentation under "data binding expressions". You'll probably end up with something like this (assuming "imgFileName" is the field name for your image files in your datasource):asp:TemplateColumn
<ItemTemplate>
<img src='<%# DataBinder.Eval(Container.DataItem, "imgFileName") %>' />
</ItemTemplate>
</asp:TemplateColumn>I have used the following code. but still i couldnt diaplay the image, it shows the text System.Byte[] in that column. here is the code try { string Category = Request.QueryString["UserID"]; SqlConnection myConn = new SqlConnection("server=localhost;uid=sa;pwd=;database=BzzzNet"); SqlCommand myCommand = new SqlCommand("perustorproc", myConn); myCommand.CommandType = CommandType.StoredProcedure; myConn.Open(); DG_Persons.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection); DG_Persons.DataBind(); } catch (SqlException ex) { throw new Exception(ex.ToString()); } finally { } --------------- can any one help me to solve this problem. thanks
-
I have used the following code. but still i couldnt diaplay the image, it shows the text System.Byte[] in that column. here is the code try { string Category = Request.QueryString["UserID"]; SqlConnection myConn = new SqlConnection("server=localhost;uid=sa;pwd=;database=BzzzNet"); SqlCommand myCommand = new SqlCommand("perustorproc", myConn); myCommand.CommandType = CommandType.StoredProcedure; myConn.Open(); DG_Persons.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection); DG_Persons.DataBind(); } catch (SqlException ex) { throw new Exception(ex.ToString()); } finally { } --------------- can any one help me to solve this problem. thanks
Hi there. Okay, it looks like you're working with an image stored directly in your database, yes? Remember that when a web server wishes to instruct a browser to display an image, it does so by sending the browser an <img> tag with the
src
attribute set to the image source. There is no way in standard html for a server to send a Byte[] array to a browser and expect the browser to render it as an image. Typically thesrc
attribute of the <img> tag is set to a file on the server, but it doesn't have to be. You could instead set thesrc
attribute to an .aspx page which is coded to process a request for an image stored in a database, and responds with the image's binary byte stream. Such a tag then would look something like this:<img src="myImageRetriever.aspx?imageId=xxx" />
If you google for stuff like "display image from database asp.net" you will find several exmaples of this sort of thing. Here is one CodeProject Article[^] that discusses the technique. Now, with that being said, displaying images this way is more performance-intensive for your server than say just serving up static image files. You may wish to reconsider storing complete images as binary objects in your database. You may wish instead to store a filename in the database, and render that as the
src
attribute in your <TemplateColumn>. -
I have used the following code. but still i couldnt diaplay the image, it shows the text System.Byte[] in that column. here is the code try { string Category = Request.QueryString["UserID"]; SqlConnection myConn = new SqlConnection("server=localhost;uid=sa;pwd=;database=BzzzNet"); SqlCommand myCommand = new SqlCommand("perustorproc", myConn); myCommand.CommandType = CommandType.StoredProcedure; myConn.Open(); DG_Persons.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection); DG_Persons.DataBind(); } catch (SqlException ex) { throw new Exception(ex.ToString()); } finally { } --------------- can any one help me to solve this problem. thanks
Here's another CodeProject article[^] that shows retrieving images from a database.