I am trying to loop through a DataSet table and find the difference for some dates. I keep getting invalid DBnull errors. When I try to set my dims to DBnull the code still won't work. Here is my code:
Private Sub censusFinalTable()
Dim Conn_PatientDaysDlyCY As SqlConnection
Dim SqlSelect_PatientDaysDlyCY
As SqlCommand
Dim DSfinal As New DataSet()
Dim myreaderCY As SqlDataReader
Dim ConnString As String = "*************************
**********
**********
**********
**********
****"
Dim SelectStatementCY As String = "SELECT CensusRdPhyBcd.Account AS Expr1, vw_ThomasData.fc, vw_ThomasData.admdate, vw_ThomasData.disdate" + _
" FROM CensusRdPhyBcd LEFT OUTER JOIN " + _
"vw_ThomasData ON CensusRdPhyBcd.Account = vw_ThomasData.acctno" + _
" ORDER BY vw_ThomasData.admdate"
Dim adaptor As SqlDataAdapter = New SqlDataAdapter(SelectState
mentCY, ConnString)
Dim cmdBuilder As SqlCommandBuilder = New SqlCommandBuilder(adaptor)
Conn_PatientDaysDlyCY = New SqlConnection(ConnString)
Conn_PatientDaysDlyCY.Open
()
SqlSelect_PatientDaysDlyCY
= New SqlCommand(SelectStatement
CY, Conn_PatientDaysDlyCY)
myreaderCY = SqlSelect_PatientDaysDlyCY
.ExecuteRe
ader
adaptor.Fill(DSfinal)
'*************************
**********
**********
**********
**********
**********
**********
*****
Dim firstDay As Date
Dim lastDay As Date
Dim days As String
Dim admdate As String
Dim disDate As String
Dim aditdate As DBNull
Dim DischargeDate As DBNull
'loop through dataset
Dim J As Integer
aditdate = DSfinal.Tables(0).Rows(J).
Item(2)
DischargeDate = DSfinal.Tables(0).Rows(J).
Item(3)
admdate = aditdate
For J = 0 To DSfinal.Tables(0).Rows.Cou
nt - 1
If admdate <= "04/01/2007" Then
firstDay = "04/01/2007"
Else
firstDay = admdate
If DisDate >= "04/30/2007" Then
lastDay = "04/30/2007"
Else
lastDay = DisDate
End If
days = DateDiff("d", firstDay, lastDay)
MsgBox(lastDay)
End If
Next
Start Free Trial