3

I have a VB.NET program that builds multiple datasets from 10 different databases.

I am getting this Exception:

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

This error happens on the following lines:

Me.OverTableAdapter.Adapter.SelectCommand.CommandTimeout = 60000
Me.OverTableAdapter.Fill(Me.Dataset.Over, TodayDt, TodayEnd)

What does this Exception mean?

Stephen Kennedy
  • 16,598
  • 21
  • 82
  • 98
Shmewnix
  • 1,513
  • 9
  • 30
  • 63

2 Answers2

5

Well if it's failing on this line:

Me.OverTableAdapter.Adapter.SelectCommand.CommandTimeout = 60000

Then either:

  • Me.OverTableAdapter is Nothing
  • Me.OverTableAdapter.Adapter is Nothing
  • Me.OverTableAdapter.SelectCommand is Nothing

(The second line you've shown us is irrelevant, as you're not getting that far.)

We can't tell based on what you've shown us, but you should be able to find out either in a debugger or by adding diagnostic logging.

Once you've worked out why it's failing, fixing it should be easy - it's almost certainly just a matter of initializing it properly. Compare your initialization of this adapter with the initialization of the other ones.

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • It's not selecting anything, it's simply telling the program to increase the timeout of this specific adapter fill... It's assigned the connection string earlier in the program, however as I stated earlier, it's identical to the other 10... I assign the database connection string differently in each section, and then cycle through and collect the results using the same code. – Shmewnix Mar 12 '13 at 14:43
  • @Shmewnix: I didn't say it was selecting anything. I said that one of those expressions is `Nothing`. And I strongly suspect it *isn't* actually identical to the other 10. I strongly suspect you've got a typo somewhere, but we can't tell what as you've only shown us a tiny piece of code. As I said, you should go in with a debugger and work out which expression is null and why. – Jon Skeet Mar 12 '13 at 14:52
  • I believe I found the issue. I Fill the datasets information locally from the first call of the LOCAL database. I did not assign "overtableadapter" anything in the first call, locally, therefor it was NULL when calling for it initially in this external database. The internal database does not have a timeout call, because it's a local databases, and never times out. Thank You. – Shmewnix Mar 12 '13 at 14:56
2

The reason for this exception is that the SelectCommand will be inititalized from TableAdapter.Fill and not before. So when you try to change the Timeout before you get the NullReferenceException.

You can extend the TableAdapter by creating the partial class in a separate file than the designer.vb/designer.cs.

Namespace DataSet1TableAdapters

    Partial Public Class OverTableAdapter
        Public Property CommandTimeout As System.Int32
            Get
                If Me.CommandCollection Is Nothing OrElse Me.CommandCollection.Length = 0 Then
                    Return -1
                Else
                    Return Me.CommandCollection(0).CommandTimeout
                End If
            End Get
            Set(value As System.Int32)
                If Not Me.CommandCollection Is Nothing Then
                    For Each cmd In Me.CommandCollection
                        cmd.CommandTimeout = value
                    Next
                End If
            End Set
        End Property
    End Class

End Namespace

Now you can use this property instead after you've created an instance of the adapter.

Tim Schmelter
  • 411,418
  • 61
  • 614
  • 859
  • This is incorrect. It's the 4th selectcommand, followed by Fill for this particular database. If it's any other way... it times out. (again this works for all other databases in the program). – Shmewnix Mar 12 '13 at 14:47
  • @Shmewnix: What means _"it's the 4th SelectCommand"_? A `TableAdapter` has only one `SelectCommand` and that is initialized from the methods which select rows. That can be `Fill`, `GetData` or a custom query which returns rows. However, one of these must be executed, otherwise the `SelectCommand`(which is `protected` btw) is `Nothing/null`. But even if it is not null it'll be overridden on every method call from the appropriate command. That's why my property above sets all command's `CommandTimeouts`. – Tim Schmelter Mar 12 '13 at 14:54
  • You are correct. Your info was Helpful. – Shmewnix Mar 12 '13 at 15:00