Click here to Skip to main content
15,891,726 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to create the connection string by selecting from ComboBoxes. I want to load DataSource in the First Combobox and accordingly load the second combobox with associated DataBase Name.
I am using SMO to do this .I managed to load first combobox with DataSource or Say Servernames and on this Selected Changed Event want to load the second Combo but I am getting error: Failed to Connect to the Server when event is fired. Where's The Problem.
My Code Follows :
Public Sub fetchDataSource()
        Dim dataTable = SmoApplication.EnumAvailableSqlServers(False)
        For i As Integer = 0 To dataTable.Rows.Count - 1
            cmbDataSource.Items.Add(dataTable.Rows(i).Item(0))

        Next
      
    End Sub
 Private Sub cmbDataSource_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDataSource.SelectedIndexChanged
        cmbDatabase.Items.Clear()

        Dim serverName As String = cmbDataSource.SelectedText.ToString()
        Dim server As Server = New Server(serverName)

        For Each database As Database In server.Databases
            cmbDatabase.Items.Add(database.Name)
        Next
End Sub
Posted
Updated 29-May-11 21:43pm
v2
Comments
Ankit Rajput 1-Jun-11 5:13am    
What is the problem with that?
Karwa_Vivek 1-Jun-11 5:32am    
when I am Trying to List all the databases
from the selected Server (LOADED IN THE FIRST COMBO)
it Says Failed to connect to the SERVER

You need to look at the string you are actually creating: How does it compare with the string that VS presents you via the Server Explorer in the Database Properties?

You code isn't helpful: we don't know the values or what you are actually doing with them!
 
Share this answer
 
Comments
Karwa_Vivek 30-May-11 3:26am    
I need this DataSource/ServerName: and Then List Of DataBases on the basis of Selected Servername
That's All...And It will Solve my Problem
Karwa_Vivek 30-May-11 3:38am    
fetchDataSource() is Function I am calling this in Form's Load
to Fill the cmbDataSource(combobox)
I have managed to Get the Servernames on this Combo
cmbDatabase(combobox) in which I need the DatabaseNames on the Selection of Servernames.
Here I am getting The Error
Hi,

try this code to get all the SQL Server instances in the local Network.

System.Data.DataTable Dt = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources();

Server obj = new Server(new Microsoft.SqlServer.Management.Common.ServerConnection("<instancename>"));
</instancename>


Regards
AR
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900