locked
handling null values - null reference exception RRS feed

  • Question

  • I've recently started using entity framework to display my database records.

    Im having trouble handling null values. Trying to display a field from a database with a null value returns this error: NullReferenceException was unhandled

    I've had this problem before i used entity framework and to fix it, i simply added .tostring() at the end of the code, but it isnt working this time.

        Dim publishContext As New project_ManagementEntities
        Dim currentEngineer As New tblEmployee
    
        Private Sub Page_Loaded(sender As Object, e As RoutedEventArgs)
    
            cboQuickFind.DataContext = publishContext.tblEmployees
            cboQuickFind.DisplayMemberPath = "fullName"
            cboQuickFind.SelectedIndex = 0
            populate()
    
        End Sub
    
        Private Sub populate()
    
            txtFirstName.Text = currentEngineer.FirstName.ToString()
            txtLastName.Text = currentEngineer.Surname.ToString()
    
    
        End Sub
    If there is no firstName or surname saved for the current record, it returns the error. How do i stop this happening?


    • Edited by typicalalex1 Wednesday, July 9, 2014 8:44 AM code error
    Wednesday, July 9, 2014 8:43 AM

Answers

  • Hi typicalalex1;

    See if changing the following two lines in your code corrects the issue.

    txtFirstName.Text = If( currentEngineer.FirstName = Nothing, String.Empty, currentEngineer.FirstName )
    txtLastName.Text = If( currentEngineer.Surname = Nothing, String.Empty, currentEngineer.Surname )
    

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by typicalalex1 Wednesday, July 9, 2014 2:04 PM
    Wednesday, July 9, 2014 1:47 PM

All replies

  • Hi typicalalex1;

    See if changing the following two lines in your code corrects the issue.

    txtFirstName.Text = If( currentEngineer.FirstName = Nothing, String.Empty, currentEngineer.FirstName )
    txtLastName.Text = If( currentEngineer.Surname = Nothing, String.Empty, currentEngineer.Surname )
    

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by typicalalex1 Wednesday, July 9, 2014 2:04 PM
    Wednesday, July 9, 2014 1:47 PM
  • Hi typicalalex1;

    See if changing the following two lines in your code corrects the issue.

    txtFirstName.Text = If( currentEngineer.FirstName = Nothing, String.Empty, currentEngineer.FirstName )
    txtLastName.Text = If( currentEngineer.Surname = Nothing, String.Empty, currentEngineer.Surname )

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thanks. This does work. Why doesn't tostring() work like it does when using a dataset?

    This is code from an old project using a dataset:

                txtFirstName.Text = ds.Tables(0).Rows(0).Item("firstName").ToString
                txtLastName.Text = ds.Tables(0).Rows(0).Item("lastName").ToString

    Wednesday, July 9, 2014 2:03 PM
  • From your statement, "Trying to display a field from a database with a null value returns this error: NullReferenceException was unhanded", A database null value has no concept in Visual Basic and is not an object. Therefore accessing the ToString method will also cause an exception to happen.

    Nothing in Visual Basic is handled differently From Microsoft documentation on the Nothing keyword:

    Nothing (Visual Basic)
    Represents the default value of any data type. For reference types, the default value is the 
    null reference. For value types, the default value depends on whether the value type is nullable.

    The String data type is a reference type and the default value for a reference type is null reference. So testing to see if the value you are trying to place in the TextBox is valid or not should be done any time it is possible of having a Nothing as well as DbNull which you could test using IsDbNull function.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, July 9, 2014 2:48 PM