Private Sub Load_Faculty()

        Dim con As New OleDbConnection


    Dim cmd As New OleDbCommand
    Dim dr As OleDbDataReader
    Dim arrImage() As Byte
    Dim myMS As New IO.MemoryStream
    Dim sSQL As String = String.Empty
    Try
        sSQL = "SELECT b.FacultyID, b.Image, b.Course, a.Course"
        sSQL = sSQL & " FROM studentsrecords as a left join faculty as b "
        sSQL = sSQL & " on a.ID = b.FacultyID WHERE(a.Course = @Course) order by a.Course"

        con = New OleDbConnection(Get_Constring)
        con.Open()
        cmd.Connection = con
        cmd.CommandType = CommandType.Text
        cmd.CommandText = sSQL
        ' cmd.Parameters.Add("@EvaluationID", OleDbType.DBDate).Value

        dr = cmd.ExecuteReader
        If dr.HasRows Then
            While dr.Read
                If Not IsDBNull(dr("Image")) Then
                    myMS = New IO.MemoryStream
                    arrImage = dr("Image")
                    For Each ar As Byte In arrImage
                        myMS.WriteByte(ar)
                    Next
                End If
                Select Case dr("Course")
                    Case 1
                        Me.pic1.Tag = dr("FacultyID")
                        Me.pic1.Image = System.Drawing.Image.FromStream(myMS)
                    Case 2
                        Me.pic2.Tag = dr("FacultyID")
                        Me.pic2.Image = System.Drawing.Image.FromStream(myMS)
                    Case 3
                        Me.pic3.Tag = dr("FacultyID")
                        Me.pic3.Image = System.Drawing.Image.FromStream(myMS)
                    Case 4
                        Me.pic4.Tag = dr("FacultyID")
                        Me.pic4.Image = System.Drawing.Image.FromStream(myMS)
                    Case 5
                        Me.pic5.Tag = dr("FacultyID")
                        Me.pic5.Image = System.Drawing.Image.FromStream(myMS)
                    Case 6
                        Me.pic6.Tag = dr("FacultyID")
                        Me.pic6.Image = System.Drawing.Image.FromStream(myMS)
                    Case 7
                        Me.pic7.Tag = dr("FacultyID")
                        Me.pic7.Image = System.Drawing.Image.FromStream(myMS)
                    Case 8
                        Me.pic8.Tag = dr("FacultyID")
                        Me.pic8.Image = System.Drawing.Image.FromStream(myMS)
                    Case 9
                        Me.pic9.Tag = dr("FacultyID")
                        Me.pic9.Image = System.Drawing.Image.FromStream(myMS)
                    Case 10
                        Me.pic10.Tag = dr("FacultyID")
                        Me.pic10.Image = System.Drawing.Image.FromStream(myMS)

                End Select

            End While
        End If

    Catch ex As Exception
        MsgBox(ErrorToString)
    Finally
        con.Close()
    End Try
End Sub

i want to it ti read datas from the database if the system see the course it will load all teachers that is teaching within the course

Recommended Answers

All 3 Replies

For starters
In your Select Query you will need to add a Parameter for @Course.

            cmd.Connection = conn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = sSQL
            cmd.Parameters.AddWithValue("@Course",value of Course)

I also wouldn't open the connection until you have the Command Initialized but thats just me.

A shorter way of coding that would be to reference the controls dynamically as

Dim pbx As PictureBox = Me.Controls("PictureBox" & dr("Course"))
pbx.Tag = dr("FacultyID")
pbx.Image = System.Drawing.Image.FromStream(myMS)

That replaces the entire select-case block with three lines of code.

Mmm why don't you make it simple by making a folder that will contain all your pictures that will be used and then on your database just save there the directory address to prevent lot of coding for instance if you want to replace the image. saving to file will also prevent the future freezing on a slow computer memory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.