Working with SQL server's Image data type

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2004
Posts: 634
Reputation: Slade has a spectacular aura about Slade has a spectacular aura about 
Solved Threads: 7
Slade's Avatar
Slade Slade is offline Offline
Practically a Master Poster

Working with SQL server's Image data type

 
0
  #1
Sep 6th, 2004
Hey guys next question . Now I have having troubles with the IMAGE data type, can anyone provide examples on how to retrieve and update the image? I have tried the following:

 
Dim imgdatastream As FileStream

Dim imgdatalen AsInteger

Dim imgtype AsString

Dim n AsInteger

imgdatastream = Pic.PostedFile.InputStream

imgdatalen = Pic.PostedFile.ContentLength

imgtype = Pic.PostedFile.ContentType

Dim imgdata() AsByte = New [Byte](imgdatalen) {}

n = imgdatastream.Read(imgdata, 0, imgdatalen)
 
Dim paramAvatar As SqlParameter = myCommand.Parameters.Add("@Avatar", SqlDbType.Image)

paramAvatar.Value = n



But I recieved the lovely error "
Specified cast is not valid. "


help?
Formerly known as Slade.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 898
Reputation: Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light 
Solved Threads: 28
Moderator
Tekmaven's Avatar
Tekmaven Tekmaven is offline Offline
The C# Man, Myth, Legend

Re: Working with SQL server's Image data type

 
0
  #2
Sep 6th, 2004
Which line did you get the error on? I really haven't worked with the image data type before (i usually store an uploaded image on the server's file system), but I can play with it .
-Ryan Hoffman

.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 634
Reputation: Slade has a spectacular aura about Slade has a spectacular aura about 
Solved Threads: 7
Slade's Avatar
Slade Slade is offline Offline
Practically a Master Poster

Re: Working with SQL server's Image data type

 
0
  #3
Sep 6th, 2004
For this error it was this line
  1. imgdatastream = Pic.PostedFile.InputStream

Cheers Tekmaven,

Slade
Formerly known as Slade.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 634
Reputation: Slade has a spectacular aura about Slade has a spectacular aura about 
Solved Threads: 7
Slade's Avatar
Slade Slade is offline Offline
Practically a Master Poster

Re: Working with SQL server's Image data type

 
0
  #4
Sep 6th, 2004
Hmm I think I found my mistake, but I can't test it as of yet... will let you know how I go when I get home
Formerly known as Slade.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 634
Reputation: Slade has a spectacular aura about Slade has a spectacular aura about 
Solved Threads: 7
Slade's Avatar
Slade Slade is offline Offline
Practically a Master Poster

Re: Working with SQL server's Image data type

 
0
  #5
Sep 8th, 2004
Hey guys

Well i've written some code to upload it but now I have to get some to download it and display it ehehe...

Ok stored procedure:

CREATE procedure sf_GetProfileImage
 @UserID nvarchar(20) 
  
AS
SELECT Avatar, Av_type
FROM sf_Members
WHERE UserID = @UserID
GO


and now the vb code...

 
PrivateSub UpdateProfile(ByVal UserID AsString)

Dim imgstream As Stream = Pic.PostedFile.InputStream

Dim imgdata(Pic.PostedFile.ContentLength) AsByte

imgstream.Read(imgdata, 0, Pic.PostedFile.ContentLength)



Dim myConnection AsNew SqlConnection(ConfigurationSettings.AppSettings("strSqlConnectionString"))

Dim myCommand = New SqlCommand("sf_GetProfileImage", myConnection)

myCommand.CommandType = CommandType.StoredProcedure

Dim paramUserID As SqlParameter = myCommand2.Parameters.Add("@UserID", SqlDbType.NVarChar, 20)

paramUserID.Value = UserID

Dim myReader As SqlDataReader

myConnection.Open()

myReader = myCommand.ExecuteReader
 
Dim paramAvatar As SqlParameter = myCommand.Parameters.Add("@Avatar", SqlDbType.Image)
 
paramAv_type.Value = Pic.PostedFile.ContentType
Dim paramAv_type As SqlParameter = myCommand.Parameters.Add("@Av_type", SqlDbType.NVarChar, 100)

paramAvatar.Value = imgdata

myReader.Close()
myconnection.close
myconnection.dispose
mycommand.dispose
 
End Sub


And the html input button in the aspx page is:

  1. < INPUT id="Pic" type="file" name="Pic" runat="server" >

and then of course you have to declare it in the server side code
ProtectedWithEvents Pic As System.Web.UI.HtmlControls.HtmlInputFile


PHEW! Finally done, now I need to know how the heck I can display these images on the page. Anyone got any ideas?
Formerly known as Slade.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 634
Reputation: Slade has a spectacular aura about Slade has a spectacular aura about 
Solved Threads: 7
Slade's Avatar
Slade Slade is offline Offline
Practically a Master Poster

Re: Working with SQL server's Image data type

 
0
  #6
Sep 8th, 2004
oh... I got it working but I must've forgot to hit the submit button when I typed it last night eheh.... I'll do it when I get home.
Formerly known as Slade.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 1
Reputation: rfidler is an unknown quantity at this point 
Solved Threads: 0
rfidler rfidler is offline Offline
Newbie Poster

Re: Working with SQL server's Image data type

 
0
  #7
Apr 4th, 2007
paramAvatar.Value = n <----- this needs to be imgdata
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC