object reference not set

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TG

    object reference not set

    hi!

    I have a combobox where the user can type the sql server name. Then a
    connect button that, when the user click, I want it to populate
    another combobox with all the databases from the server in the
    previous combobox.

    Unfortunately, i am getting the error object reference is not set....!

    Here is where i am getting the error:

    For Each objDB As Database In Me.SMOServer.Da tabases


    I am new to VB ...I'm using vb2008 connecting to sql server 2005.


    Your help is appreciated.

    Thanks!

    Tammy






    Here is my complete code:

    Imports Microsoft.SqlSe rver.Management .Smo
    Imports Microsoft.SqlSe rver.Management .Common
    Public Class Form1

    'Public Class frmSQLConnectio n


    Private m_objServer As Server
    Public Property SMOServer() As Server
    Get
    Return m_objServer
    End Get
    Private Set(ByVal value As Server)
    m_objServer = value
    End Set
    End Property



    Private m_objDatabase As Database
    Public Property SMODatabase() As Database
    Get
    Return m_objDatabase
    End Get
    Private Set(ByVal value As Database)
    m_objDatabase = value
    End Set
    End Property

    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load

    Dim objServers As DataTable
    Dim strServer As String

    '---- retrieve a list of SQL Server instances on the network
    objServers = SmoApplication. EnumAvailableSq lServers(False)

    For Each objRow As DataRow In objServers.Rows

    strServer = CStr(objRow("Se rver"))
    If Not TypeOf objRow("Instanc e") Is DBNull AndAlso
    CStr(objRow("In stance")).Lengt h 0 Then

    strServer += "\" & CStr(objRow("In stance"))

    End If

    Me.comboServers .Items.Add(strS erver)

    Next

    Me.ComboDatabas e.Enabled = False

    End Sub



    Private Sub button1_Click(B yVal sender As System.Object, ByVal e
    As System.EventArg s) Handles Button1.Click

    Dim objConn As ServerConnectio n

    If Me.comboServers .Text.Trim.Leng th() = 0 Then


    objConn = New ServerConnectio n()

    If Me.comboServers .Text.Trim.Leng th() 0 Then

    objConn.ServerI nstance = Me.comboServers .Text.Trim()

    End If


    Me.SMOServer = New Server(objConn)

    End If


    '---- Note: the connection will open when we call our first
    method on the Server object


    Me.ComboDatabas e.Items.Clear()


    For Each objDB As Database In Me.SMOServer.Da tabases


    Me.ComboDatabas e.Items.Add(obj DB.Name)


    Next

    Me.ComboDatabas e.Enabled = True

    Me.ComboDatabas e.SelectedIndex = 0




    End Sub


    Private Sub button2_Click(B yVal sender As System.Object, ByVal e
    As System.EventArg s) Handles Button2.Click

    SMODatabase = Me.SMOServer.Da tabases(Me.Comb oDatabase.Text)

    Me.DialogResult = Windows.Forms.D ialogResult.OK

    Me.Close()

    End Sub


    Private Sub button3_Click(B yVal sender As System.Object, ByVal e
    As System.EventArg s) Handles Button3.Click

    Me.DialogResult = Windows.Forms.D ialogResult.Can cel

    Me.Close()

    End Sub

    End Class
  • =?ISO-8859-1?Q?G=F6ran_Andersson?=

    #2
    Re: object reference not set

    TG wrote:
    hi!
    >
    I have a combobox where the user can type the sql server name. Then a
    connect button that, when the user click, I want it to populate
    another combobox with all the databases from the server in the
    previous combobox.
    >
    Unfortunately, i am getting the error object reference is not set....!
    >
    Here is where i am getting the error:
    >
    For Each objDB As Database In Me.SMOServer.Da tabases
    >
    >
    I am new to VB ...I'm using vb2008 connecting to sql server 2005.
    >
    >
    Your help is appreciated.
    >
    Thanks!
    >
    Tammy
    >
    8<
    >
    Private Sub button1_Click(B yVal sender As System.Object, ByVal e
    As System.EventArg s) Handles Button1.Click
    >
    Dim objConn As ServerConnectio n
    You only assign anything to the SMOServer property if the content of the
    combobox is empty:
    If Me.comboServers .Text.Trim.Leng th() = 0 Then
    >
    >
    objConn = New ServerConnectio n()
    >
    If Me.comboServers .Text.Trim.Leng th() 0 Then
    This line can never be reached:
    objConn.ServerI nstance = Me.comboServers .Text.Trim()
    >
    End If
    >
    >
    Me.SMOServer = New Server(objConn)
    >
    End If
    >

    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

      #3
      RE: object reference not set

      If I'm reading the code below correctly, SMOServer is only set if the
      combobox text length is zero. That doesn't sound likely or correct. Inside
      that if block you test the length greater than zero. I think your if block
      needs to be reworked.

      Private Sub button1_Click(B yVal sender As System.Object, ByVal e
      As System.EventArg s) Handles Button1.Click
      >
      Dim objConn As ServerConnectio n
      >
      If Me.comboServers .Text.Trim.Leng th() = 0 Then
      >
      >
      objConn = New ServerConnectio n()
      >
      If Me.comboServers .Text.Trim.Leng th() 0 Then
      >
      objConn.ServerI nstance = Me.comboServers .Text.Trim()
      >
      End If
      >
      >
      Me.SMOServer = New Server(objConn)
      >
      End If
      >
      >
      '---- Note: the connection will open when we call our first
      method on the Server object
      >
      >
      Me.ComboDatabas e.Items.Clear()
      >
      >
      For Each objDB As Database In Me.SMOServer.Da tabases
      >
      >
      Me.ComboDatabas e.Items.Add(obj DB.Name)
      >
      >
      Next
      >
      Me.ComboDatabas e.Enabled = True
      >
      Me.ComboDatabas e.SelectedIndex = 0
      >
      >
      >
      >
      End Sub
      >
      >
      >

      Comment

      • TG

        #4
        Re: object reference not set

        How should I re-write it?

        Like I said i am new to VB and I found something similar to this which
        I was trying to modify to get what I need.

        Thanks a lot for your help guys!

        Tammy

        Comment

        • TG

          #5
          Re: object reference not set

          I got it!



          Private Sub button1_Click(B yVal sender As System.Object, ByVal e As
          System.EventArg s) Handles Button1.Click

          Dim objConn As ServerConnectio n

          'If Me.comboServers .Text.Trim.Leng th() = 0 Then


          objConn = New ServerConnectio n()

          If Me.comboServers .Text.Trim.Leng th() 0 Then

          objConn.ServerI nstance = Me.comboServers .Text.Trim()

          End If


          Me.SMOServer = New Server(objConn)

          'End If


          '---- Note: the connection will open when we call our first
          method on the Server object


          Me.ComboDatabas e.Items.Clear()


          For Each objDB As Database In Me.SMOServer.Da tabases


          Me.ComboDatabas e.Items.Add(obj DB.Name)


          Next

          Me.ComboDatabas e.Enabled = True

          Me.ComboDatabas e.SelectedIndex = 0




          I commented out this line and now I get the results I was looking
          for!!!!!

          Thanks a lot for pointing me in the right direction!!!! :-)


          Tammy



          Comment

          Working...