你调用的对象是空的。当我将项目添加到我的arraylist中时

时间:2018-06-13 02:40:49

标签: asp.net vb.net

此代码在页面加载时运行顺畅,但当我尝试从另一个页面调用loadAllergies()时发生错误。我试图找到答案,我看到的大多数是实例化我的arraylist但没有任何帮助。我尝试将我想要添加到数组中的项目分配到变量中,但仍然会得到相同的错误。看起来当我从另一个页面调用我的sub时,它会丢失我想要添加到我的数组中的值,因此会出现错误

这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

感谢Chris Dunaway的评论,我解决了这个问题。对于会话变量,我使用HttpContext.Current.Session(“”)而不是使用Session.Item(“”)。我不知道为什么在从其他页面调用subs或函数时会丢失session.item值,但这解决了这个问题。