Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConfigurationManager.ConnectionStrings.ConnectionString Issue

I am trying to pull in data from a Microsoft Access database file, in order to populate several textboxes. (The textboxes are all done in XAML.) I'm quite sure I'm missing something, because the database file isn't accessed.

Here is my code:

    DataTable tblVFWPostManagers = new DataTable();
    string connString2 = ConfigurationManager.ConnectionStrings**/*["\\Documents\DatabaseFile.accdb"]*/**.ConnectionString;
    string query2 = @"SELECT Manager ID, Manager FName, Manager LName, Manager Address, Manager City, Manager State, Manager Zip Code,
            Manager Home Phone Number, Manager Cell Phone Number, Manager Email FROM tblVFWPostManagers";

        //Fill the VFWPostManagers Set with the data
        using (SqlConnection conn2 = new SqlConnection(connString2))
        {
            SqlDataAdapter da2 = new SqlDataAdapter(query2, conn2);
            da2.Fill(tblVFWPostManagers);
        }

Note: I'm sure the bolded is incorrect. However, I'm not really sure what goes in those brackets. I assumed, at first, that it was where the filepath went. When I commented that section out, the error disapperead though.

How can I pull in the data from my database using the above method? What am I missing?

like image 608
Samuel Brockmann Avatar asked Aug 13 '13 19:08

Samuel Brockmann


People also ask

What is ConfigurationManager connectionStrings?

ConfigurationManager.ConnectionStrings references a specific section of your app config where are stored the informations to access your databases (one or more).

What is ConnectionString property?

The value of the ConnectionString property is a connection string that includes the source database name and the parameters you need to establish the connection. The default value of the ConnectionString property is an empty string. The Server attribute is mandatory in all situations.

Where do I put ConnectionString in web config?

config file in the Views folder.) Find the <connectionStrings> element: Add the following connection string to the <connectionStrings> element in the Web.


2 Answers

A couple of errors in your code:

ConfigurationManager.ConnectionStrings references a specific section of your app config where are stored the informations to access your databases (one or more). The Section contains lines like these

  <connectionStrings>
            <add name="MyDataBase" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;
                                   Data Source=C:\myFolder\myAccess2007file.accdb;
                                   Persist Security Info=False"/>
  </connectionStrings>

(To create a valid connectionstring for your app look at www.connectionstrings.com)
So your code refers to these section voices using the 'name' key with

string connString2 = ConfigurationManager.ConnectionStrings["MyDataBase"].ConnectionString;

Said that now the text of your query will fail because you use extensively columns names with spaces. In this case every column name should be enclosed in square brackets.

string query2 = @"SELECT [Manager ID], [Manager FName], [Manager LName], .....
like image 124
Steve Avatar answered Sep 18 '22 21:09

Steve


In your app.config or web.config file you have a ConnectionStrings section :

<configuration>
    <connectionStrings>
        <add name="MyConnection" connectionString="..."/>
    </connectionStrings>
    ...
</configuration>

You can access it in your code :

string connString2 = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
like image 31
Jonesopolis Avatar answered Sep 20 '22 21:09

Jonesopolis