User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 401,571 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,368 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 37113 | Replies: 6
Reply
Join Date: Mar 2004
Location: Brisbane
Posts: 632
Reputation: Slade has a spectacular aura about Slade has a spectacular aura about 
Rep Power: 7
Solved Threads: 6
Slade's Avatar
Slade Slade is offline Offline
Practically a Master Poster

Working with SQL server's Image data type

  #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 As Integer

Dim imgtype As String

Dim n As Integer

imgdatastream = Pic.PostedFile.InputStream

imgdatalen = Pic.PostedFile.ContentLength

imgtype = Pic.PostedFile.ContentType

Dim imgdata() As Byte = 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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2002
Location: New York
Posts: 862
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 
Rep Power: 12
Solved Threads: 15
Moderator
Tekmaven's Avatar
Tekmaven Tekmaven is offline Offline
The C# Man, Myth, Legend

Re: Working with SQL server's Image data type

  #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

ASP.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  
Join Date: Mar 2004
Location: Brisbane
Posts: 632
Reputation: Slade has a spectacular aura about Slade has a spectacular aura about 
Rep Power: 7
Solved Threads: 6
Slade's Avatar
Slade Slade is offline Offline
Practically a Master Poster

Re: Working with SQL server's Image data type

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

Cheers Tekmaven,

Slade
Formerly known as Slade.
Reply With Quote  
Join Date: Mar 2004
Location: Brisbane
Posts: 632
Reputation: Slade has a spectacular aura about Slade has a spectacular aura about 
Rep Power: 7
Solved Threads: 6
Slade's Avatar
Slade Slade is offline Offline
Practically a Master Poster

Re: Working with SQL server's Image data type

  #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  
Join Date: Mar 2004
Location: Brisbane
Posts: 632
Reputation: Slade has a spectacular aura about Slade has a spectacular aura about 
Rep Power: 7
Solved Threads: 6
Slade's Avatar
Slade Slade is offline Offline
Practically a Master Poster

Re: Working with SQL server's Image data type

  #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...

 
Private Sub UpdateProfile(ByVal UserID As String)

Dim imgstream As Stream = Pic.PostedFile.InputStream

Dim imgdata(Pic.PostedFile.ContentLength) As Byte

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



Dim myConnection As New 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:

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

and then of course you have to declare it in the server side code
Protected WithEvents 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  
Join Date: Mar 2004
Location: Brisbane
Posts: 632
Reputation: Slade has a spectacular aura about Slade has a spectacular aura about 
Rep Power: 7
Solved Threads: 6
Slade's Avatar
Slade Slade is offline Offline
Practically a Master Poster

Re: Working with SQL server's Image data type

  #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  
Join Date: Apr 2007
Posts: 1
Reputation: rfidler is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
rfidler rfidler is offline Offline
Newbie Poster

Re: Working with SQL server's Image data type

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

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb ASP.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 4:01 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC