954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

ConnectionString in combo box

Hy, I have a question...
I want for my application to select on startup the database i want...I thought about keeping my database name in a combo box and when i run my application i will select what i want...but....how can i concatenate in app.config the ConnectionString?Does anyone Know?
Thanks.

stefilina
Newbie Poster
13 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Write the code as following it will display the database name but also hold the connection string of database in as value member.

comboBox1.ValueMember=ConnectionString;
comboBox1.DiaplayMember=NameOfDataBase;
abelLazm
Postaholic
2,113 posts since Feb 2011
Reputation Points: 219
Solved Threads: 124
 

You, can not do like Concatenating to the app.config, but instead you can use the help of two more text boxes for storing the Username and Password and also two radio buttons for seeing whether it is Sql server authentication or Windows authentication.

Saikalyankumar
Junior Poster in Training
91 posts since Mar 2011
Reputation Points: 8
Solved Threads: 23
 

You can use a Dictionary collection object:

public partial class Form1 : Form
    {
        Dictionary<string, string> connStrings;
        public Form1()
        {
            InitializeComponent();

            connStrings = new Dictionary<string, string>();            
            connStrings.Add("Connection string 1", "Your full connection string 1");
            connStrings.Add("Connection string 2", "Your full connection string 2");
            connStrings.Add("Connection string 3", "Your full connection string 3");

            //the name in comboBox will be a value of dictionary
            //the key of the dictionary will be the full connection string:
            comboBox1.DataSource = new BindingSource(connStrings, null);
            comboBox1.DisplayMember = "Value";
            comboBox1.ValueMember = "Key";

            comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string connString = ((KeyValuePair<string, string>)comboBox1.SelectedItem).Value;
            MessageBox.Show("This is your connection string:\n" + connString);
        }
    }
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: