Object reference not set to an instance of an object : Web Method

Posted on 2004-11-22
Last Modified: 2006-11-17
Getting the error "Object reference not set to an instance of an object" from the following code. Any ideas?

  <WebMethod()> Public Function UserDisplayName(ByVal strLogin As String)
        Dim ds As DataSet
        Dim row As DataRow
        Dim DisplayName As String

        ds.Merge(GetEmployees(0, 0, strLogin))
        row = ds.Tables("Employees").Rows(0)
        If ds.Tables("Employees").Rows.Count = 1 Then
            DisplayName = row("FirstName") & " " & row("LastName")
            Return DisplayName
            Return "Invalid"
        End If
    End Function
Question by:cbasulto
    LVL 4

    Expert Comment

    You need to define what datatype you want your function to return

     <WebMethod()> Public Function UserDisplayName(ByVal strLogin As String) AS String

    Author Comment

      Still same error with:
     <WebMethod()> Public Function UserDisplayName(ByVal strLogin As String) As String...

    Author Comment

    I have identified where the reference error is occurring;

            ds.Merge(GetEmployees(0, 0, strLogin))

    The GetEmployees function is another webmethod in the same service that fetches a dataset with records filtered by input parameters.
    LVL 3

    Accepted Solution

    Yep, whenever you see that error - assume you haven't instantiated an object... it's a common mistake made and something ASP developers do all the time.

    You have these lines:

    Dim ds As DataSet
    Dim row As DataRow

    You have declared that ds and row have a type of DataSet and DataRow respectively, BUT you have instantiated them.

    Dim ds As New DataSet
    Dim row As New DataRow

    which is a shorthand method of writing:

    Dim ds As DataSet = New DataSet
    Dim row As DataRow = New DataRow

    Expert Comment

    Weddell is correct

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
    Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
    This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now