Hey guys. I have a program that does a SQL query and parses the info into a text file. The problem is it misses the first row every time it loops. I know the root cause here is my double use of the read function. I'm hoping to gain some help in how I should use these two loops without the double read.
Here's the code:
For i As Integer = 0 To arrDept.Count - 1
vDeptPO = vPO & "-" & arrDept(i)
For k As Integer = 0 To arrStation.Count - 1
vStationDeptPO = vDeptPO & "-" & arrStation(k)
qImportDet = New SqlCommand("SELECT Department, Station, VendorItem, . . .", DBconn)
dr = qImportDet.ExecuteReader()
Dim fnextresult As Boolean = True
'Write Data
Do Until Not fnextresult
While dr.Read()
'Write Header
If dr.GetValue(1) = "01" Then
writer.WriteLine("HDR~" & vStationDeptPO & "~" & "cust3" . . .))
Else
If dr.GetValue(1) = "02" Then
writer.WriteLine("HDR~" & vStationDeptPO & "~" & "cust4" . . .))
Else
If dr.GetValue(1) = "07" Then
writer.WriteLine("HDR~" & vStationDeptPO & "~" & "cust5" . . .))
Else
writer.WriteLine("HDR~" & vStationDeptPO & "~" & "cust2" . . .))
End If
End If
End If
While dr.Read()
'Write Detail Line (loop)
writer.WriteLine("DET~" & dr.GetValue(0) & "~" & dr.GetValue(1) . . .)
End While
writer.WriteLine("")
End While
fnextresult = dr.NextResult()
Loop
dr.Close()
Next
Next
Thanks guys!