8

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?

3
  • Show the contents of the section ConnectionString in your app.config or web.config Commented Aug 13, 2013 at 20:00
  • you should post your app.config Commented Aug 13, 2013 at 20:00
  • What goes in the brackets (indexer) is the name of the connection string in your app.config. ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString. Inside of app.config will have <add name="MyConnString" connectionString="whatever"/> Commented Aug 13, 2013 at 20:02

2 Answers 2

14

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], .....
Sign up to request clarification or add additional context in comments.

2 Comments

I think that's exactly what I needed. Thanks. Dumb question: Would the <ConnectionStrings> section be somewhere in my XAML, code?
The connectionstrings as well other configuration info for your application are stored in a specific XML file called app.config when you add it to your project. At runtime it will be copied in the same folder of your application with the name of your application plus the extension config (see Application Configuration Files)
4

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.