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.

Recommended Answers

All 3 Replies

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;

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.

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);
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.