List SQL Server Instances

Michael Mastro II 51 Reputation points

Good afternoon,

I have a windows form that I am working with. When I first wrote this code it worked and was able to pull a list of SQL Servers for a ComboBox, and then as I changed servers it would populate another ComboBox with databases. I had not updated the code in a few years, and it is using .net 4.6.1 and the Microsoft.SqlServer.SqlManagementObjects version: 150.18147.0. I went to use the program and launched it now it is not returning any sql servers. I opened up visual studio and went to debug it and it is still not returning servers.
All this code is contained within the form class itself.

private async void ActiveStatuses_LoadAsync(object sender, EventArgs e)  
            //List all the servers, if only want to see local set to true  
            DataTable dataTable = await System.Threading.Tasks.Task.Run(() => { return SmoApplication.EnumAvailableSqlServers(false); });  
            cbServers.ValueMember = "Name";  
            cbServers.DataSource = dataTable;  
private void cbServers_SelectedIndexChanged(object sender, EventArgs e)  
            if (cbServers.SelectedIndex != -1)  
                string serverName = cbServers.SelectedValue.ToString();  
                Server server = new Server(serverName);  
                    foreach (Database database in server.Databases)  
                catch (Exception ex)  
                    string excepection = ex.Message;  
        private void cbDatabases_SelectedIndexChanged(object sender, EventArgs e)  
            string databaseName = cbDatabases.SelectedItem.ToString();  

I cannot see why this would stop working and debugging shows enumeration yielded no results. Also it does output any errors.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,823 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,208 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Bruce ( 55,196 Reputation points

    Most likely your network no longer has the SQLBrowser service setup. Security usually shuts this down.

  2. Rijwan Ansari 746 Reputation points MVP


    Can you try the below code?

    //// Retrieve the enumerator instance, and then retrieve the data sources.  
            SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;  
            DataTable dtDatabaseSources = instance.GetDataSources();  

  3. Karen Payne MVP 35,031 Reputation points

    I had the same problem on a work machine but not my home computer.

    Steps to fix

    1. Administrative Tools
    2. Services
    3. Properties
    4. General tab
    5. Change Startup to auto
    6. Start the service

    I work on an area which is ungodly secure (IRS restrictions) so even with the above out of around 20 network servers all I can see is my local machine

  4. Olaf Helper 40,656 Reputation points

    => Enumeration Limitations
    The method wasn't ever reliable and can return incomplete results.

    0 comments No comments