0

I am trying to get the bound values from a row in a ListItem. I have a button on each row that when clicked will perform a task (sending an email message to the person who's name and email address is in that listitem row. So, I have an event handler tied to the listview, and I am trying to get to the underlying datarowview to extract the data items.

Here's what I have in the event handler:

Protected Sub lvUsers_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles lvUsers.ItemCommand
    Dim diCurrentUser As ListViewDataItem = CType(e.Item, ListViewDataItem)
    Dim drCurrentRow As DataRowView = CType(diCurrentUser.DataItem, DataRowView)
    Select Case e.CommandName
            Case "Email"
                Dim strEmailAddress As String = drCurrentRow("contact_email").ToString.Trim
                Dim strUserName As String = drCurrentRow("login").ToString.Trim
                Dim strUserID As String = drCurrentRow("username").ToString.Trim
                Dim strPassword As String = drCurrentRow("password").ToString.Trim

Now, it fails at the line where I am trying to access the contact_email element in the DataRowView object.

What an I doing wrong?

Thanks

John M. Wright
  • 4,477
  • 1
  • 43
  • 61
Ken Ray
  • 2,500
  • 3
  • 21
  • 28
  • possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders Dec 09 '11 at 19:30
  • John, I do know what an NullReferenceException is - clearly when I try to access my crCurrentRow variable, it is referring to "nothing". My question is why, when I am using the "canonical" method for accessing the underlying data from a ListItem datarow, and the DataItem is not a null reference, why does the cast to the DataRowView not work? – Ken Ray Dec 09 '11 at 20:04
  • I'm surprised you haven't solved this problem by now, given the number of answers in the linked article. Clearly, you need to check `drCurrentRow` and `drCurrentRow("contact_email")` to see if they are `Nothing`. Don't assume. Go find out. – John Saunders Dec 09 '11 at 22:01

1 Answers1

0

You have a few things to check:

1) Ensure that drCurrentRow IsNot Nothing before trying to access its values.

2) If drCurrentRow is valid, ensure that it contains a column called contact_email before processing it. For example:

If drCurrentRow.Row.Table.Columns.Contains("contact_email") Then

3) If the column does exist, you need to check the contents of the item for Nothing and/or DBNull.Value equivalence before attempting to perform operations on the value. For example:

If Not drCurrentRow.IsNull("contact_email") Then

So, a portion of your code code be rewritten as follows:

Public Function GetRowStringValue(oRowView As DataRowView, sColumnName As String) As String
   If oRowView IsNot Nothing Then
       If oRowView.Row.Table.Columns.Contains(sColumnName) Then
          If Not oRowView.IsNull(sColumnName) Then
             Return oRowView(sColumnName).ToString.Trim
          End If
       End If
   End If
End Function  

Select Case e.CommandName
    Case "Email"
        Dim strEmailAddress As String = GetRowStringValue(drCurrentRow, "contact_email")
        Dim strUserName As String = GetRowStringValue(drCurrentRow, "login")
        Dim strUserID As String = GetRowStringValue(drCurrentRow, "username")
        Dim strPassword As String = GetRowStringValue(drCurrentRow, "password")
competent_tech
  • 44,465
  • 11
  • 90
  • 113