System.NullReferenceException: Object reference not set to an instance of an object.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aknamit
    New Member
    • Feb 2010
    • 5

    System.NullReferenceException: Object reference not set to an instance of an object.

    I am getting error after publishing a website. At debug mode it works fine.
    Any body can help me that what is the reason.

    Code:
    Private Sub fill_ddl_Dist(ByVal _query As String)
    Dim con As Connection
    con = New Connection
    Dim ds As DataSet = con.GetRecord(_query)
    ' ddl_Dist.Items.Clear()
    ddl_Dist.Items.Add("All")
    'For Each row As DataRow In ds.Tables(0).Rows
    ' ddl.Items.Add(row(0).ToString)
    'Next
    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
    ddl_Dist.Items.Add(ds.Tables(0).Rows(i).Item(0).To String)
    Next
    End Sub
    ----------------------------------------------------------------------------------------------

    and error comes when i add or cleare item of DropDownList(dd l_dist.) if i remove these lines then error comes on dataset asignment line.

    Please help me to understand the problem...
    Attached Files
    Last edited by tlhintoq; Feb 8 '10, 03:15 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Well, according to the error it looks like your DataSet doesn't exist....
      You should always check to make sure that Objects exist before you attempt to use them

      For example, the following code will check to make sure that the DataSet "ds" exists before attempting to use it:

      Code:
      Dim ds As DataSet = con.GetRecord(_query)
      
      If ds IsNot Nothing Then
        For Each row As DataRow In ds.Tables(0).Rows 
         ' .....
        Next
      End If
      The code that you have is not going to work though. As far as I know, you cannot fill a DataSet the way you are attempting to...

      See:

      -Frinny

      Comment

      • aknamit
        New Member
        • Feb 2010
        • 5

        #4
        thank you Frinny..
        I've got solved that problem..You were right.
        Thanks a lot..

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          I'm glad I could help!

          Happy Coding!

          -Frinny

          Comment

          Working...