Reputation: 431
I'm trying to list all the SQL Servers that are available on our Network in a gridview on a form. I have this code in a button click event.
SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
System.Data.DataTable myDataTable = instance.GetDataSources();
gvSqlServers.DataSource = myDataTable;
gvSqlServers.DataBind();
It runs but nothing shows up in the gridview and I don't get any errors.
Upvotes: 1
Views: 614
Reputation: 11
Try this.It is working for me..
SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
DataTable dt = instance.GetDataSources();
DropDownList1.DataSource = dt;
foreach (DataRow dr in dt.Rows)
{
DropDownList1.Items.Add(string.Concat(dr["ServerName"], "\\", dr["InstanceName"]));
}
Note:In your code you used databind method for dropdownlist.For this you must mention Datatextfield and datavalue field.We can't mention here.So use this,it gives the accurate result.
Upvotes: 1
Reputation: 4797
As a start, I believe the SQL Servers must have SQL Browsing turned on, which is off many times. Otherwise I'm guessing that your application wouldn't find it anyways.
When I run your code on my local box from a Windows Form app, it works fine
I also ran the same code in ASP.NET and it works as well.
My guess is that it is an issue with SQL Browsing or possibly something else preventing the SQL servers from broadcasting
Upvotes: 1