2

In my windows form i have connection string in app.config as

<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="Database"
            connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database.accdb"
            providerName="System.Data.OleDb" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

And in all classes i am using the following code to connect to the database.

string connString = ConfigurationManager.ConnectionStrings["Database"].ConnectionString;

But its not connected to the database.

Can somebody point out the mistake.

But when i use this code without use of app.config it works fine.

   string connString  = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source = C:\\Users\\Amrit\\Desktop\\Database.accdb; Persist Security Info = False;";

How can i make the app.config connection string work..

Mohammad Dehghan
  • 17,853
  • 3
  • 55
  • 72
Amrit Sharma
  • 1,906
  • 8
  • 45
  • 75
  • 2
    What do you mean "But its not connected to the database"? How do you create a database connection? – nemesv Feb 17 '13 at 07:31
  • 1
    Please put your code where you have made the connection to the database. – Igoy Feb 17 '13 at 07:32
  • Please have look in the edit. – Amrit Sharma Feb 17 '13 at 07:37
  • 1
    Your two connection strings are different: one has `Data Source=|DataDirectory|\Database.accdb` and the another `Data Source=C:\\Users\\Amrit\\Desktop\\Database.accdb`. Why do you expect both to work? Just use in config the "working" one `connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Amrit\Desktop\Database.accdb"` and it should work fine. – nemesv Feb 17 '13 at 07:40
  • i am not using both connection string. I want to use the connection string of app.config as this application will be installed in various systems. – Amrit Sharma Feb 17 '13 at 07:41
  • If it gives you an error, what is the error? If not, what exaclty happens when using connection string from `app.config`? – Mohammad Dehghan Feb 17 '13 at 07:42
  • Also, make sure you trim all the whitespace that is not supposed to be there like this(\\Database.accdb ;) <--- should be \\Database.accdb; – Dimitar Dimitrov Feb 17 '13 at 07:42
  • Have you added System.Configuration Reference to your project? – coder Feb 17 '13 at 07:43
  • well when i try to use the app.config connection string, nothing goes inside the datbase – Amrit Sharma Feb 17 '13 at 07:43
  • yes i have added System.Configuration refrence – Amrit Sharma Feb 17 '13 at 07:43
  • See this basic configuration and check whether you are able to test the connection or not http://www.c-sharpcorner.com/UploadFile/1a81c5/configuring-connection-string-in-app-config-file-during-runt/ – coder Feb 17 '13 at 07:45
  • Test connection passed – Amrit Sharma Feb 17 '13 at 07:48
  • Then something might be the error not with the app.config Right? – coder Feb 17 '13 at 07:49

3 Answers3

0

It seams (from the comments) that you are targeting two difference database files in those two connection strings. The first one is in your App_Data folder of your project, and the second one resides on your desktop.

The file in your App_Data folder is copied in to the output folder (bin/Debug or bin/Release for a WinForms project) every time you start the project in the VS. It overwrites previous contents of the file so every time you have a fresh copy of the file form the App_Data folder in your output folder. To find out, run the program and execute a few insertions. Then close the program and open the database file in the output folder (not in projects App_Data).

This happens because you have set the Copy to Output Directory property of the database file to Copy always.

Mohammad Dehghan
  • 17,853
  • 3
  • 55
  • 72
  • when i start working on the application, i used the database in desktop but now i want to use the database which is in the application folder so that i can create installable setup file. – Amrit Sharma Feb 17 '13 at 07:52
  • @AmritSharma Well, you said that the connection string from the app.config conntects to the database but deos not insert it the database. Which file did you check? Did you open the file in the `App_Data` folder? – Mohammad Dehghan Feb 17 '13 at 08:25
  • I just realized that, the application is able to retrieve the data from the database but not able to write. The database is within the root of project folder. How can i solve this – Amrit Sharma Feb 17 '13 at 08:26
  • What do you mean of 'not able to write'? Please be specific and exactly explain what happens. It is impossible that the command executes successfully (without error) but **nothing** happens. – Mohammad Dehghan Feb 17 '13 at 08:33
  • Well in my application, there is combox which populates its items from database. It is easily able to populate the database. Also i have search function with in the application, which is also able to search what ever i asked it to search. But when i try to add user and add product, it doesnot do that, but there is no error. But when i do same with the database in the desktop it does fine. I think the problem may be with the readonly properties of the project folder. – Amrit Sharma Feb 17 '13 at 08:36
  • If the file would be read-only, your insertion would have failed. So you are really inserting into the file. The question is: where is the file? – Mohammad Dehghan Feb 17 '13 at 08:40
  • Ok the database file is in this location C:\Users\Amrit\Documents\Visual Studio 2012\Projects\Purchase Management\Purchase Management\Resources\App_Data. But when i remove readonly permisission, it comes back again as readonly. – Amrit Sharma Feb 17 '13 at 08:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24656/discussion-between-amrit-sharma-and-md-unicorn) – Amrit Sharma Feb 17 '13 at 08:59
0

You may do it so

<configuration>
 <appSettings>
   <add key="ApplicationTitle" value="Sample Console Application" />
   <add key="ConnectionString"
       value="Server=localhost;Database=Northwind;Integrated
              Security=false;User Id=sa;Password=;" />
</appSettings>

then use ConfigurationSettings.AppSettings["ConnectionString"];

dotmido
  • 1,374
  • 3
  • 13
  • 29
0

you need to set DataDirectory.

You can then set the path in Application_Start in your Global.ascx.cs

AppDomain.CurrentDomain.SetData("DataDirectory", "C:\Users\Amrit\Desktop");

https://stackoverflow.com/a/1409378/2745294

https://stackoverflow.com/a/6708279/2745294

Hope this helps.

Community
  • 1
  • 1
Sushil Mate
  • 583
  • 6
  • 14