PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2008 Displaying an image from my database-VBForums
Results 1 to 12 of 12

Thread: Displaying an image from my database

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    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:
    1. Private Sub Details()
    2.         Dim oleDBC As New OleDbCommand
    3.         Dim OLEDBDR As OleDbDataReader
    4.         con.Open()
    5.         With oleDBC
    6.             .Connection = con
    7.             .CommandText = "SELECT UserID,imagepath FROM Users"
    8.         End With
    9.         OLEDBDR = oleDBC.ExecuteReader
    10.         If OLEDBDR.HasRows Then
    11.  
    12.             While OLEDBDR.Read
    13.                 lblName.Text = (OLEDBDR.Item(0))
    14.                 PictureBox1.Image = (OLEDBDR.Item(1))
    15.             End While
    16.  
    17.  
    18.            
    19.             con.Close()
    20.         End If
    21.     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.?
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

  2. #2
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    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:
    1. While OLEDBDR.Read
    2.     lblName.Text = (OLEDBDR.Item(0))
    3.     PictureBox1.Image = [B]Bitmap.FromFile((OLEDBDR.Item(1)))[/B]
    4. 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*
    Last edited by DavesChillaxin; Feb 9th, 2012 at 04:29 PM.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    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:
    1. Imports System.Data.OleDb
    2.  
    3. Public Class frmLogin
    4.  
    5.     Dim fullpath As String = AppDomain.CurrentDomain.BaseDirectory + "Data\db.mdb"
    6.     Dim constring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fullpath + ";Persist Security Info=False;Jet OLEDB:Database Password=d82f1fc"
    7.     Dim con As New OleDbConnection(constring) 'Connection Ready to Use
    8.  
    9.  
    10.     Private Sub LoadUsers()
    11.         con.Open()
    12.         Dim OledBC As New OleDbCommand
    13.         Dim OleDBDR As OleDbDataReader
    14.         With OledBC
    15.             .Connection = con
    16.             .CommandText = "SELECT UserID FROM Users"
    17.         End With
    18.         OleDBDR = OledBC.ExecuteReader
    19.         cboUsername.Items.Clear()
    20.         If OleDBDR.HasRows Then
    21.  
    22.             While OleDBDR.Read
    23.                 cboUsername.Items.Add(OleDBDR.Item(0))
    24.             End While
    25.  
    26.         End If
    27.         con.Close()
    28.     End Sub
    29.  
    30.     Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    31.         Call LoadUsers()
    32.     End Sub
    33.  
    34.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    35.         con.Open()
    36.         Dim OleDBC As New OleDbCommand
    37.         Dim OleDBDR As OleDbDataReader
    38.         With OleDBC
    39.             .Connection = con
    40.             .CommandText = "SELECT * FROM Users " & _
    41.             "WHERE UserID='" & _
    42.             cboUsername.Text & "' AND " & _
    43.             "Upass='" & _
    44.             txtPassword.Text & "'"
    45.         End With
    46.  
    47.         OleDBDR = OleDBC.ExecuteReader
    48.         OleDBDR.Read()
    49.  
    50.         If OleDBDR.HasRows Then
    51.             If OleDBDR.Item(3) = "Administrator" Then
    52.                 MsgBox("Access Granted", MsgBoxStyle.Information, "SUCCESS")
    53.                 Me.Hide()
    54.                 frmMain.Show()
    55.             Else
    56.                 MsgBox("Access Granted", MsgBoxStyle.Information, "SUCCESS")
    57.                 Me.Hide()
    58.                 frmMain.Show()
    59.             End If
    60.         Else
    61.             MsgBox("Access Denied", MsgBoxStyle.Critical, "ERROR")
    62.         End If
    63.         cboUsername.Text = ""
    64.         txtPassword.Text = ""
    65.         txtPassword.Focus()
    66.         con.Close()
    67.     End Sub
    68. End Class
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: Displaying an image from my database

    Quote Originally Posted by DavesChillaxin View Post
    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:
    1. While OLEDBDR.Read
    2.     lblName.Text = (OLEDBDR.Item(0))
    3.     PictureBox1.Image = [B]Bitmap.FromFile((OLEDBDR.Item(1)))[/B]
    4. 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.?
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

  5. #5
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Displaying an image from my database

    Quote Originally Posted by Jamie_Garland View Post
    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:
    1. Imports System.Data.OleDb
    2.  
    3. Public Class frmLogin
    4.  
    5.     Dim fullpath As String = AppDomain.CurrentDomain.BaseDirectory + "Data\db.mdb"
    6.     Dim constring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fullpath + ";Persist Security Info=False;Jet OLEDB:Database Password=d82f1fc"
    7.     Dim con As New OleDbConnection(constring) 'Connection Ready to Use
    8.  
    9.  
    10.     Private Sub LoadUsers()
    11.         con.Open()
    12.         Dim OledBC As New OleDbCommand
    13.         Dim OleDBDR As OleDbDataReader
    14.         With OledBC
    15.             .Connection = con
    16.             .CommandText = "SELECT UserID FROM Users"
    17.         End With
    18.         OleDBDR = OledBC.ExecuteReader
    19.         cboUsername.Items.Clear()
    20.         If OleDBDR.HasRows Then
    21.  
    22.             While OleDBDR.Read
    23.                 cboUsername.Items.Add(OleDBDR.Item(0))
    24.             End While
    25.  
    26.         End If
    27.         con.Close()
    28.     End Sub
    29.  
    30.     Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    31.         Call LoadUsers()
    32.     End Sub
    33.  
    34.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    35.         con.Open()
    36.         Dim OleDBC As New OleDbCommand
    37.         Dim OleDBDR As OleDbDataReader
    38.         With OleDBC
    39.             .Connection = con
    40.             .CommandText = "SELECT * FROM Users " & _
    41.             "WHERE UserID='" & _
    42.             cboUsername.Text & "' AND " & _
    43.             "Upass='" & _
    44.             txtPassword.Text & "'"
    45.         End With
    46.  
    47.         OleDBDR = OleDBC.ExecuteReader
    48.         OleDBDR.Read()
    49.  
    50.         If OleDBDR.HasRows Then
    51.             If OleDBDR.Item(3) = "Administrator" Then
    52.                 MsgBox("Access Granted", MsgBoxStyle.Information, "SUCCESS")
    53.                 Me.Hide()
    54.                 frmMain.Show()
    55.             Else
    56.                 MsgBox("Access Granted", MsgBoxStyle.Information, "SUCCESS")
    57.                 Me.Hide()
    58.                 frmMain.Show()
    59.             End If
    60.         Else
    61.             MsgBox("Access Denied", MsgBoxStyle.Critical, "ERROR")
    62.         End If
    63.         cboUsername.Text = ""
    64.         txtPassword.Text = ""
    65.         txtPassword.Focus()
    66.         con.Close()
    67.     End Sub
    68. 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..)
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    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.
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    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.
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

  8. #8
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    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:
    1. If OleDBDR.Item(3) = "Administrator" Then
    2.     MsgBox("Access Granted", MsgBoxStyle.Information, "SUCCESS")
    3.     Me.Hide()
    4.     frmMain.Show()
    5. Else
    6.     MsgBox("Access Granted", MsgBoxStyle.Information, "SUCCESS")
    7.     Me.Hide()
    8.     frmMain.Show()
    9. End If

    It should just be..

    vb Code:
    1. If OleDBDR.HasRows Then
    2.     MsgBox("Access Granted", MsgBoxStyle.Information, "SUCCESS")
    3.     Me.Hide()
    4.     frmMain.Show()
    5. Else
    6.     MsgBox("Access Denied", MsgBoxStyle.Critical, "ERROR")
    7. 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:
    1. Private Sub Details()
    2.     Dim oleDBC As New OleDbCommand
    3.     Dim OLEDBDR As OleDbDataReader
    4.     con.Open()
    5.     With oleDBC
    6.         .Connection = con
    7.         .CommandText = "SELECT UserID,imagepath FROM Users"
    8.     End With
    9.     OLEDBDR = oleDBC.ExecuteReader
    10.     If OLEDBDR.HasRows Then
    11.  
    12.         While OLEDBDR.Read
    13.             lblName.Text = (OLEDBDR.Item(0))
    14.             PictureBox1.Image = (OLEDBDR.Item(1))
    15.         End While
    16.  
    17.     con.Close()
    18.     End If
    19. 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.
    Last edited by DavesChillaxin; Feb 9th, 2012 at 05:25 PM.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    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:
    1. Private Sub cboUsername_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboUsername.SelectedIndexChanged
    2.         con.Open()
    3.         Dim OleDBC As New OleDbCommand
    4.         Dim OleDBDR As OleDbDataReader
    5.         With OleDBC
    6.             .Connection = con
    7.             .CommandText = "SELECT * FROM Users WHERE UserID='" & cboUsername.Text & "'"
    8.         End With
    9.         OleDBDR = OleDBC.ExecuteReader
    10.         If OleDBDR.HasRows Then
    11.             While OleDBDR.Read
    12.                 frmMain.lblName.Text = (OleDBDR.Item(1))
    13.                 frmMain.PictureBox1.Image = Bitmap.FromFile((OleDBDR.Item(4)))
    14.             End While
    15.  
    16.         End If
    17.         con.Close()
    18.     End Sub
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

  10. #10
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    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
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    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?.
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

  12. #12
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    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:
    1. Private _User As String = String.Empty
    2. Public Property User As String
    3.     Set
    4.         Return Me._User
    5.     End Set
    6.     Get(ByVal value As String)
    7.         Me._User = value
    8.         Call Details(value)
    9.     End Get
    10. End Property


    vbnet Code:
    1. Private Sub Details(ByVal User As String)
    2.     Dim oleDBC As New OleDbCommand
    3.     Dim OLEDBDR As OleDbDataReader
    4.  
    5.     con.Open()
    6.     With oleDBC
    7.         .Connection = con
    8.         .CommandText = "SELECT * FROM Users WHERE UserID='" & User & "'"
    9.     End With
    10.     OLEDBDR = oleDBC.ExecuteReader
    11.     If OLEDBDR.HasRows Then
    12.  
    13.         While OLEDBDR.Read
    14.             lblName.Text = (OLEDBDR.Item(1))
    15.             PictureBox1.Image = Bitmap.FromFile(OleDBDR.Item(4))
    16.         End While
    17.  
    18.         con.Close()
    19.     End If
    20. 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
    Last edited by DavesChillaxin; Feb 9th, 2012 at 06:25 PM.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width