Hello,
I have created project in c# and i want to use connection string stored in app.config file in all my form. I have wrote that connectionManager code but it gives different output (not proper output) and sometimes it gives exception what to do?

Recommended Answers

All 16 Replies

What error does it give and what exactly do you mean by 'different output'. The connection string you have in your app.config is exactly the same as the one you used in your class?

Ok tell of only that thing, connection string written in app.config file in that |datadirectory| this we have to change to some drive name where database is stored like F: \databasename ? Because if i dont change it than it gives exception for some table that table not found. Not for all tables. So should i change it? Will it give any problem in future?

well, your connection string (where ever you store it) will need to point to the actual location of the database or you will have problems. If the connection string is wrong you will get an error for all tables though as your code can't locate the database, let alone specific tables within it.

It might be time to post some code up so we can understand the issue.

In app.config file string is
<add name="RailwayDBcon" connectionString="provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|\RailwayDB.mdb" providerName="System.Data.OleDb"/>

and in my .cs file or code
configurationManager.connectionStrings["RailwayDBcon"].connectionString so in my app.config file i need to change that datadirectory word to F: because my database is in f

Your data source will need to be the actual location of the database, so if it resides on F drive you will need to include the full path to the file.

So only i need to do is in app.config file F: \databasename. Dis is nly d full path
No changs in .cs file.

No, your .cs file should be OK. If the error solely relates to not being able to locate the database then the connection string in the config file was the problem.

Ok. In .cs i have written that configuration manager wala code only. So it automaticaly rfrs to config file.by that name attribute. So whnevr if i run my prjct on anthr pc so nly i hav 2 chang path in one file only

That is correct. And seeing the config file is just XML that is easy to do.

Ok. Hericles. I have one more doubt, after making setup of the project is database location matters? How to attach that database file while making setup?

You can attach the database when you build your installer, its been a while since I've done and I can't remember the exact steps but you'll find an online tutorial to help. Or, if the config specifies the correct location, you can just dump the database there afterwards. But location will matter. If you have specified a drive and folder where the database will be it will need to be there.

Supposean giving may application's setup file to any user for his use and he is a non technical person than if he install app than to he has to dump database file in his pc? Even if he dont know what is database? Or is it like that when a person installing application database file should get store automatically in program file folder

Ok thank you so much hericles

first: You must add System.Configuration to References.
And add app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="conStr"
            connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database1.accdb;Persist Security Info=True"
            providerName="System.Data.OleDb" />
    </connectionStrings>
</configuration>

So example;

using System.Configuration;

string strcon = configurationManager.connectionString["conStr"].connectionString;

it is your connection. You can change after your program install your computer.

@LP
My code exactly as you have given. Instead of DataDirectory i have written F: \dbname.mdb. But i didn't get you.

Put Your Database From F: drive into your project folder then It will be work for you

way to add open your project in Vstudio2005/2008/2010 then in right corner solution explorer perform right click select ( add-> new item ) one window open in that you select Local database then browse your database and select it it will auto matically included to your project

Another way copy your database and save to your project folder it may in your local drive such as C,D,F or in my documents->visual studio->projects->your project name

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.