8 Replies - 2335 Views - Last Post: 14 January 2011 - 09:01 AM Rate Topic: -----

#1 tendaimare  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 170
  • Joined: 04-November 10

Object reference not set to an instance of an object

Posted 14 January 2011 - 06:36 AM

i have corrected my conn string i am now using sql connection but now when i run the code it gives me he error Object reference not set to an instance of an object.


'I put this code in the load event
ListBox1.Items.Add(CType(GetTables().ToString, String))


Public Shared Function GetTables() As String()
        Dim kon As SqlConnection
        Dim kom As New SqlCommand
        Dim i As Integer
        Dim str() As String
        kon = New SqlConnection("server=sql...,1444;uid=....;pwd=.....;database=.....")
        kon.Open()
        kom.Connection = kon
        Dim dTable As DataTable
        Try
            dTable = kon.GetSchema(SchemaType.Mapped, New Object() {Nothing, Nothing, Nothing, "TABLE"})
            For i = 0 To dTable.Rows.Count - 1
                ReDim Preserve str(i)
                str(i) = dTable.Rows(i).Item(2).ToString
            Next
            Return str
        Catch ex As Exception
            Return Nothing
        End Try

    End Function



Is This A Good Question/Topic? 0
  • +

Replies To: Object reference not set to an instance of an object

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1030
  • View blog
  • Posts: 4,011
  • Joined: 02-July 08

Re: Object reference not set to an instance of an object

Posted 14 January 2011 - 06:54 AM

Public Shared Function GetTables() As String()
        Dim kon As SqlConnection
        Dim kom As New SqlCommand
        Dim i As Integer
        Dim str() As String
        kon = New SqlConnection("server=sql...,1444;uid=....;pwd=.....;database=.....")
        kon.Open()
        kom.Connection = kon
        Dim dTable As New DataTable '<<< here
        Try
            dTable = kon.GetSchema(SchemaType.Mapped, New Object() {Nothing, Nothing, Nothing, "TABLE"})
            For i = 0 To dTable.Rows.Count - 1
                ReDim Preserve str(i)
                str(i) = dTable.Rows(i).Item(2).ToString
            Next
            Return str
        Catch ex As Exception
            Return Nothing
        End Try

    End Function



Was This Post Helpful? 1
  • +
  • -

#3 tendaimare  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 170
  • Joined: 04-November 10

Re: Object reference not set to an instance of an object

Posted 14 January 2011 - 08:10 AM

okay, can someone elaborate on what is wrong with that part where in line 9 where hawkvalley pointed and said here bcoz i do not know and/or see what is wrong with that part
Was This Post Helpful? 0
  • +
  • -

#4 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Object reference not set to an instance of an object

Posted 14 January 2011 - 08:14 AM

You need to use the 'new' keyword in order to create an instance of object type DataTable.

Dim dTable as new DataTable

This post has been edited by Kilorn: 14 January 2011 - 08:15 AM

Was This Post Helpful? 0
  • +
  • -

#5 tendaimare  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 170
  • Joined: 04-November 10

Re: Object reference not set to an instance of an object

Posted 14 January 2011 - 08:30 AM

I've done that but i still get the same error here: object refference not set to instance of an object
ListBox1.Items.Add(CType(GetTables().ToString, String))

Was This Post Helpful? 0
  • +
  • -

#6 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Object reference not set to an instance of an object

Posted 14 January 2011 - 08:34 AM

Show us how ListBox1 is declared. Have you actually created a variable with the name ListBox1 and created a new instance of it?
Was This Post Helpful? 0
  • +
  • -

#7 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1030
  • View blog
  • Posts: 4,011
  • Joined: 02-July 08

Re: Object reference not set to an instance of an object

Posted 14 January 2011 - 09:00 AM

Don't call the toString method of your function. Your function returns a String array.

ListBox1.Items.AddRange(GetTables.ToArray)
Was This Post Helpful? 0
  • +
  • -

#8 tinase  Icon User is offline

  • New D.I.C Head

Reputation: -16
  • View blog
  • Posts: 33
  • Joined: 19-June 08

Re: Object reference not set to an instance of an object

Posted 14 January 2011 - 09:00 AM

Using the New Keyword is the first step as Kilorn said.
But I would be more helpfull, if you tell me where the code breaks(where the code starts debugging). Is it on line 9 or other lines
Was This Post Helpful? 0
  • +
  • -

#9 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1030
  • View blog
  • Posts: 4,011
  • Joined: 02-July 08

Re: Object reference not set to an instance of an object

Posted 14 January 2011 - 09:01 AM

Personally I would use a List(Of String) not an array.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1