object reference not set

T

TG

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.Databases


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.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common
Public Class Form1

'Public Class frmSQLConnection


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(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim objServers As DataTable
Dim strServer As String

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

For Each objRow As DataRow In objServers.Rows

strServer = CStr(objRow("Server"))
If Not TypeOf objRow("Instance") Is DBNull AndAlso
CStr(objRow("Instance")).Length > 0 Then

strServer += "\" & CStr(objRow("Instance"))

End If

Me.comboServers.Items.Add(strServer)

Next

Me.ComboDatabase.Enabled = False

End Sub



Private Sub button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

Dim objConn As ServerConnection

If Me.comboServers.Text.Trim.Length() = 0 Then


objConn = New ServerConnection()

If Me.comboServers.Text.Trim.Length() > 0 Then

objConn.ServerInstance = 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.ComboDatabase.Items.Clear()


For Each objDB As Database In Me.SMOServer.Databases


Me.ComboDatabase.Items.Add(objDB.Name)


Next

Me.ComboDatabase.Enabled = True

Me.ComboDatabase.SelectedIndex = 0




End Sub


Private Sub button2_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button2.Click

SMODatabase = Me.SMOServer.Databases(Me.ComboDatabase.Text)

Me.DialogResult = Windows.Forms.DialogResult.OK

Me.Close()

End Sub


Private Sub button3_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button3.Click

Me.DialogResult = Windows.Forms.DialogResult.Cancel

Me.Close()

End Sub

End Class
 
G

Göran Andersson

TG said:
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.Databases


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(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

Dim objConn As ServerConnection

You only assign anything to the SMOServer property if the content of the
combobox is empty:
If Me.comboServers.Text.Trim.Length() = 0 Then


objConn = New ServerConnection()

If Me.comboServers.Text.Trim.Length() > 0 Then

This line can never be reached:
 
F

Family Tree Mike

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.
 
T

TG

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
 
T

TG

I got it!



Private Sub button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim objConn As ServerConnection

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


objConn = New ServerConnection()

If Me.comboServers.Text.Trim.Length() > 0 Then

objConn.ServerInstance = 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.ComboDatabase.Items.Clear()


For Each objDB As Database In Me.SMOServer.Databases


Me.ComboDatabase.Items.Add(objDB.Name)


Next

Me.ComboDatabase.Enabled = True

Me.ComboDatabase.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top