Trying to add image to sql2K DB

J

Jim in Arizona

I was, for the most part, following the directions at
http://aspalliance.com/articleViewer.aspx?aId=138&pId= . I shortened mine a
bit so that I only have to click browse, select my image and hit the submit
button. I get this error though when I try:


Server Error in '/' Application.
--------------------------------------------------------------------------------

Input string was not in a correct format.
Description: 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.

Exception Details: System.FormatException: Input string was not in a correct
format.

Source Error:


Line 12: Dim ImageStream As Stream
Line 13:
Line 14: intImageSize = File1.PostedFile.ContentType
Line 15: strImageType = File1.PostedFile.ContentType
Line 16: ImageStream = File1.PostedFile.InputStream



Source File: E:\hhsinternal\tests\sqlimage.aspx.vb Line: 14

Stack Trace:


[FormatException: Input string was not in a correct format.]
Microsoft.VisualBasic.CompilerServices.Conversions.ParseDecimal(String
Value, NumberFormatInfo NumberFormat) +193
Microsoft.VisualBasic.CompilerServices.Conversions.ToLong(String Value)
+72

[InvalidCastException: Conversion from string "image/x-png" to type 'Long'
is not valid.]
Microsoft.VisualBasic.CompilerServices.Conversions.ToLong(String Value)
+227
sqlimage.Button1_Click(Object sender, EventArgs e) in
E:\hhsinternal\tests\sqlimage.aspx.vb:14
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
+78
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5670

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50215.44; ASP.NET
Version:2.0.50215.44



Here is my code:

Imports System.IO
Imports System.Data
Imports System.Data.SqlClient

Partial Class sqlimage
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim intImageSize As Int64
Dim strImageType As String
Dim ImageStream As Stream

intImageSize = File1.PostedFile.ContentType
strImageType = File1.PostedFile.ContentType.ToString
ImageStream = File1.PostedFile.InputStream

Dim ImageContent(intImageSize) As Byte
Dim intStatus As Integer

intStatus = ImageStream.Read(ImageContent, 0, intImageSize)

Dim objConnection As SqlConnection
Dim objCommand As SqlCommand

objConnection = New
SqlConnection(ConfigurationManager.AppSettings("aphrodite_test"))
objCommand = New SqlCommand("sp_addimage", objConnection)

objCommand.CommandType = CommandType.StoredProcedure

Dim InsertImage As New SqlParameter("@ImageFile", SqlDbType.Image)
InsertImage.Value = ImageContent
objCommand.Parameters.Add(InsertImage)

Dim InsertImageType As New SqlParameter("@ImageFileType", SqlDbType.VarChar,
255)
InsertImageType.Value = strImageType
objCommand.Parameters.Add(InsertImageType)

Try

objConnection.Open()
objCommand.ExecuteNonQuery()
objConnection.Close()
Response.Write("Success")

Catch ex As Exception

Response.Write(ex.Message.ToString)

End Try
End Sub
End Class


This is my Stored Procedure on the sql box:

Create Proc sp_addimage
@ImageFile Image,
@ImageType varchar(255)
AS
BEGIN
INSERT INTO test.imagetest (photoimage, photoimagetype)
VALUES (@ImageFile, @ImageType)
END
GO


I have a database called test with a single table called imagetest with
three columns:

ID int IDENTITY,
photoimage image,
photoimagetype varchar(255)


Any help would be appreciated. If someone knows of an easier way to insert
an image into a sql 2000 table without using stored procedures, that'd be
great too! I only know how to use VB. C# would just leave me scratching my
head.

Thanks,
Jim
 
S

Steve C. Orr [MVP, MCSD]

Perhaps this code sample will work out better for you:
http://SteveOrr.net/articles/EasyUploads.aspx




Jim in Arizona said:
I was, for the most part, following the directions at
http://aspalliance.com/articleViewer.aspx?aId=138&pId= . I shortened mine a
bit so that I only have to click browse, select my image and hit the submit
button. I get this error though when I try:


Server Error in '/' Application.
--------------------------------------------------------------------------------

Input string was not in a correct format.
Description: 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.

Exception Details: System.FormatException: Input string was not in a
correct format.

Source Error:


Line 12: Dim ImageStream As Stream
Line 13:
Line 14: intImageSize = File1.PostedFile.ContentType
Line 15: strImageType = File1.PostedFile.ContentType
Line 16: ImageStream = File1.PostedFile.InputStream



Source File: E:\hhsinternal\tests\sqlimage.aspx.vb Line: 14

Stack Trace:


[FormatException: Input string was not in a correct format.]
Microsoft.VisualBasic.CompilerServices.Conversions.ParseDecimal(String
Value, NumberFormatInfo NumberFormat) +193
Microsoft.VisualBasic.CompilerServices.Conversions.ToLong(String Value)
+72

[InvalidCastException: Conversion from string "image/x-png" to type 'Long'
is not valid.]
Microsoft.VisualBasic.CompilerServices.Conversions.ToLong(String Value)
+227
sqlimage.Button1_Click(Object sender, EventArgs e) in
E:\hhsinternal\tests\sqlimage.aspx.vb:14
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String
eventArgument) +78

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5670

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50215.44;
ASP.NET Version:2.0.50215.44



Here is my code:

Imports System.IO
Imports System.Data
Imports System.Data.SqlClient

Partial Class sqlimage
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim intImageSize As Int64
Dim strImageType As String
Dim ImageStream As Stream

intImageSize = File1.PostedFile.ContentType
strImageType = File1.PostedFile.ContentType.ToString
ImageStream = File1.PostedFile.InputStream

Dim ImageContent(intImageSize) As Byte
Dim intStatus As Integer

intStatus = ImageStream.Read(ImageContent, 0, intImageSize)

Dim objConnection As SqlConnection
Dim objCommand As SqlCommand

objConnection = New
SqlConnection(ConfigurationManager.AppSettings("aphrodite_test"))
objCommand = New SqlCommand("sp_addimage", objConnection)

objCommand.CommandType = CommandType.StoredProcedure

Dim InsertImage As New SqlParameter("@ImageFile", SqlDbType.Image)
InsertImage.Value = ImageContent
objCommand.Parameters.Add(InsertImage)

Dim InsertImageType As New SqlParameter("@ImageFileType",
SqlDbType.VarChar, 255)
InsertImageType.Value = strImageType
objCommand.Parameters.Add(InsertImageType)

Try

objConnection.Open()
objCommand.ExecuteNonQuery()
objConnection.Close()
Response.Write("Success")

Catch ex As Exception

Response.Write(ex.Message.ToString)

End Try
End Sub
End Class


This is my Stored Procedure on the sql box:

Create Proc sp_addimage
@ImageFile Image,
@ImageType varchar(255)
AS
BEGIN
INSERT INTO test.imagetest (photoimage, photoimagetype)
VALUES (@ImageFile, @ImageType)
END
GO


I have a database called test with a single table called imagetest with
three columns:

ID int IDENTITY,
photoimage image,
photoimagetype varchar(255)


Any help would be appreciated. If someone knows of an easier way to insert
an image into a sql 2000 table without using stored procedures, that'd be
great too! I only know how to use VB. C# would just leave me scratching my
head.

Thanks,
Jim
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top