DB Images to datagrid - how

R

Raghu Raman

Hi,

I want to load the images from my sqlserver200 db to the datagrid.I've
seen a code from odetocode.com and it recreated to my wish.

Database : Northwind;Table : from Employees , i want to take only
employeeid,photo to the grid.
html code for Datagrid..
<Columns>
<asp:BoundColumn DataField="employeeid"
HeaderText="EmployeeId"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Picture">
<ItemTemplate>
<img runat ="server" src ='<%# "WebGridPture.aspx?IDS=" +
DataBinder.Eval(Container.DataItem,"EmployeeId") %> ' ID ="Img1"/>
</ItemTemplate>
</asp:TemplateColumn></Columns>
</asp:datagrid></form>
------------------------------------pageload code------------------
database connection and grid fillings and the grid displays with the
empty images, quite natual since no byte conversion.But in a button a
've the following code to get the image to the grid.
--------------------button code-------------------------------------
string employeeid = Request.QueryString["IDS"].ToString(); //** object
ref not allowed to an instance of an object * error *//
Dr=cmd.ExecuteReader();
Dr.Read();
byte[] bytearray=(byte[]) Dr["photo"];
Response.BinaryWrite(bytearray);
---------------------------------
let us assume that the DBconnection is good.
I am bit confused , how the query string gets the value for all the
records. and where the datasource for the grid ,i also get the errors.

Could u please tell the mistake on these or if u provide a better code ,
it would be great .

Thanks in advance
Raghuraman
 
S

Shiva

The image data is served from an another file called WebGridPture.aspx which
accepts the photo Id as a query string (check the src tag of the <img> tag).
So, the code you have written for button will work if is put in a separate
file named webgridpture.aspx (probably in page load).

HTH.



Hi,

I want to load the images from my sqlserver200 db to the datagrid.I've
seen a code from odetocode.com and it recreated to my wish.

Database : Northwind;Table : from Employees , i want to take only
employeeid,photo to the grid.
html code for Datagrid..
<Columns>
<asp:BoundColumn DataField="employeeid"
HeaderText="EmployeeId"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Picture">
<ItemTemplate>
<img runat ="server" src ='<%# "WebGridPture.aspx?IDS=" +
DataBinder.Eval(Container.DataItem,"EmployeeId") %> ' ID ="Img1"/>
</ItemTemplate>
</asp:TemplateColumn></Columns>
</asp:datagrid></form>
------------------------------------pageload code------------------
database connection and grid fillings and the grid displays with the
empty images, quite natual since no byte conversion.But in a button a
've the following code to get the image to the grid.
--------------------button code-------------------------------------
string employeeid = Request.QueryString["IDS"].ToString(); //** object
ref not allowed to an instance of an object * error *//
Dr=cmd.ExecuteReader();
Dr.Read();
byte[] bytearray=(byte[]) Dr["photo"];
Response.BinaryWrite(bytearray);
---------------------------------
let us assume that the DBconnection is good.
I am bit confused , how the query string gets the value for all the
records. and where the datasource for the grid ,i also get the errors.

Could u please tell the mistake on these or if u provide a better code ,
it would be great .

Thanks in advance
Raghuraman
 
R

Raghu Raman

Hi Shiva,

Thanks for that. That is right , the image tag src gets the query string
"IDS" for the photo , & it gets from the same paghe itself.

<img runat ="server" src ='<%# "WebGridPture.aspx?IDS=" +
DataBinder.Eval(Container.DataItem,"EmployeeId") %> ' ID ="Img1"/>


My doubt is , since i loaded the grid in the page load event, does the
<img src > tag get the query string for the every row in the grid.If it
is the case , how the following code works.Or shall i put the foll/
code in the page load event , so that it will go to the html tag for
every row.How ever the following code could be called only after the
grid's databind() method, am i right.Clear me , Please.


string employeeid = Request.QueryString["IDS"].ToString(); //** object
ref not allowed to an instance of an object * error *//
Dr=cmd.ExecuteReader();
Dr.Read();
byte[] bytearray=(byte[]) Dr["photo"];
Response.BinaryWrite(bytearray);


With thanks
Raghu
 
S

Shiva

Hi,

Yes, for each row in the grid, the browser puts a request to
WebGridPture.aspx passing the employee id as IDS querystring value, in order
to render the image. This .aspx files in turn reads the image data from the
database using the IDS value passed on to it (via querystring).
As per the image tag's src attribute, this code which fetches the image has
to be in a separate file named WebGridPture.aspx. Having this code in the
same page as the grid will result in the error that you are getting now.

Hope this helps.

Hi Shiva,

Thanks for that. That is right , the image tag src gets the query string
"IDS" for the photo , & it gets from the same paghe itself.

<img runat ="server" src ='<%# "WebGridPture.aspx?IDS=" +
DataBinder.Eval(Container.DataItem,"EmployeeId") %> ' ID ="Img1"/>


My doubt is , since i loaded the grid in the page load event, does the
<img src > tag get the query string for the every row in the grid.If it
is the case , how the following code works.Or shall i put the foll/
code in the page load event , so that it will go to the html tag for
every row.How ever the following code could be called only after the
grid's databind() method, am i right.Clear me , Please.


string employeeid = Request.QueryString["IDS"].ToString(); //** object
ref not allowed to an instance of an object * error *//
Dr=cmd.ExecuteReader();
Dr.Read();
byte[] bytearray=(byte[]) Dr["photo"];
Response.BinaryWrite(bytearray);


With thanks
Raghu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top