Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am connecting to the database with the code below
string connString = @"Data source = test2 ; Database=test1data ; User Id=a ; Password=test1234";
   using (SqlConnection sqlConn = new SqlConnection(connString))
            {
                string sqlQuery = @"SELECT * from Zone";
                SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                da.Fill(table);
                dataGridView1.DataSource = new BindingSource(table, null);
            }

But I wanted to create a file to connect that could be changed.

For example:

I'm connecting to a database, but changing this file I'll connect to another database

What I have tried:

var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["test"].ConnectionString;
          // ConfigurationManager.ConnectionStrings["testdb"].ConnectionString;
           using (SqlConnection sqlConn = new SqlConnection(connectionString))
           {
               string sqlQuery = @"SELECT * from Zone";
               SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn);
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               DataTable table = new DataTable();
               da.Fill(table);
               dataGridView1.DataSource = new BindingSource(table, null);
           }

And this is the code i tryed in app.config
<add name="test" connectionString="Data Source=.;Initial Catalog=test1data; User Id=a ; Password=test123" providerName="System.Data.SqlClient" />
Posted
Updated 16-Aug-17 3:41am
Comments
Karthik_Mahalingam 16-Aug-17 10:00am    
so what is the issue

1 solution

This will show you how: How to: Save and Edit Connection Strings[^]

Update: Here is a CodeProject article that show you how to do it in code: Saving Connection Strings to app.config[^]
 
Share this answer
 
v2
Comments
PedroAzevedo1234 16-Aug-17 9:50am    
That dont explain
Graeme_Grant 16-Aug-17 9:59am    
Huh? It has step-by-step instructions on how to do it. You want me to write the code for you?
PedroAzevedo1234 16-Aug-17 10:10am    
But it don´t help in my problem, when I run the progam it gives this error ConfigurationErrorsException: Connectionstrings Unknown configuration section
Dave Kreskowiak 16-Aug-17 10:12am    
Without seeing the code you wrote to get the connection string from the config file and the content of the config file itself, it's pretty much impossible to tell you what you did wrong.
Dave Kreskowiak 16-Aug-17 10:13am    
Connectionstrings Unknown configuration section

If this is really how you named the section in the config file, you've got it wrong. It should be "connectionStrings", not "Connectionstrings". CHARACTER CASE MATTERS!

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