I have a desktop application using vb.net, to process some excel files, those files are stored in a sql server database.
Here is the code I have:
Try
conDCS.Open()
comDCS.Connection = conDCS
comDCS.CommandType = CommandType.Text
comDCS.CommandText = "select top 2 [Filename], [File] " & _
"from tblFiles (nolock) " & _
"where ([Filename] like 'DIG%' or [Filename] like 'FAC%') and " & _
"(UploadDate>='" & FromDate & "' and UploadDate<'" & ToDate & "')"
comDCS.ExecuteNonQuery()
rdrDCS = comDCS.ExecuteReader
If rdrDCS.HasRows Then
While rdrDCS.Read
Dim imageInBytes As Byte() = rdrDCS(1)
Dim memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream(imageInBytes, False)
Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(memoryStream)
image.Save(TempPath & rdrDCS(0))
End While
ToProcess = True
End If
rdrDCS.Close()
Catch ex As Exception
ToProcess = False
MessageBox.Show("Error accessing to the files: " & ex.Message)
Finally
conDCS.Close()
End Try
I’m getting: “Parameter is not valid” in this line:
Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(memoryStream)
I have understood that this error is related to an invalid image data, but I can’t figure it out what could be the problem.
imageInBytes has a length of 41473. And some items of the array have zero as value.
What could be wrong here, or perhaps, can anyone supply a working code to achieve this?
We use a common method to save a file from a database field to a file:
This can be called as follows: