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