Hi ok so i want to read images from an sql server database..the images are saved in bytes but im struggeling to retrieve them my code for the retrieving

this is the code i used to save the image and its working.

 Dim sql As String = "INSERT INTO item VALUES(@itemName, @itemCost, @itemRecipe1, @itemRecipe2, @itemRecipe3, @itemShop, @itemBonus, @pictureName, @itemPicture)"
            Dim cmd As New SqlCommand(sql, sqlcon)

            cmd.Parameters.AddWithValue("@itemName", txtItemN.Text)
            cmd.Parameters.AddWithValue("@itemCost", txtCost.Text)
            cmd.Parameters.AddWithValue("@itemRecipe1", txtRecipe1.Text)
            cmd.Parameters.AddWithValue("@itemRecipe2", txtRecipe2.Text)
            cmd.Parameters.AddWithValue("@itemRecipe3", txtRecipe3.Text)
            cmd.Parameters.AddWithValue("@itemShop", txtShop.Text)
            cmd.Parameters.AddWithValue("@itemBonus", txtBonus.Text)
            cmd.Parameters.AddWithValue("@pictureName", txtPictureN.Text)

            Dim ms As New MemoryStream()

            pbItemAdd.BackgroundImage.Save(ms, pbItemAdd.BackgroundImage.RawFormat)

            Dim data As Byte() = ms.GetBuffer()
            Dim p As New SqlParameter("@itemPicture", SqlDbType.Image)
            p.Value = data


this is the code i use to retrieve

 Dim SQL As String = "INSERT @itemName, @itemCost, @itemRecipe1, @itemRecipe2, @itemRecipe3, @itemShop, @itemBonus, @pictureName, @itemPicture FROM item"
        cmd = New SqlClient.SqlCommand(SQL, sqlcon)
        pbItemAdd.ImageLocation = cmd.ExecuteScalar.ToString

but i get an error saying "Must declare the scalar variable "@itemName"." and i get this error at "pbItemAdd.ImageLocation = cmd.ExecuteScalar.ToString"
I dont even know if this code is correct.
can someone please help me and tell me what im doing wrong here and what i should actually do

In your retrieval SQL statement you are using an Insert Statement not a Select statement...

Also, cmd.ExecuteScalar is for running queries that return a single scalar value, it would appear you wish to return information other than the picture as well.

Once you get the picture out of the SQL statement, you will have it in the form of a byte stream so you will have to stream the data back into a picturebox or some such object.

This article has been dead for over six months. Start a new discussion instead.