displaying images in vb from Database

timtom1

Member
Joined
Jun 12, 2006
Messages
24
Programming Experience
Beginner
I have this script but my database connection works I think my SQL is sound but I am not sure what RS is and also how to get the image to display in a picture box called TextBoxImageID when you enter a image ID in TextBoxImageID

Hope this makes sence

VB.NET:
Private[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button1.Click[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] conn [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] MySqlConnection[/SIZE]
[SIZE=2]conn = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] MySqlConnection()[/SIZE]
[SIZE=2][COLOR=#008000]' connect to db[/COLOR][/SIZE]
[SIZE=2]conn.ConnectionString = [/SIZE][SIZE=2][COLOR=#800000]"server=localhost;"[/COLOR][/SIZE][SIZE=2] & _[/SIZE]
[SIZE=2][COLOR=#800000]"user id=********"[/COLOR][/SIZE][SIZE=2] & _[/SIZE]
[SIZE=2][COLOR=#800000]"password=********"[/COLOR][/SIZE][SIZE=2] & _[/SIZE]
[SIZE=2][COLOR=#800000]"database=********"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rs[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] TextBoxImageID, strQuery()[/SIZE]
[SIZE=2]TextBoxImageID = Request([/SIZE][SIZE=2][COLOR=#800000]"Image Id"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] TextBoxImageID = [/SIZE][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2] TextBoxImageID = 0[/SIZE]
[SIZE=2]rs = Server.CreateObject([/SIZE][SIZE=2][COLOR=#800000]"ADODB.Recordset"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#008000]'open db connection[/COLOR][/SIZE]
[SIZE=2]conn.Open()[/SIZE]
[SIZE=2][COLOR=#008000]'Get the specific image based on the ID passed in a querystring[/COLOR][/SIZE]
[SIZE=2]strQuery = [/SIZE][SIZE=2][COLOR=#800000]"SELECT image FROM image where img_id ="[/COLOR][/SIZE][SIZE=2] & TextBoxImageID[/SIZE]
[SIZE=2]rs.Open(strQuery, conn, 3, 3)[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] rs.eof [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'No records found[/COLOR][/SIZE]
[SIZE=2]Response.End()[/SIZE]
[SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'Display the contents[/COLOR][/SIZE]
[SIZE=2]Response.ContentType = [/SIZE][SIZE=2][COLOR=#800000]"image/*.jpg"[/COLOR][/SIZE]
[SIZE=2]Response.BinaryWrite(rs([/SIZE][SIZE=2][COLOR=#800000]"image"[/COLOR][/SIZE][SIZE=2]))[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'destroy the variables.[/COLOR][/SIZE]
[SIZE=2]rs.Close()[/SIZE]
[SIZE=2]rs = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'close connection[/COLOR][/SIZE]
[SIZE=2]conn.Dispose()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE]
 
try this

Your code is using the COM object ADODB.Recordset and the .Net provider for mySql.
I would go with a pure .Net solution and eliminate the RS (it's an untyped ADODB.Recordset)

Basically its the same as you find googling mysql and images in asp pages.

Dim oConn AsNew MySqlConnection("server=YOURSERVER;user id=YOURUSER;password=YOURPASSWORD;database=YOURDB;")
Dim oAdapter AsNew MySqlDataAdapter("SELECT image FROM image where img_id =" & TextBoxImageID, oConn)
Dim oDataSet AsNew DataSet
oConn.Open() 'optional
oAdapter.Fill(oDataSet, "TheTable")
oConn.Close() 'optional
oConn = Nothing'optional
Dim oRow As DataRow = oDataSet.Tables("image").Rows(0)
Dim vData() AsByte = CType(oRow("Data"), Byte())
With Response
.Buffer = True
.ContentType = "Image/JPEG"
.BinaryWrite(vData)
.End()
EndWith
 
Back
Top