0

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
Prescott Chartier
  • 1,180
  • 2
  • 14
  • 24
  • It seems like the answer you expect to find is someone telling you what's wrong with your code but the answer is actually for you to learn how to debug your own code so that you can work out what's wrong. For a start, you haven't told us where the exception is thrown, indicating that you haven't taken notice. Once you know where, you can determine which reference on that line is `Nothing`. You can then step through the code to work out why no object was assigned. Start learning how to debug [here](https://docs.microsoft.com/en-au/visualstudio/debugger/debugger-feature-tour). – jmcilhinney Jun 13 '18 at 02:47
  • Or [here](https://docs.microsoft.com/en-au/visualstudio/debugger/navigating-through-code-with-the-debugger). – jmcilhinney Jun 13 '18 at 02:48
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – jmcilhinney Jun 13 '18 at 02:49
  • Once I call loadAllergies from another page, the error occurs on the first item I like to add. The first query – Chimichangas Jun 13 '18 at 02:52
  • You may have other problems. Are you expecting just one record returned from each query? You only call .Read once for each query. Check out DataTable.Load(DataReader) and you won't need the loop to fill the data table. – Mary Jun 13 '18 at 03:42
  • If the exception is occurring on the call to `Add` then your problem is with `Session.Item("HospNum")` or `Session.Item("IDNUM")`. Are you sure `Session` is not null? Have you debugged the values of each of those items? – Chris Dunaway Jun 13 '18 at 14:13
  • I have just realized that, and this actually is giving me a hard time in passing session values across pages. It seems like when I call other methods from other pages, my session variables are lost. I don't understand why. I did a work around and made my methods a public functions. Can I have any suggestions on how to fix this? Because I think that making my methods into a public function every time I need to call it from other pages would not do me any good in the long run. – Chimichangas Jun 14 '18 at 01:31

1 Answers1

0

Thanks to Chris Dunaway's comment, I have fixed the problem. For the session variables, instead of using Session.Item(""), I used HttpContext.Current.Session(""). I do not know why the session.item values are lost when calling subs or functions from other pages, but this fixed the problem.