Displaying an image from my database
Hello,
How do i get this code to display my image using a database this is my code.
code Code:
Private Sub Details()
Dim oleDBC As New OleDbCommand
Dim OLEDBDR As OleDbDataReader
con.Open()
With oleDBC
.Connection = con
.CommandText = "SELECT UserID,imagepath FROM Users"
End With
OLEDBDR = oleDBC.ExecuteReader
If OLEDBDR.HasRows Then
While OLEDBDR.Read
lblName.Text = (OLEDBDR.Item(0))
PictureBox1.Image = (OLEDBDR.Item(1))
End While
con.Close()
End If
End Sub
I get an error saying
Unable to cast object of type 'System.String' to type 'System.Drawing.Image'.
but if i use lblName.Text = (OLEDBDR.Item(1)) it will display the path to the image.?
Re: Displaying an image from my database
I could be mistaken but you may need to load your image from the database into an array of bytes, then convert it into an image.
But if it's the path you're saving and not the actual image then use..
vbnet Code:
While OLEDBDR.Read
lblName.Text = (OLEDBDR.Item(0))
PictureBox1.Image = [B]Bitmap.FromFile((OLEDBDR.Item(1)))[/B]
End While
You also may want to double check to be sure the files exists first.
EDIT: Why don't the bold tags work inside of the code highlight tags....? *Grunts*
Re: Displaying an image from my database
I'm trying to get the person who is login in to display on frm this is how i login to the database.
code Code:
Imports System.Data.OleDb
Public Class frmLogin
Dim fullpath As String = AppDomain.CurrentDomain.BaseDirectory + "Data\db.mdb"
Dim constring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fullpath + ";Persist Security Info=False;Jet OLEDB:Database Password=d82f1fc"
Dim con As New OleDbConnection(constring) 'Connection Ready to Use
Private Sub LoadUsers()
con.Open()
Dim OledBC As New OleDbCommand
Dim OleDBDR As OleDbDataReader
With OledBC
.Connection = con
.CommandText = "SELECT UserID FROM Users"
End With
OleDBDR = OledBC.ExecuteReader
cboUsername.Items.Clear()
If OleDBDR.HasRows Then
While OleDBDR.Read
cboUsername.Items.Add(OleDBDR.Item(0))
End While
End If
con.Close()
End Sub
Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call LoadUsers()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
con.Open()
Dim OleDBC As New OleDbCommand
Dim OleDBDR As OleDbDataReader
With OleDBC
.Connection = con
.CommandText = "SELECT * FROM Users " & _
"WHERE UserID='" & _
cboUsername.Text & "' AND " & _
"Upass='" & _
txtPassword.Text & "'"
End With
OleDBDR = OleDBC.ExecuteReader
OleDBDR.Read()
If OleDBDR.HasRows Then
If OleDBDR.Item(3) = "Administrator" Then
MsgBox("Access Granted", MsgBoxStyle.Information, "SUCCESS")
Me.Hide()
frmMain.Show()
Else
MsgBox("Access Granted", MsgBoxStyle.Information, "SUCCESS")
Me.Hide()
frmMain.Show()
End If
Else
MsgBox("Access Denied", MsgBoxStyle.Critical, "ERROR")
End If
cboUsername.Text = ""
txtPassword.Text = ""
txtPassword.Focus()
con.Close()
End Sub
End Class
Re: Displaying an image from my database
Quote:
Originally Posted by
DavesChillaxin
I could be mistaken but you may need to load your image from the database into an array of bytes, then convert it into an image.
But if it's the path you're saving and not the actual image then use..
vbnet Code:
While OLEDBDR.Read
lblName.Text = (OLEDBDR.Item(0))
PictureBox1.Image = [B]Bitmap.FromFile((OLEDBDR.Item(1)))[/B]
End While
You also may want to double check to be sure the files exists first.
EDIT: Why don't the bold tags work inside of the code highlight tags....? *Grunts*
I tried that but when i login with admin account it shows the image and the name admin which i logged in with.
But when i login with another account it dosen't change the picture or the name Admin.?
Re: Displaying an image from my database
Quote:
Originally Posted by
Jamie_Garland
I'm trying to get the person who is login in to display on frm this is how i login to the database.
code Code:
Imports System.Data.OleDb
Public Class frmLogin
Dim fullpath As String = AppDomain.CurrentDomain.BaseDirectory + "Data\db.mdb"
Dim constring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fullpath + ";Persist Security Info=False;Jet OLEDB:Database Password=d82f1fc"
Dim con As New OleDbConnection(constring) 'Connection Ready to Use
Private Sub LoadUsers()
con.Open()
Dim OledBC As New OleDbCommand
Dim OleDBDR As OleDbDataReader
With OledBC
.Connection = con
.CommandText = "SELECT UserID FROM Users"
End With
OleDBDR = OledBC.ExecuteReader
cboUsername.Items.Clear()
If OleDBDR.HasRows Then
While OleDBDR.Read
cboUsername.Items.Add(OleDBDR.Item(0))
End While
End If
con.Close()
End Sub
Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call LoadUsers()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
con.Open()
Dim OleDBC As New OleDbCommand
Dim OleDBDR As OleDbDataReader
With OleDBC
.Connection = con
.CommandText = "SELECT * FROM Users " & _
"WHERE UserID='" & _
cboUsername.Text & "' AND " & _
"Upass='" & _
txtPassword.Text & "'"
End With
OleDBDR = OleDBC.ExecuteReader
OleDBDR.Read()
If OleDBDR.HasRows Then
If OleDBDR.Item(3) = "Administrator" Then
MsgBox("Access Granted", MsgBoxStyle.Information, "SUCCESS")
Me.Hide()
frmMain.Show()
Else
MsgBox("Access Granted", MsgBoxStyle.Information, "SUCCESS")
Me.Hide()
frmMain.Show()
End If
Else
MsgBox("Access Denied", MsgBoxStyle.Critical, "ERROR")
End If
cboUsername.Text = ""
txtPassword.Text = ""
txtPassword.Focus()
con.Close()
End Sub
End Class
Well I'm wondering is what way or form are you storing the images in your database? Are you just storing the file path or are you actually storing the image.
If your storing the path, then just use Bitmap.FromFile("") or if it's an image then you'll probably need to load it from the database in a byte array which can be converted to an image(I think..)
Re: Displaying an image from my database
i'm storing just the files path.
But when i try to login with admin account it works ok but when i try to login with another account it dosent change just displays the same name.
Re: Displaying an image from my database
What i'm trying to do is show the records information i used to login from frmLogin to frmMain.
Re: Displaying an image from my database
First, this part here confuses me, really both do exactly the same, and occurs every time whether or not it's the admin or not.
vb Code:
If OleDBDR.Item(3) = "Administrator" Then
MsgBox("Access Granted", MsgBoxStyle.Information, "SUCCESS")
Me.Hide()
frmMain.Show()
Else
MsgBox("Access Granted", MsgBoxStyle.Information, "SUCCESS")
Me.Hide()
frmMain.Show()
End If
It should just be..
vb Code:
If OleDBDR.HasRows Then
MsgBox("Access Granted", MsgBoxStyle.Information, "SUCCESS")
Me.Hide()
frmMain.Show()
Else
MsgBox("Access Denied", MsgBoxStyle.Critical, "ERROR")
End If
Secondly, how are you storing the UserID value? and where are you setting it?
The main focus however I feel is here...
vb Code:
Private Sub Details()
Dim oleDBC As New OleDbCommand
Dim OLEDBDR As OleDbDataReader
con.Open()
With oleDBC
.Connection = con
.CommandText = "SELECT UserID,imagepath FROM Users"
End With
OLEDBDR = oleDBC.ExecuteReader
If OLEDBDR.HasRows Then
While OLEDBDR.Read
lblName.Text = (OLEDBDR.Item(0))
PictureBox1.Image = (OLEDBDR.Item(1))
End While
con.Close()
End If
End Sub
I feel your sql command should only be grabbing a single data row, where here your grabbing them all and your reading them all, so in result your last data row read will be displayed. Try changing the sql command to this...
"SELECT UserID,imagepath FROM Users WHERE UserID =" & XX
Replace the 'XX' with the string to compare with the UserID in the database you wish to return. This way only 1 record or none will be read.
Re: Displaying an image from my database
Hello,
What i done is went on to the login page and typed the following code and it now seems to work fine.
vb Code:
Private Sub cboUsername_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboUsername.SelectedIndexChanged
con.Open()
Dim OleDBC As New OleDbCommand
Dim OleDBDR As OleDbDataReader
With OleDBC
.Connection = con
.CommandText = "SELECT * FROM Users WHERE UserID='" & cboUsername.Text & "'"
End With
OleDBDR = OleDBC.ExecuteReader
If OleDBDR.HasRows Then
While OleDBDR.Read
frmMain.lblName.Text = (OleDBDR.Item(1))
frmMain.PictureBox1.Image = Bitmap.FromFile((OleDBDR.Item(4)))
End While
End If
con.Close()
End Sub
Re: Displaying an image from my database
yup, just as I suggested. Your command before was grabbing all rows when read, would always result in displaying of the final row. Now your only selecting the appropriate row(a single row) and displaying it :)
Re: Displaying an image from my database
is there any other way i can do it on from load rather than using the combo way?.
Re: Displaying an image from my database
Update your Details sub to function properly and use that. Just be sure your user information is loaded before doing anything with the main form.. which should be ok if you load that when your log in form loads
But first you'll need to pass which user has logged in into your main form.
Under your main form you could have a property such as..
vbnet Code:
Private _User As String = String.Empty
Public Property User As String
Set
Return Me._User
End Set
Get(ByVal value As String)
Me._User = value
Call Details(value)
End Get
End Property
vbnet Code:
Private Sub Details(ByVal User As String)
Dim oleDBC As New OleDbCommand
Dim OLEDBDR As OleDbDataReader
con.Open()
With oleDBC
.Connection = con
.CommandText = "SELECT * FROM Users WHERE UserID='" & User & "'"
End With
OLEDBDR = oleDBC.ExecuteReader
If OLEDBDR.HasRows Then
While OLEDBDR.Read
lblName.Text = (OLEDBDR.Item(1))
PictureBox1.Image = Bitmap.FromFile(OleDBDR.Item(4))
End While
con.Close()
End If
End Sub
Now when your button is pressed to log in and you load the main form. First initialize your main form, then before showing it set that User property to the value of your combobox. Finally show your main form, then close your log in form