Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I have a SQL2008 table containing varbinary(max) data the table structure looks like this:

SQL
[ID] [nvarchar](50) NOT NULL,
   [Name] [nvarchar](100) NOT NULL,
   [Type] [nvarchar](100) NOT NULL,
   [Size] [nchar](10) NULL,
   [Date_Uploaded] [date] NULL,
   [Time_Uploaded] [nchar](10) NULL,
   [Data] [varbinary](max) NULL,
CONSTRAINT [PK_Files] PRIMARY KEY CLUSTERED
   [ID] ASC,
   [Name] ASC


I have my upload tool working well, but I am unable to bring the data back to be displayed in a grid view.
This is the current code I have to do that.
VB
<asp:GridView ID="grdImageData" runat="server" AutoGenerateColumns="False" DataKeyNames="Data" DataSourceID="DS_Images_View" Width="600px" CellPadding="4" ForeColor="Black" GridLines="Vertical" 
            BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px">
                    <FooterStyle BackColor="#CCCC99" />
                    <RowStyle BackColor="#F7F7DE" />
                    <Columns>
                    <asp:TemplateField HeaderText="Icon">
                        <ItemTemplate>
                        <asp:Image ID="Image" runat="server" ImageUrl='<%# Eval("Data") %>' AlternateText='<%# Eval("Type") %>' Width= "300" ImageAlign="Left"/>
                        </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
                    <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                    <AlternatingRowStyle BackColor="White" />
                </asp:GridView>



Could anyone please help me further on this?
Posted
Updated 19-Jan-12 2:35am
v2

C#
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;

public class Handler : IHttpHandler {
 
public void ProcessRequest (HttpContext context) 
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings
 ["ConnectionString"].ConnectionString;

// Create SQL Command 
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Select ImageName,Image from Images" + 
 " where ID =@ID";
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;

SqlParameter ImageID = new SqlParameter
 ("@ID", System.Data.SqlDbType.Int);
ImageID.Value = context.Request.QueryString["ID"];
cmd.Parameters.Add(ImageID);
con.Open();
SqlDataReader dReader = cmd.ExecuteReader();
dReader.Read();
context.Response.BinaryWrite((byte[])dReader["Image"]);
dReader.Close();
con.Close();
}
 
Share this answer
 
v3
Comments
Zac Newman 19-Jan-12 16:56pm    
Heya Rahul,

I am guessing you mean to make a new Generic Handler and slap that code into it, rejigging it a little to reference both of the PK's in the database.
If I do that and it would be simple enough to do what then?

<asp:Image ID="Image" runat="server" ImageUrl='<%# Eval GenHandler.ashx("Data") %>'
AlternateText='<%# Eval("Type") %>' Width= "300" ImageAlign="Left"/>

Something like that?

Regards,

Caz
Hi,
Get the best examples with source code:
CodeProject.com[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900