Image not retrieving back from mysql

callraheel

Well-known member
Joined
Dec 12, 2004
Messages
65
Location
London,UK
Programming Experience
1-3
Hello
Now the photo is not retrieving back...Its saves well and i have created some code which you can see as follows to get back photo :
Public Shared Function GetPictureFromDB(ByVal sid As String) As MemoryStream

' returning stream

Dim rStream As New MemoryStream

Try

Dim q As String = "SELECT PhotoSize,Photo FROM photos WHERE PhotoID='" & sid & "';"

Dim connDB As New MySqlConnection

connDB = Connection.AutoConnect

Dim objCmd As New MySqlCommand(q, connDB)

Dim objReader As MySqlDataReader

objReader = objCmd.ExecuteReader(CommandBehavior.SequentialAccess)

' create an array to return..

While objReader.Read()

' first get the size of the picture

Dim picSize As Long = objReader.Item("PhotoSize")

' specify the constant size of bytes to fetch

Const bufferSize As Integer = 200

Dim startIndex As Long = 0

Dim outByte(bufferSize - 1) As Byte

Dim gotBytes As Long

' now start reading and saving in the memory stram...

gotBytes = objReader.GetBytes(1, startIndex, outByte, 0, bufferSize)

' now using this picture size get all data from BLOB value of Photo

Do While bufferSize = gotBytes

' save the fetched byte array...

rStream.Write(outByte, 0, outByte.Length - 1)

' now increment the fetchedbyte

startIndex += bufferSize

' now proceed reading and saving in the memory stram...

gotBytes = objReader.GetBytes(1, startIndex, outByte, 0, bufferSize)

Loop

' save the fetched byte array...

rStream.Write(outByte, 0, outByte.Length - 1)

Console.WriteLine("Last Got Bytes : " & gotBytes)

End While

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Function

Now see it do well until the last fetch operation. Where it says the array cannot be null it means the last time the values are not written properly..So anyone can help out please..............

Regards
 
Hello.........anyone there...Well i think the code is little bit long....isnt it..But its also necessary to trace errors...so looking forward for your replies...
 
Back
Top