asp .net / sql / image data type question

M

mattmerc

Hi all,

I posted this about a week back with no response so I want to try
again. I have a weird questions in regards to working with sql image
data types. I know how to take the image type and convert it into a
picture on the screen. What I have is a situation where I need to read
the image data from one database, store it, and then upload it to
another database. I tried storing it in viewstate and in a byte
variable. I don't know if I am loosing the lenth property or what, but
I cannot get the data from one database to another. So once I read
IMAGE data type out of an sql database, what kind of asp .net variable
can I save it to so I can write it into another database? Thanks all!
 
G

Guest

I think the way to go is: reading the Image data from the DB to the byte[]
(byte array) using StreamReader and backwards.

Cheers
 
M

mattmerc

I appreciate the advise guys.

Pat,

You are showing me how to display a picture from SQL image type. I have
successfully implemented this already. I need to know how to transfer
this information into another column in another table in another
database. Should be easy, but I am not sure how to.

Alex444,

I do not understand how to do what you have suggested. I will do some
searches. If you could give me more of an example, I would GREATLY
appreciate it.

Thanks again all. You guys are great.
 
M

mattmerc

Thanks rviray,

The database are on two different server and I do not have control
(just read access) where the image is coming from. I tried using
viewstate to transfer information with not luck. I tried a byte array
but I dont think I read the length correctly. I will try the session
and get back to you. I would need to see code for the byte aray because
I am doing something wrong. Simple version of my code below ( I am
purposely leaving the database configurations out):

Dim my_byt_variable As Byte()
my_byt_variable = my_data_reader("my_image")
INSERT INTO my_table (image) VALUES (my_byt_variable)

the above format doesn't work for me. I think have to redim the byte
variable with the length of the datareader data

Dim my_byt_variable As Byte()
ReDim bytContent(my_data_reader("my_image").length)
my_byt_variable = my_data_reader("my_image")
INSERT INTO my_table (image) VALUES (my_byt_variable)

but I don't think this worked for me either. It has been a week since I
have played with it. Need to get back to it I guess. Thanks for your
help.
 
M

mattmerc

I just tried it with session and it does not work. I loaded the image
field into a datareader and then into a session variable. I made a
connection to the new database and wrote the session variable to the
image column. When a reload the picture it is blank.
 
M

mattmerc

man....I finally got it. I'm ashamed of myself.

I had a syntax error in my page that displays the image, but since I
bound this page to an img tag(the one with the error), I wasn't getting
an error message. Just a picture not displayed. I tried loading the
page bound to my img tag and got the error and fixed it. Storing in
session variable worked fine. Thanks all.
 
G

Guest

Matt,

You specify in one of your posts that you already retreive your images from
the SQL DB. Could you post an example or send it to me on how to do that?

I can upload images into my SQL DB, but can't retreive them in a GridView or
DetailsView. I'm using ASP.NET 2.0 and VB.NET.

Thanks for your help,
Bart
 
M

mattmerc

I don't know if my way is conventional. Here is what I do.

The page that requests an image uses as session variable to hold the
primary key information for the record I am looking for. This page also
has an img tag bound to a 2nd page that will load the picture. I check
to see if there is picture data, and if there is, I make the img tag
visible that is bound to the page showing the picture. I'm not very
experienced with this stuff, but I was able to get it to work this way.
So again,

1) user click a data grid in order to see a picture.
2) I pass the primary key for that record based on previous click to
session
3) I check to see if picture data is there and show the img tag bound
to a 2nd page that will output the picture based on the session
variable.

here is my img tag:

<img id ="img_email_picture_lenel" src="picture_lenel.aspx"
height="300" width="250" runat="server">

Here is my datagrid code:

If e.commandname="picture" Then


Dim cmd_load_picture As SqlCommand
Dim dtr_load_picture As SqlDataReader

'Dim str_blob_length As String

cmd_load_picture = New SqlCommand ("SELECT * FROM mmobjs WHERE
empid='" & session("empid") & "'", conMatt_I3)
dtr_load_picture = cmd_load_picture.ExecuteReader()

If dtr_load_picture.read() = false Then
div_person_lookup_no_picture.visible = true 'shows alert div
stating no photo available
img_person_lookup_lenel.visible = false 'hides lenel picture
because there isn't one
img_person_lookup_none.visible = true 'shows image for no photo
available
Else
div_person_lookup_no_picture.visible = false 'hides alert div
stating no photo available
img_person_lookup_lenel.visible = true ' show lenel picture
img_person_lookup_none.visible = false 'hide no photo picture
'Response.Write("<Script
language='javascript'>window.open('picture.aspx',null,'height=300,
width=300, status= no, resizable= no, scrollbars=yes,
toolbar=no,location=no,menubar=no, left=900, top=100 ');" & chr(60) &
"/script>")
End If
End If
Here is the page showing the picture:
Sub Page_Load


conMatt_I3.Open()

Dim cmdLoadAttachment As SqlCommand
Dim dtrLoadAttachment As SqlDataReader

'Dim str_blob_length As String

cmdLoadAttachment = New SqlCommand ("SELECT * FROM mmobjs WHERE
empid='" & session("empid") & "'", conMatt_I3)
dtrLoadAttachment = cmdLoadAttachment.ExecuteReader()
dtrLoadAttachment.Read()
Response.Buffer = True
Response.ContentType = "Image/BMP"
Response.BinaryWrite(dtrLoadAttachment("lnl_blob"))
dtrLoadAttachment.Close()

Hope this helps you.
End Sub
 

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

Top