This code runs smoothly on page load but error occurs when I try to call loadAllergies() from another page. I have tried finding for answers, most of what I saw was to instantiate my arraylist but nothing helps. I tried assigning the items I would like to add in the array into a variable and still gets the same error. It seems like when I call my sub from another page, it loses the values that I want to add into my array and so gets the error
Here is my Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack = False Then
loadAllergies()
End If
End Sub
Public Sub loadAllergies()
'Dim qal As ArrayList = TryCast(Session("Queries"), ArrayList)
'Dim qal As New List(Of String)()
Dim qal As New ArrayList()
qal.Add(" Select ISNULL(b.allergy,'') + ', ' + ISNULL(c.allergy,'') + ', ' + ISNULL(d.allergy,'') + ', ' + " &
" ISNULL(e.allergy, '') + ', ' + ISNULL(f.allergy,'') AS Allergies" &
" From Patient_Data..tbmasterAllergies a " &
" Left outer join Build_FIle..tbCoAllergy b on a.AllergyID1=b.AllergyID" &
" Left outer join Build_FIle..tbCoAllergy c on a.AllergyID2=c.AllergyID" &
" Left outer join Build_FIle..tbCoAllergy d on a.AllergyID3=d.AllergyID" &
" Left outer join Build_FIle..tbCoAllergy e on a.AllergyID4=e.AllergyID" &
" Left outer join Build_FIle..tbCoAllergy f on a.AllergyID5=f.AllergyID" &
" where HospNum = '" & Session.Item("HospNum") & "' ")
qal.Add(" Select ISNULL(b.ChiefComplaint,'') + ', ' + ISNULL(c.ChiefComplaint,'') + ', ' + ISNULL(d.ChiefComplaint, '') + ', ' + " &
" ISNULL(e.ChiefComplaint,'') + ', ' + ISNULL(f.ChiefComplaint,'') AS ChiefComplaints from Patient_Data..tbOutPatientHistory a " &
" left outer join Build_FIle..tbCoChiefComplaint b on a.ChiefComplaintID1 = b.ChiefComplaintID" &
" Left outer join Build_FIle..tbCoChiefComplaint c On a.ChiefComplaintID2 = c.ChiefComplaintID" &
" left outer join Build_FIle..tbCoChiefComplaint d on a.ChiefComplaintID3 = d.ChiefComplaintID" &
" Left outer join Build_FIle..tbCoChiefComplaint e on a.ChiefComplaintID4 = e.ChiefComplaintID" &
" left outer join Build_FIle..tbCoChiefComplaint f on a.ChiefComplaintID5 = f.ChiefComplaintID" &
" where IdNum='" & Session.Item("IDNUM") & "' ")
Dim allal As ArrayList = New ArrayList
Dim ccal As ArrayList = New ArrayList
For Each query As String In qal
Dim index As Integer = qal.IndexOf(query)
sqlstr = query
sqlcomm = New SqlCommand(sqlstr, sqlconn)
sqlconn.Open()
rd = sqlcomm.ExecuteReader
If rd.HasRows Then
rd.Read()
If index = 0 Then
ccal.Add(rd.GetString(0))
Else
allal.Add(rd.GetString(0))
End If
End If
sqlconn.Close()
rd.Close()
Next
If allal.Count = 0 Then
Exit Sub
End If
If ccal.Count = 0 Then
Exit Sub
End If
Dim dt As DataTable = New DataTable
dt.Columns.Add("Allergies")
dt.Columns.Add("ChiefComplaints")
With dt
Dim dr As DataRow
dr = dt.NewRow
dt.Rows.Add(dr)
For i As Integer = 0 To .Rows.Count - 1
For ii As Integer = 0 To .Columns.Count - 1
If .Columns(ii).ColumnName = "Allergies" Then
.Rows(i)("Allergies") = allal(0)
Continue For
End If
If .Columns(ii).ColumnName = "ChiefComplaints" Then
.Rows(i)("ChiefComplaints") = ccal(0)
End If
Next
Next
End With
gv1.DataSource = dt
gv1.DataBind()
'qal.Clear()
'allal.Clear()
'ccal.Clear()
End Sub
End Class