Hi Jim,
I just tried the following code and a messagebox is returned for each server found. Make sure you set a reference to the COM object called "Microsoft SQLDMO Object Library":
Dim sqlApp As SQLDMO.Application = New SQLDMO.ApplicationClass()
Dim listServers As SQLDMO.NameList = sqlApp.ListAvailableSQLServers()
Dim server As Object
For Each server In listServers
MessageBox.Show(server.ToString())
Next server
>Hello,
>
> I am creating a program that documents the tables and fields in a database. I want the user to be able to select the name of the server from a combobox. The combobox should be filled with all available SQL servers. I found some code from a sample on this site and it doesn't work. The error message and code are below. Does anyone know what is wrong?
>
>Error:
>An unhandled exception of type 'System.InvalidCastException' occurred in MSDEManager.exe
>
>Additional information: QueryInterface for interface SQLDMO.NameList failed.
>
>Code:
>Private Sub GetSqlServers()
> Dim sqlApp As SQLDMO.Application = New SQLDMO.ApplicationClass()
> Dim listServers As SQLDMO.NameList = sqlApp.ListAvailableSQLServers()
> Dim server As Object
>
> Dim root, node As TreeNode
>
> root = Me.treeServers.Nodes(0)
> For Each server In listServers
> node = New TreeNode(server.ToString(), ICON_ServerDisconnected, ICON_ServerDisconnected)
> node.Tag = "Server"
> root.Nodes.Add(node)
> Next server
>
> If root.GetNodeCount(False) = 0 Then
> node = New TreeNode("
", ICON_ServerDisconnected, ICON_ServerDisconnected)
> node.Tag = "Nothing"
> root.Nodes.Add(node)
> End If
>
> root.Expand()
> SetStatus("Connected", "Ok", "Ready")
> End Sub
-----------------------------------------
Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.comwww.prenia.comWeblog: blogs.prenia.com/cathi