Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inserting an image into SQL Server error

Status
Not open for further replies.

rodeomount

Programmer
Sep 25, 2007
50
US
I have written the following code to upload an image to sql server 2000 using a fileupload control on the GUI but I'm getting an error stating "An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. " in reference to the ExecuteNonQuery method. What am I doing wrong?

Public Sub Insert()
Dim objConnection As SqlConnection = New SqlConnection(Session("ConnxString"))
Dim objCommand As SqlCommand
objCommand = New SqlCommand("INSERT INTO dbo.Horses (FoalYear,Price,[Image],Descript,Gender,Type)Values(@FoalYear,@Price,@Image,@Descript,@Gender,@Type)")
objCommand.Connection = objConnection
Dim ds As New DataSet
Dim reader As BinaryReader

Dim liImage As SqlParameter
liImage = New SqlParameter("@inputimage", SqlDbType.Image)
Dim fs As FileStream = New FileStream(flupImage.PostedFile.FileName, FileMode.Open)
reader = New BinaryReader(fs)
'Dim fs As System.IO.FileStream = New System.IO.FileStream(Server.MapPath(flupImage.PostedFile.FileName), System.IO.FileMode.Open)
Dim imagecontent() As Byte

'fs.Read(imagecontent, 0, (Convert.ToInt32(fs.Length.ToString, 10) - 1))
imagecontent = reader.ReadBytes(CType(fs.Length, Integer))
fs.Close()

par1.Value = imagecontent


'Creating an object for the 'PagedDataSource' for holding the data.

objCommand.Parameters.AddWithValue("@FoalYear", txtFoalYear.Text)
objCommand.Parameters.AddWithValue("@Price", txtPrice.Text)
objCommand.Parameters.AddWithValue("@Image", liImage)
objCommand.Parameters.AddWithValue("@Descript", txtDescript.Text)
objCommand.Parameters.AddWithValue("@Gender", cboGender.Text)
objCommand.Parameters.AddWithValue("@Type", "SaleHorse")
objConnection.Open()
objCommand.ExecuteNonQuery()
objConnection.Close()


End Sub
 
The value assigned to the @Image parameter should be imagecontent not limage as that implies you are passing a SqlParameter as a value to a SqlParameter.

All the stuff to do with creating limage appears to be redundant as there is no parameter called @inputimage.


Bob Boffin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top