Image Upload Irrational Error - "not declared" error despite identicalcode!!!

N

Neo Geshel

I am trying to upload images to an access db using a DataGrid. The code
inside the INSERT sub is identical to almost everything else I've seen
on the net, but it still won't work. The compiler seems to not recognize
that the add_CarShowGalleryPhoto is referring to a form field with a
type of "file". How do I modify the code so that it works??

Sub dgCarShowGalleryPhotos_Insert(sender As Object, e As
DataGridCommandEventArgs)
If e.CommandName = "Insert" Then
If Not add_CarShowGalleryPhoto.PostedFile Is Nothing Then
If add_CarShowGalleryPhoto.PostedFile.ContentLength > 0 And
add_CarShowGalleryPhoto.PostedFile.ContentType = "image/jpeg" Then
Dim imgStream As Stream = add_CarShowGalleryPhoto.PostedFile.InputStream()
If add_CarShowGalleryPhoto.PostedFile.Width > 640 Or
add_CarShowGalleryPhoto.PostedFile.Height > 640 Then
Dim imgbin(createThumbnail(imgStream, 640, 640)) as Byte
Else
Dim imgLen As Int32 = add_CarShowGalleryPhoto.PostedFile.ContentLength
Dim imgBinaryData(imgLen) as Byte
Dim n As Int32 = imgStream.Read(imgBinaryData, 0, imgLen)
Dim imgBin(imgBinaryData) as Byte
End If
Dim strComment As String =
CType(e.Item.FindControl("add_CarShowGalleryComment"),
TextBox).Text.Replace("'","’")
Dim strSQL As String = "INSERT INTO [tblCarShowGalleryPhotos]
(CarShowGalleryYearID, Image, Comment) VALUES (" & Session("IDHolder") &
", @Image, @Comment)"
Dim objConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
objConn.Open()
Dim myCommand as OleDbCommand = new OleDbCommand(strSQL, objConn)
myCommand.CommandType = CommandType.Text
Dim parameterImage as OleDbParameter = new OleDbParameter("@Image",
OleDbType.LongVarBinary)
parameterImage.Value = imgBin
myCommand.Parameters.Add(parameterImage)
Dim parameterComment as OleDbParameter = new OleDbParameter("@Comment",
OleDbType.LongVarWChar)
parameterComment.Value = strComment
myCommand.Parameters.Add(parameterComment)
myCommand.ExecuteNonQuery()
objConn.Close()
dgCarShowGalleryPhotos.EditItemIndex = -1
BindData()
End If
End If
End If
End Sub



TIA
...Geshel
--
**********************************************************************
My reply-to is an automatically monitored spam honeypot. Do not use it
unless you want to be blacklisted by SpamCop. Please reply to my first
name at my last name dot org.
**********************************************************************
 
G

Guest

That is because you need to declare it within the codebehind class (not
inside any method). Look at your othercontrols: they will all be declared
like that. Also be sure runnat=server is on the control.
 
N

Neo Geshel

Jason said:
Neo Geshel said:
I am trying to upload images to an access db using a DataGrid. The code
inside the INSERT sub is identical to almost everything else I've seen
on the net, but it still won't work. The compiler seems to not recognize
that the add_CarShowGalleryPhoto is referring to a form field with a
type of "file". How do I modify the code so that it works??

Sub dgCarShowGalleryPhotos_Insert(sender As Object, e As
DataGridCommandEventArgs)
If e.CommandName = "Insert" Then
If Not add_CarShowGalleryPhoto.PostedFile Is Nothing Then
If add_CarShowGalleryPhoto.PostedFile.ContentLength > 0 And
add_CarShowGalleryPhoto.PostedFile.ContentType = "image/jpeg" Then
Dim imgStream As Stream = add_CarShowGalleryPhoto.PostedFile.InputStream()
If add_CarShowGalleryPhoto.PostedFile.Width > 640 Or
add_CarShowGalleryPhoto.PostedFile.Height > 640 Then
Dim imgbin(createThumbnail(imgStream, 640, 640)) as Byte
Else
Dim imgLen As Int32 = add_CarShowGalleryPhoto.PostedFile.ContentLength
Dim imgBinaryData(imgLen) as Byte
Dim n As Int32 = imgStream.Read(imgBinaryData, 0, imgLen)
Dim imgBin(imgBinaryData) as Byte
End If
Dim strComment As String =
CType(e.Item.FindControl("add_CarShowGalleryComment"),
TextBox).Text.Replace("'","’")
Dim strSQL As String = "INSERT INTO [tblCarShowGalleryPhotos]
(CarShowGalleryYearID, Image, Comment) VALUES (" & Session("IDHolder") &
", @Image, @Comment)"
Dim objConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
objConn.Open()
Dim myCommand as OleDbCommand = new OleDbCommand(strSQL, objConn)
myCommand.CommandType = CommandType.Text
Dim parameterImage as OleDbParameter = new OleDbParameter("@Image",
OleDbType.LongVarBinary)
parameterImage.Value = imgBin
myCommand.Parameters.Add(parameterImage)
Dim parameterComment as OleDbParameter = new OleDbParameter("@Comment",
OleDbType.LongVarWChar)
parameterComment.Value = strComment
myCommand.Parameters.Add(parameterComment)
myCommand.ExecuteNonQuery()
objConn.Close()
dgCarShowGalleryPhotos.EditItemIndex = -1
BindData()
End If
End If
End If
End Sub

TIA
...Geshel
That is because you need to declare it within the codebehind class (not
inside any method). Look at your othercontrols: they will all be declared
like that. Also be sure runnat=server is on the control.

Actually, I think I've got it working now. I'm not using code-behind at
this time for simplicity. I'm not the most advanced at ASP.NET, so I'm
keeping everything inline until it works.

Turns out I needed to DIM the form field first (Dim imgContent as Object
= e.Item.Cells(2).Controls(1)), and then use imgContent as a placeholder
(imgContent.PostedFile...)

Turns out quite a few web sites failed to display or mention this...
hence my frustration.

Thanks anyhow.
...Geshel
--
**********************************************************************
My reply-to is an automatically monitored spam honeypot. Do not use it
unless you want to be blacklisted by SpamCop. Please reply to my first
name at my last name dot org.
**********************************************************************
 

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