Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6622355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:23:43+00:00 2026-05-25T21:23:43+00:00

I am developing an windowsform application . It consists of a combobox with list

  • 0

I am developing an windowsform application . It consists of a combobox with list of servernames and one more for list of databases for selected servername and one more for list of tables for selected database . when user selects servername and clicks button it displays the database names in tht server . If user changes his mind and selects another servername still i am having same list of databases of first selected . My database list should refresh each time depending upon the server slection change how can i achive this

Here is my sample code :

   public MainForm()
    {
        InitializeComponent();
        // FileHelper = new SqlDatabaseDataExport.FileHelper.FileUtilHelper();
        dt = SmoApplication.EnumAvailableSqlServers(false);

        if (dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                ServernamesList_combobox.Items.Add(dr["Name"]);

            }
           // ServernamesList_combobox.Items.Add("10.80.104.30\\webx");

            DisplayMainWindow("Server list added");
            Logger.Log("Server List added");
        }

        Authentication_combobox.Items.Add("Windows Authentication");
        Authentication_combobox.Items.Add("Sql Authentication");
    }



    /// <summary>
    /// Generating list of databases with in the selected Server and list of 
    /// selected tables with in the selected 
    /// databse
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    private void DatabasenamesList_combobox_SelectedIndexChanged(object sender, EventArgs e)
    {

        dbName = DatabasenamesList_combobox.SelectedItem.ToString();


        connectionString = GetConnectionString();
        string mySelectQuery = "select [name] from sys.tables WHERE type = 'U' AND is_ms_shipped = 0 ORDER BY [name];";
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand myCommand = new SqlCommand(mySelectQuery, con);

        con.Open();
        SqlDataReader myReader = myCommand.ExecuteReader();
        try
        {
            while (myReader.Read())
            {
                SelectTables.Items.Add(myReader.GetString(0));
            }
        }
        finally
        {

            myReader.Close();
            con.Close();
        }


    }



      private void button1_Click_1(object sender, EventArgs e)
    {
        serveName = ServernamesList_combobox.SelectedItem.ToString();
        if (string.IsNullOrEmpty(serveName))
        {
            MessageBox.Show("Please select servername");
            return;
        }

        if (Authentication_combobox.SelectedItem == null)
        {
            MessageBox.Show("Please select authentication");
            return;
        }
        String conxString = string.Empty;

        if (Authentication_combobox.SelectedItem == "Windows Authentication")
        {
            conxString = "Data Source=" + serveName + "; Integrated Security=True;";
        }

        if (Authentication_combobox.SelectedItem == "Sql Authentication")
        {
            if (string.IsNullOrEmpty(Username.Text))
            {
                MessageBox.Show("Please Enter Valid User name");
                return;
            }
            if (string.IsNullOrEmpty(Password.Text))
            {
                MessageBox.Show("Please Enter Valid Password");
                return;
            }
            conxString = "Data Source=" + serveName + "; Integrated Security=False;User ID =" + Username.Text + ";Password=" + Password.Text;
        } 


        using (SqlConnection sqlConx = new SqlConnection(conxString))
        {

            try
            {
                sqlConx.Open();
                MessageBox.Show("Connection established successfully");

            }
            catch (Exception ex)
            {

                MessageBox.Show("Exception" + ex);
                MessageBox.Show(" Please enter valid Credentials");
                return;

            }

            DataTable tblDatabases = sqlConx.GetSchema("Databases");
            sqlConx.Close();

            foreach (DataRow row in tblDatabases.Rows)
            {
                Databases.Add(row["database_name"].ToString());
            }

        foreach (var database in Databases)
        {
            DatabasenamesList_combobox.Items.Add(database);
        }


    }

  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:23:44+00:00Added an answer on May 25, 2026 at 9:23 pm

    Add a SelectedIndexChanged event to your DatabasenamesList_combobox.
    In the code for that method, simply call your code to populate your database. Right nnow everything is stuffed into ‘button1’. Move out your procedure into one called something like

    
    private void PopulateDatabases(string serverName)
    {
            //populate Databases
            //.. your code here ...
    
            //clear the list
            DatabasenamesList.Items.Clear();
            foreach (var database in Databases)
            {
                DatabasenamesList_combobox.Items.Add(database);
            }
    
    }
    
    

    There are many other ways to clean up this code, such as in your:
    catch (Exception ex)

    how do you know an exception is from not having the right authentication type? You should catch a specific type of exception and handle it separately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am Developing Windows Form Application in .Net, I want to insert selected rows
I am currently developing a C# Windows Form Application. After the user has login
I'm developing a windows form application. Could any one please advise what are the
We are developing C# .net 4.0 Windows Form Based Application. Here, User Will enter
I have been developing a C# windows form application in XP. It all works
Developing a .NET WinForms application: how can I check if the window is in
Developing a heavily XML-based Java-application, I recently encountered an interesting problem on Ubuntu Linux.
VB Windows form Application.. I am developing an application of which part of the
My situation is that I'm developing a C# application which is launching an instance
I'm having the following problem: I'm developing a C# application which requires unsafe code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.