Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 how to connect database using app.config file

Author  Topic 

salmonraju
Yak Posting Veteran

54 Posts

Posted - 2006-11-10 : 01:40:55
i wrote database connection in app.config file , in follwing tag
<configuration>
<appsettings>
<add key = "dbconnection" value ="E:\\database.mdf">
</appsettings>
</configuration>

and
sqlcommand con = new sqlcommand(configurationsettings.appsettings["dbconnection"]);

but
at time of run the application, it shows this error

Argument Exception:
format of specified string does not confirm to specification starting at index 0.

please tell me how to connect database using app.config file

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-11-10 : 03:09:36
How can you create a connection object by specifying connection string in the command object?

Or is this a typo?

sqlconnection con = new sqlconnection(configurationsettings.appsettings["dbconnection"]);


Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

salmonraju
Yak Posting Veteran

54 Posts

Posted - 2006-11-10 : 03:37:54
sorry Actually i used sqlconnection only here but i typed wrongly

i wrote database connection in app.config file , in follwing tag
<configuration>
<appsettings>
<add key = "dbconnection" value ="E:\\database.mdf">
</appsettings>
</configuration>

and
sqlconnection con = new sqlconnection(configurationsettings.appsettings["dbconnection"]);

but
at time of run the application, it shows this error

Argument Exception:
format of specified string does not confirm to specification starting at index 0.

please tell me how to connect database using app.config file
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-10 : 04:33:59
There are a lot of examples with the sample projects with Visual Studio.
Even GotDotNet has many examples of this.
Take a look and enjoy.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-11-10 : 09:30:10
Tons of things wrong ... probably because you are not showing us your actual code.

First:

quote:

<configuration>
<appsettings>
<add key = "dbconnection" value ="E:\\database.mdf">
</appsettings>
</configuration>



That is not valid XML. You need /> :

<add key = "dbconnection" value ="E:\\database.mdf" />

Also, you should have only 1 slash in the path; you don't escape your XML file the way you do with string literals in C#:

<add key = "dbconnection" value ="E:\database.mdf" />

Next, your C# code:

quote:

sqlconnection con = new sqlconnection(configurationsettings.appsettings["dbconnection"]);



That is not valid C# --- the language is case sensitive and you've used all lowercase. None of that code is valid.

Or ...

show us your ACTUAL code and your ACTUAL app.config file, don't try to re-create it. It is hard to troubleshoot something that is not your actual code, don't you think? You know how to cut and paste, right?

- Jeff
Go to Top of Page
   

- Advertisement -