0

I just want some help for my project which is RFID attendance system. The RFID tag should be scan to retrieve the information and Image from the database mysql. The first student will scan his RFID tag to display his picture in picturebox1 and when another student scan the picturebox1 will be updated to a new student picture and data and the previous one is on the picturebox2 displayed The issue here is how can I display the previous Image of the student. Just only the Image ,the data is not included.

I have a 2 picturebox

Picturebox1 is for new student scanned Picturebox2 is for the previous one or the student who scan before the new.

Thank you guys ..Any suggestions and comment will totally appreciated

Here is my code

Private Sub studtag_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles studtag.TextChanged
    If studtag.TextLength = 8 Then


        con = New MySqlConnection
        con.ConnectionString = "server=localhost;userid=root;password=1234;database=dat"
        Dim query As String

        query = "select * from dat.students"
        cmd = New MySqlCommand(query, con)



        Dim table As New DataTable

        Try
            con.Open()
            'Gets or sets an SQL statement or stored procedure used to select records in the database.
            With cmd
                .Connection = con
                .CommandText = "SELECT * from students where `studtags`='" & studtag.Text & "';"
            End With
            da.SelectCommand = cmd
            da.Fill(table)
            'it gets the data from specific column and fill it into textbox
            studtag.Text = table.Rows(0).Item(0)
            idno.Text = table.Rows(0).Item(1)
            lastxt.Text = table.Rows(0).Item(2)
            firstxt.Text = table.Rows(0).Item(3)
            middletxt.Text = table.Rows(0).Item(4)
            dob.Text = table.Rows(0).Item(6)

            crsetxt.Text = table.Rows(0).Item(10)

            tagtxt.Text = studtag.Text
            timein.Text = times.Text

            dr = cmd.ExecuteReader()
            dr.Read()

            If dob.Text = datenow.Text Then
                greet.Text = "Happy Birthday To You"

            End If



            Dim img() As Byte = CType(dr("studpic"), Byte())


            Using ms As New IO.MemoryStream(img)
                PictureBox1.Image = Image.FromStream(ms)

            End Using
            insert()
            loadtable()

        Catch ex As Exception
            Notenrolled.Show()
        Finally

            con.Dispose()
            con.Close()
        End Try

    End If

End Sub

Public Sub loadtable()

    con = New MySqlConnection
    con.ConnectionString = "server=localhost;userid=root;password=1234;database=dat"
    Dim SDA As New MySqlDataAdapter
    Dim dbDataset As New DataTable
    Dim bSource As New BindingSource


    Try

        con.Open()
        Dim query3 As String

        query3 = "select studtags,idno,lastxt,firstxt,middletxt,dob,log,timein,crse from dat.studlogs"
        cmd = New MySqlCommand(query3, con)
        SDA.SelectCommand = cmd
        SDA.Fill(dbDataset)
        bSource.DataSource = dbDataset
        DataGridView1.DataSource = bSource
        SDA.Update(dbDataset)
        DataGridView1.Sort(DataGridView1.Columns(8), System.ComponentModel.ListSortDirection.Ascending)
        If dbDataset.Rows.Count > 0 Then
            logins.Text = table2.Rows.Count.ToString()
        End If


        con.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        con.Dispose()
    End Try
End Sub

 Private Sub Students_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    loadtable()
    ''Date Now 
    Timer2.Start()


    Try
        DataGridView1.AllowUserToAddRows = False ' Disabled or hide (*) Symbol...

        DataGridView1.RowHeadersVisible = False 'To hide Left indicator..
        DataGridView1.DefaultCellStyle.SelectionBackColor = Color.SteelBlue  'Selection backcolor....
        DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGoldenrodYellow 'Alternating Backcolor.
        DataGridView1.AllowUserToResizeRows = False 'Disabled  row resize...
        DataGridView1.ReadOnly = True
        DataGridView1.MultiSelect = False
        DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridView1.ShowRowErrors = False
        DataGridView1.ShowCellErrors = False


        table2.Columns.Add("Student Tag", Type.GetType("System.String"))
        table2.Columns.Add("Student ID", Type.GetType("System.Int32"))
        table2.Columns.Add("Last Name", Type.GetType("System.String"))
        table2.Columns.Add("First Name", Type.GetType("System.String"))
        table2.Columns.Add("Middle Name", Type.GetType("System.String"))

        table2.Columns.Add("Status", Type.GetType("System.String"))
        table2.Columns.Add("Birthday", Type.GetType("System.String"))
        table2.Columns.Add("Time in", Type.GetType("System.String"))
        table2.Columns.Add("Course/Sec", Type.GetType("System.String"))


    Catch ex As Exception

    End Try
