开发者

Clearing items in Combobox if user selection changed

开发者 https://www.devze.com 2023-04-09 15:06 出处:网络
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

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 + ";Pass开发者_Go百科word=" + 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);
        }


    }

  }


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

精彩评论

暂无评论...
验证码 换一张
取 消