-2

I would like to know whats wrong with this code, because when I retrieve the image it gives me an error message saying:

"Connection must be valid and open."

'Send sql query to retrieve information about the employees
sqlConn = "SELECT Description, Price, Maker, Color, Items, Store_Available, Serial_number, Sales_number, Picture from `mayombe_mdcs`.`shoesinstore` where Serial_number = @serial"

objDataAdapter = New MySqlDataAdapter(sqlConn, objConn)
objDataAdapter.SelectCommand.Parameters.AddWithValue("@serial", strSerial)
Dim pictureData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())


Dim picture As Image = Nothing
'Create a stream in memory containing the bytes that comprise the image.
Using stream As New IO.MemoryStream(pictureData)

    'Read the stream and create an Image object from the data.    
    PictureBox1.Image = Image.FromStream(stream)
End Using

'Fill the form with retrieved informations
objDataAdapter.Fill(objDataset)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Hfirst
  • 41
  • 10
  • 4
    What's up with the unused data adapter `objDataAdapter`? Before calling `ExecuteScalar` open the connection by calling `sqlConn.Open()`. When you've fixed this, you might receive a cast error as `Description` doesn't sound much like an image. `ExecuteScalar` returns the first column in the first row. – Bjørn-Roger Kringsjå Sep 30 '14 at 18:50
  • I would consider not storing an image in your database, but to instead store a string value of the file path to the image. There may be some constraint in your case that prevents this, but it is usually not preferred to store images in a database. Check out this link for more information, the accepted answer states why very succinctly. http://stackoverflow.com/questions/561447/store-pictures-as-files-or-in-the-database-for-a-web-app – davidallyoung Sep 30 '14 at 19:00
  • 3
    there is no code there indicating there is an open or valid connection just like the error message says – Ňɏssa Pøngjǣrdenlarp Sep 30 '14 at 19:06

1 Answers1

0
'Send sql query to retreive information about the employees
obConn.open()'<=================================================================================
sqlConn = "SELECT Description, Price, Maker, Color, Items, Store_Available, Serial_number, Sales_number, Picture from `mayombe_mdcs`.`shoesinstore` where Serial_number = @serial"

objDataAdapter = New MySqlDataAdapter(sqlConn, objConn)
objDataAdapter.SelectCommand.Parameters.AddWithValue("@serial", strSerial)
objDataAdapter.SelectCommand.ExecuteScalar()<===================================================
Dim pictureData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())


Dim picture As Image = Nothing
'Create a stream in memory containing the bytes that comprise the image.
Using stream As New IO.MemoryStream(pictureData)

    'Read the stream and create an Image object from the data.    
    PictureBox1.Image = Image.FromStream(stream)
End Using

'Fill the form with retreived informations
objDataAdapter.Fill(objDataset)
Ciper123
  • 121
  • 1
  • 13