Syntax problem

W

Wayne Hoover

Perhaps it's just that I don't fully understand VB.Net at this
point (does anyone?!), but I can not figure out how to manage
this most simple of processes. I have an image that I load from
disk and then save to a SQL database. That part works fine. The
problem has to do with my DIM statement. I want to DIM the variable
at the beginning of the code (and must do so) but LENGTH is not
a member of BYTE. I need to do a CTYPE on the line below (#1)
the way it is done in #2, but I can't get the syntax on this
to work. Var 'bytBlobData' is apparently inheriting the length
member from the CTYPE operation and it looks like I should be
able to DIM the variable outside the IF END IF block, then
do a CTYPE operation in the block. So what is the ctype operation
that I'm missing?

Any help appreciated.

Private Sub cmdSaveAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSaveAll.Click
Dim fsBLOBFile As FileStream
Dim lsOrgName As String
Dim bytBlobData As Byte '<-- This is what I want
IF (strBlobFilePath <> "") then
sBlobFile = new FileStream(strBLOBFilePath, FileMode.Open, FileAccess.Read)
'---------------------------------------------------
'-- The next line fails with 'Length' is not a member of 'Byte'
'---------------------------------------------------
bytBlobData = CType(fsBLOBFile.Length(), Integer) - 1 ' (#1)

'---------------------------------------------------
'-- This line works, but is out of scope in the
'-- rest of the procedure so I can't access the
'-- bytBLOBData variable outside of the IF ENDIF
'-- block..
'---------------------------------------------------
'Dim bytBLOBData(CType(fsBLOBFile.Length(), Integer) - 1) As Byte (#2) '<-- This is what I need to do

fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length)
fsBLOBFile.Close()
If (bytblobdata.Length = 0) Then
messagebox.show(The selected bitmap is corrupt.")
Exit Sub
End If
End If

cmd.Connection = cn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "UpdateDatabase"
cmd.parameters.Add(new SqlParameter("@RecID", iRecordID))
'-------------------------------------------------------
'-- Because bytBLOBData was DIMed in the IF END IF block
'-- above, this command can't see bytBLOBData.
'-------------------------------------------------------
cmd.Parameters.Add(new SqlParameter("@BLOBData", SqlDbType.VarBinary, _
bytBLOBData.Length, ParameterDirection.Input, True, _
0, 0, Nothing, DataRowVersion.Current, bytBLOBData)) '<-- Fails!
<Code omitted>
End Sub
 
S

Scott

If bytBlobData is a Byte array then declare it originally as

Dim bytBlobData() as Byte

And then redimension it in the "If" block using

ReDim bytBlobData(CType(fsBLOBFile.Length(), Integer) - 1)


Perhaps it's just that I don't fully understand VB.Net at this
point (does anyone?!), but I can not figure out how to manage
this most simple of processes. I have an image that I load from
disk and then save to a SQL database. That part works fine. The
problem has to do with my DIM statement. I want to DIM the variable
at the beginning of the code (and must do so) but LENGTH is not
a member of BYTE. I need to do a CTYPE on the line below (#1)
the way it is done in #2, but I can't get the syntax on this
to work. Var 'bytBlobData' is apparently inheriting the length
member from the CTYPE operation and it looks like I should be
able to DIM the variable outside the IF END IF block, then
do a CTYPE operation in the block. So what is the ctype operation
that I'm missing?

Any help appreciated.

Private Sub cmdSaveAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSaveAll.Click
Dim fsBLOBFile As FileStream
Dim lsOrgName As String
Dim bytBlobData As Byte '<-- This is what I want
IF (strBlobFilePath <> "") then
sBlobFile = new FileStream(strBLOBFilePath, FileMode.Open, FileAccess.Read)
'---------------------------------------------------
'-- The next line fails with 'Length' is not a member of 'Byte'
'---------------------------------------------------
bytBlobData = CType(fsBLOBFile.Length(), Integer) - 1 ' (#1)

'---------------------------------------------------
'-- This line works, but is out of scope in the
'-- rest of the procedure so I can't access the
'-- bytBLOBData variable outside of the IF ENDIF
'-- block..
'---------------------------------------------------
'Dim bytBLOBData(CType(fsBLOBFile.Length(), Integer) - 1) As Byte (#2) '<-- This is what I need to do

fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length)
fsBLOBFile.Close()
If (bytblobdata.Length = 0) Then
messagebox.show(The selected bitmap is corrupt.")
Exit Sub
End If
End If

cmd.Connection = cn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "UpdateDatabase"
cmd.parameters.Add(new SqlParameter("@RecID", iRecordID))
'-------------------------------------------------------
'-- Because bytBLOBData was DIMed in the IF END IF block
'-- above, this command can't see bytBLOBData.
'-------------------------------------------------------
cmd.Parameters.Add(new SqlParameter("@BLOBData", SqlDbType.VarBinary, _
bytBLOBData.Length, ParameterDirection.Input, True, _
0, 0, Nothing, DataRowVersion.Current, bytBLOBData)) '<-- Fails!
<Code omitted>
End Sub
 
W

Wayne Hoover

Thanks Scott -- that did it.

If bytBlobData is a Byte array then declare it originally as

Dim bytBlobData() as Byte

And then redimension it in the "If" block using

ReDim bytBlobData(CType(fsBLOBFile.Length(), Integer) - 1)


Perhaps it's just that I don't fully understand VB.Net at this
point (does anyone?!), but I can not figure out how to manage
this most simple of processes. I have an image that I load from
disk and then save to a SQL database. That part works fine. The
problem has to do with my DIM statement. I want to DIM the variable
at the beginning of the code (and must do so) but LENGTH is not
a member of BYTE. I need to do a CTYPE on the line below (#1)
the way it is done in #2, but I can't get the syntax on this
to work. Var 'bytBlobData' is apparently inheriting the length
member from the CTYPE operation and it looks like I should be
able to DIM the variable outside the IF END IF block, then
do a CTYPE operation in the block. So what is the ctype operation
that I'm missing?

Any help appreciated.

Private Sub cmdSaveAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSaveAll.Click
Dim fsBLOBFile As FileStream
Dim lsOrgName As String
Dim bytBlobData As Byte '<-- This is what I want
IF (strBlobFilePath <> "") then
sBlobFile = new FileStream(strBLOBFilePath, FileMode.Open, FileAccess.Read)
'---------------------------------------------------
'-- The next line fails with 'Length' is not a member of 'Byte'
'---------------------------------------------------
bytBlobData = CType(fsBLOBFile.Length(), Integer) - 1 ' (#1)

'---------------------------------------------------
'-- This line works, but is out of scope in the
'-- rest of the procedure so I can't access the
'-- bytBLOBData variable outside of the IF ENDIF
'-- block..
'---------------------------------------------------
'Dim bytBLOBData(CType(fsBLOBFile.Length(), Integer) - 1) As Byte (#2) '<-- This is what I need to do

fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length)
fsBLOBFile.Close()
If (bytblobdata.Length = 0) Then
messagebox.show(The selected bitmap is corrupt.")
Exit Sub
End If
End If

cmd.Connection = cn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "UpdateDatabase"
cmd.parameters.Add(new SqlParameter("@RecID", iRecordID))
'-------------------------------------------------------
'-- Because bytBLOBData was DIMed in the IF END IF block
'-- above, this command can't see bytBLOBData.
'-------------------------------------------------------
cmd.Parameters.Add(new SqlParameter("@BLOBData", SqlDbType.VarBinary, _
bytBLOBData.Length, ParameterDirection.Input, True, _
0, 0, Nothing, DataRowVersion.Current, bytBLOBData)) '<-- Fails!
<Code omitted>
End Sub
 
P

Philip Rieck

1) Try to use plain text format if you can -- many people can't (or won't) read messges in HTML


It seems you're mixing up three concepts here - Byte, arrays, and declaration vs. instantiation

What you are creating in #2 is an array of bytes (as in Dim b() as byte, not Dim b as byte). Since you don't know the length of the array when you are declaring, you can do this:


'In your dims, replace Dim bytBlobData as Byte with this line:
Dim bytBlobData() as Byte

'Replace #2 below with
ReDim bytBlobData( fsBlobFile.length - 1)


Now your SQL command will execute, because bytBlobData is still in scope.
Note that your fsBlobFile.length may not give you the accurate length
Perhaps it's just that I don't fully understand VB.Net at this
point (does anyone?!), but I can not figure out how to manage
this most simple of processes. I have an image that I load from
disk and then save to a SQL database. That part works fine. The
problem has to do with my DIM statement. I want to DIM the variable
at the beginning of the code (and must do so) but LENGTH is not
a member of BYTE. I need to do a CTYPE on the line below (#1)
the way it is done in #2, but I can't get the syntax on this
to work. Var 'bytBlobData' is apparently inheriting the length
member from the CTYPE operation and it looks like I should be
able to DIM the variable outside the IF END IF block, then
do a CTYPE operation in the block. So what is the ctype operation
that I'm missing?

Any help appreciated.

Private Sub cmdSaveAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSaveAll.Click
Dim fsBLOBFile As FileStream
Dim lsOrgName As String
Dim bytBlobData As Byte '<-- This is what I want
IF (strBlobFilePath <> "") then
sBlobFile = new FileStream(strBLOBFilePath, FileMode.Open, FileAccess.Read)
'---------------------------------------------------
'-- The next line fails with 'Length' is not a member of 'Byte'
'---------------------------------------------------
bytBlobData = CType(fsBLOBFile.Length(), Integer) - 1 ' (#1)

'---------------------------------------------------
'-- This line works, but is out of scope in the
'-- rest of the procedure so I can't access the
'-- bytBLOBData variable outside of the IF ENDIF
'-- block..
'---------------------------------------------------
'Dim bytBLOBData(CType(fsBLOBFile.Length(), Integer) - 1) As Byte (#2) '<-- This is what I need to do

fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length)
fsBLOBFile.Close()
If (bytblobdata.Length = 0) Then
messagebox.show(The selected bitmap is corrupt.")
Exit Sub
End If
End If

cmd.Connection = cn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "UpdateDatabase"
cmd.parameters.Add(new SqlParameter("@RecID", iRecordID))
'-------------------------------------------------------
'-- Because bytBLOBData was DIMed in the IF END IF block
'-- above, this command can't see bytBLOBData.
'-------------------------------------------------------
cmd.Parameters.Add(new SqlParameter("@BLOBData", SqlDbType.VarBinary, _
bytBLOBData.Length, ParameterDirection.Input, True, _
0, 0, Nothing, DataRowVersion.Current, bytBLOBData)) '<-- Fails!
<Code omitted>
End Sub
 

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

Top