none
unable to select from two table RRS feed

  • Question

  • I am selecting from two table but the system will crash at this line … txtSurnameNK.Text = dr.Item("SurName")  with the following message Additional Information: “SurName” I have checked the column name it is correct “SurName”  Here is my code below.  Thanks

     cmd = New OleDbCommand("Select * From Student WHERE Student_ID = ?", conn)
            cmd.Parameters.AddWithValue("Student_ID", txtSelect.Text)
            dr = cmd.ExecuteReader
            If dr.HasRows() Then
                While dr.Read
                    cboProgram.Text = dr.Item("Programtbl")
                    cboLevel.Text = dr.Item("Leveltbl")
    
                    cmd = New OleDbCommand("Select * From StudentNOK WHERE Student_ID = ?", conn)
                    txtSurnameNK.Text = dr.Item("SurName")
                    txtFirstNameNK.Text = dr.Item("FirstName")
                End While
            Else
                MessageBox.Show("There is no matching record")
            End If
            dr.Close()
            dr.Dispose()
            conn.Close()

    Tuesday, September 19, 2017 1:25 PM

Answers

  • Try the next code:

    cmd = New OleDbCommand("Select * From Student WHERE Student_ID = ?", conn)
    cmd.Parameters.AddWithValue("Student_ID", txtSelect.Text)
    dr = cmd.ExecuteReader
    If dr.Read Then
       cboProgram.Text = dr("Programtbl")
       cboLevel.Text = dr("Leveltbl")
    
       cmd = New OleDbCommand("Select * From StudentNOK WHERE Student_ID = ?", conn)
       cmd.Parameters.AddWithValue("Student_ID", txtSelect.Text)
       dr = cmd.ExecuteReader
       If dr.Read Then
          txtSurnameNK.Text = dr("SurName")
          txtFirstNameNK.Text = dr("FirstName")
       End If
    Else
       MessageBox.Show("There is no matching record")
    End If
    . . . .
    

    Tuesday, September 19, 2017 6:08 PM

All replies

  • Try the next code:

    cmd = New OleDbCommand("Select * From Student WHERE Student_ID = ?", conn)
    cmd.Parameters.AddWithValue("Student_ID", txtSelect.Text)
    dr = cmd.ExecuteReader
    If dr.Read Then
       cboProgram.Text = dr("Programtbl")
       cboLevel.Text = dr("Leveltbl")
    
       cmd = New OleDbCommand("Select * From StudentNOK WHERE Student_ID = ?", conn)
       cmd.Parameters.AddWithValue("Student_ID", txtSelect.Text)
       dr = cmd.ExecuteReader
       If dr.Read Then
          txtSurnameNK.Text = dr("SurName")
          txtFirstNameNK.Text = dr("FirstName")
       End If
    Else
       MessageBox.Show("There is no matching record")
    End If
    . . . .
    

    Tuesday, September 19, 2017 6:08 PM
  • Hi alobi,

    According to your question is more related to VB, I will move it to Visual Basic forum for suitable support.

    The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 20, 2017 6:20 AM
  • Thanks immensely 

    AL

    Wednesday, September 20, 2017 8:01 AM