使用DataSet从DataRow提取数据

时间:2018-05-30 19:38:45

标签: asp.net .net webforms dataset

示例1在price为空值时抛出异常,示例2显示空值。这两种从DataRow中提取数据的方式有何不同?

Using ta As New DataSet1TableAdapters.titlesTableAdapter
        Dim table As New DataSet1.titlesDataTable
        Dim row As DataSet1.titlesRow

        table = ta.GetData

        For Each row In table

            'Example 1
            'Throws an exception with nulls
            Response.Write(row.price & "<br>")

            'Example 2
            'Displays Nulls
            Response.Write(row("price") & "<br>")

        Next
    End Using

1 个答案:

答案 0 :(得分:0)

当您将null与字符串连接时,您将获得null。最好做点什么

Using ta As New DataSet1TableAdapters.titlesTableAdapter
    Dim table As New DataSet1.titlesDataTable
    Dim row As DataSet1.titlesRow

    table = ta.GetData

    For Each row In table

        if (row("price") is Nothing) Then
            Response.Write(row("price") & "<br>")
        else
            Response.Write( "<br>")
        End If
    Next
End Using

或者一些较短的手版本。我猜第一个例子产生了一个异常,因为它在row.price上隐式调用了ToString(),因为它是null,它给出了一个异常。