0

I have a VB.NET project with 2 WinForms. Now in Form 1, I added a PictureBox, a Button, a OpenFileDialog. I have coded the button to add the picture from PictureBox to sql database. Now I have another picturebox in Form 2.

So my question is, how do I retrieve the image from the sql database and show it in the picture box?

The connection string is as follows :

Dim con As New SqlConnection
con.ConnectionString = "Data source=" & My.Settings.sqlserver & "," & My.Settings.sqlport &
                       ";Network Library=DBMSSOCN;initial catalog=" & My.Settings.dbname & 
                       ";User id=" & My.Settings.Username &
                       ";Password=" & My.Settings.Password & ";"
Dim cmd As New SqlCommand("select * from userandadmins where Username = @username and Password = @password", con)
con.Open()
boop_the_snoot
  • 3,209
  • 4
  • 33
  • 44

1 Answers1

0

I use Oracle to store photos. You need to retrieve the photo with a out parameter that is of the same type as the field the photo is stored in. I assume it's a BLOB. Before storing the photo in the database you have to convert it to a byte array. As that wasn't the question here, I'll post the code I use to retrieve the photo from my Oracle database, it should be a simple matter to convert to Sql Server code.

I open a connection to the database and pass that to the retrieve function. I return a dataset that has one row of data, the photo and other identifying information. I then assign the BLOB to a variable, from there do what you need. What I do with it is assign it to an image control which calls another aspx page to retrieve the photo (imageURL). The code to retrieve it is in the called "Image.aspx", which then returns the photo to the image control. The html for that is below the GetPhoto Function. This way I do not need to store the photo anywhere on the hard drive and retrieve it.

Dim MyConnection As New OracleConnection
Dim MyDataSet As New DataSet
Dim MyPhoto() As Byte = {}

MyConnection = OpenConnection(Session("USERNAME"), Session("PASSWORD"))

MyDataSet = GetPhoto(MyConnection, pPhotoID)

myPhoto = MyDataSet.Tables("Data").Rows(0)("Photo")

Public Function GetPhoto(ByVal TheConnection As OracleConnection, ByVal pPhotoID As String) As DataSet
    Dim myCommand As New OracleCommand
    Dim DS As New DataSet
    Try
        With myCommand
            .Connection = TheConnection
            .CommandText = "Darlington.PlayerSignUps.GetPhoto"
            .CommandType = CommandType.StoredProcedure
            .Parameters.Clear()
            .Parameters.Add(New OracleParameter("pPhotoID", OracleDbType.Varchar2, pPlayerID.Length)).Value = pPhotoID 
            .Parameters.Add(New OracleParameter("pDataOut", OracleDbType.RefCursor))
            .Parameters(0).Direction = ParameterDirection.Input
            .Parameters(1).Direction = ParameterDirection.Output
            Dim DA As New OracleDataAdapter(myCommand)
            DA.Fill(DS, "DATA")
            GetPhoto = DS
        End With
    Catch exc As Exception
        Throw New Exception("Error occured while retreiving photo from database, the error is: " & exc.Message)
    End Try
    myCommand = Nothing

End Function

<tr>
    <td style="width: 372px; text-align: center">
        <asp:Image ID="Image_Photo" runat="server" ImageUrl="Image.aspx" />
    </td>
</tr>
Prescott Chartier
  • 1,519
  • 3
  • 17
  • 34