djv
  • 15,168
  • 7
  • 48
  • 72
Nathan West
  • 165
  • 1
  • 2
  • 10
  • Most of that code seems not related to the stated problem. I cant see anywhere that you are trying to move the image from one to the other. I'd use a variable and just assign it to the other PB...be sure to dispose of the ones you no longer need. – Ňɏssa Pøngjǣrdenlarp Nov 21 '17 at 18:51
  • @Plutonix I'm sorry sir I don't know where to start , on how to transfer the Picturebox1 image to picturebox2 everytime the new student scan. – Nathan West Nov 21 '17 at 19:00
  • Your code creates an Image from the bytes and directly assigns it to PB1. Instead of that, assign it to an Image variable so you can set PB2.Image to it when you need to. it might be easier to declare 2 img vars: `currentStudentImg` and `prevStudentImg` to make the code easier to read. Your app is already leaking so be sure to dispose of them – Ňɏssa Pøngjǣrdenlarp Nov 21 '17 at 19:07

1 Answers1

2

It's quiet easy,You can use a button to display the previous image :

  If datagridview1.CurrentRow.Index < datagridview1.Rows.Count Then
  datagridview1.Rows(datagridview1.SelectedRows(0).Index - 1).Selected 
             = True
 Dim pic As Byte()
 pic = datagridview1.CurrentRow.Cells(1).Value
 Dim ms As New MemoryStream(pic)
 pbox1.BackgroundImage = Image.FromStream(ms) 
Software Dev
  • 5,368
  • 5
  • 22
  • 45
  • Thank you sir But I've got an error which is Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index (ArgumentOutofRangeException was unhandled) – Nathan West Nov 22 '17 at 06:39
  • 1
    It's easy to fix, just tell me which line is showing the error – Software Dev Nov 22 '17 at 07:46
  • 1
    Anyway...the easy fix is...try to fi out which line gives the error...after finding out the line, make sure your connection string and datagridview population codes are before the line – Software Dev Nov 22 '17 at 07:56
  • In this line sir zack DataGridView1.Rows(DataGridView1.SelectedRows(0).Index - 1).Selected = True and I already assign it to a button1 when i click the error is pop up sir – Nathan West Nov 22 '17 at 08:48
  • 1
    okey,,,now tell me, do u click the button after the datagridview is already populated or before the dgvw population ? if you say before, then the error will occur..anyway if the error occurs after the dgvw is populated, just add a `Try-Catch` statement..dont use `Msgbox(ex.messgae)`, just use an empty catch statement – Software Dev Nov 22 '17 at 12:36
  • 1
    and please remember,this code goes back to the previous row of the datagridview, make sure that you are not in the first row , because if u are already in the first row, how can u even go to the previous row ? the `Empt TRY-CATCH` statement will get rid of the error but it won't fix it..so make sure u r atleast in the second row – Software Dev Nov 22 '17 at 12:39
  • Yes sir there are 5 rows in the datagridview before I add and run your code – Nathan West Nov 22 '17 at 13:06
  • 1
    make sure that atleast the 2nd row is selected , then you'ld be able to go to the previous row...and use the empty TRY CATCH statement to get rid of the error – Software Dev Nov 22 '17 at 13:11
  • Sir zack I already try just now...I run the code with a 1 button and datagridview with try catch which is already populated by 5 rows and columns.Before I click the button i selected the 2nd row when i click the button the selected rows turns to the top or on the first row and nothing displayed...and i try to put the messagebox ex and same output but the error displayed "Unable to cast object of type System.Int32 to type 'System.Byte[]'" Thanks sir – Nathan West Nov 22 '17 at 13:20
  • 1
    wich cell is the picture cell ? – Software Dev Nov 22 '17 at 13:36
  • 1
    it seems like cell(1) is not the cell that has the image in it...find out which cell contains the image and change the cell number – Software Dev Nov 22 '17 at 13:37
  • By the way sir Can I ask that I must populate also the image inside the datagridview? – Nathan West Nov 22 '17 at 13:38
  • You are amazing sir zack ..Thumbs up for you It worked :D – Nathan West Nov 22 '17 at 13:38
  • 1
    please upvote the question , my comments as well and mark my answer as answer – Software Dev Nov 22 '17 at 13:42
  • you are welcome...And hey,,if u need any help,just ask for it...and u can even help me too...i'm just 17 years old...if u have any work for me, i'm willing to do it – Software Dev Nov 22 '17 at 13:44