mohamad guntur

mohamad guntur

  • NA
  • 40
  • 40.6k

How to read multiple sql reader at VB.NET?

Jan 21 2013 10:31 AM
Well,  i have a problem with Read clause in VB.NET.

My Database:

MS_KARYAWAN         MS_KESEHATAN
--------------------        ------------------------
ID_KARYAWAN          ID_KESEHATAN
NAMA_KARYAWAN     ID_KARYAWAN
JABATAN                    PERIODE
PANGKAT                   NILAI_PENGGANTIAN

My code

---------------------------------------------------------------------------
Private Sub cmbPeriode_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbPeriode.SelectedIndexChanged
        If Not con.State = ConnectionState.Open Then
            con.Open()
        End If

        Dim query As String
        Dim dr As SqlDataReader

        Dim j As Integer
        j = Val(cmbPeriode.SelectedItem)

        For i As Integer = 0 To cmbPeriode.Items.Count Step 1
            If cmbPeriode.SelectedIndex = i Then
                query = String.Format("SELECT TOP (1) NILAI_PENGGANTIAN FROM MS_KESEHATAN WHERE ID_KARYAWAN='" + Mid(cmbID_KARYAWAN.Text, 1, 10) + "' AND PERIODE='" & Me.cmbPeriode.Text & "' ORDER BY NILAI_PENGGANTIAN DESC;")
                cmd.CommandText = query
                If cmd.ExecuteScalar() = "" Then
                    con.Close()
                    If Not con.State = ConnectionState.Open Then
                        con.Open()
                    End If
                    cmd.CommandText = "SELECT MS_KARYAWAN.JABATAN FROM MS_KARYAWAN INNER JOIN MS_KESEHATAN ON MS_KARYAWAN.ID_KARYAWAN = MS_KESEHATAN.ID_KARYAWAN WHERE MS_KESEHATAN.ID_KARYAWAN='" & Mid(cmbID_KARYAWAN.Text, 1, 10) & "' AND MS_KARYAWAN.JABATAN LIKE '%Kepala Divisi%';"
                    dr = cmd.ExecuteReader()
                    If dr.Read = True Then
                        txtNilaiPengganti.Text = "1100000"
                    Else
Msgbox("Error")
                    End If


                Else
                    txtNilaiPengganti.Text = cmd.ExecuteScalar().ToString()
                End If
            End If
        Next
    End Sub

--------------------------------------------------------------------------------------

The bold is the code which has issues

Mid(cmbID_KARYAWAN.Text, 1, 10) ---> is used to get the PK of ID_KARYAWAN on combobox

My algorithm:
When the query on ExecuteScalar running
then do the next query which is only read for related table
if the query value is TRUE then it should be inside the If statement
if it FALSE then do to Else Statement
(in Else statement i will do the same query as the previous one using INNER JOIN, but in different WHERE clause)

If i execute, it always throw to else. It won't read the If statement 


any help would be appreciated :D