c# - Error while reading ConnectionString from App.config file -
i have app.config file:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionstrings> <add name="acwconnectionstring" connectionstring="data source=ashish-pc\\sqlexpress;initial catalog=acw;user id=ash159;password=password"/> </connectionstrings> </configuration>
which using here:
string connstring = configurationmanager.connectionstrings["acwconnectionstring"].tostring();
here getting error 'nullreferenceexception unhandled'..
why getting error?
thankyou!!
tried these getting same error:
string connstring = configurationmanager.connectionstrings["acwconnectionstring"].connectionstring; string connstring = configurationmanager.connectionstrings["acwconnectionstring"].connectionstring.tostring();
note: have added system.configuration , set reference it.
know have 3 projects(admin,employee,database) in single project(abc). have added reference of database admin , employee. , made database class in 2 projects wherever needed connection.
database class follows:
public class database { sqlconnection connection; string connstring = configurationmanager.connectionstrings["acwconnectionstring"].tostring(); public sqlconnection connect() { connection=new sqlconnection(connstring); return connection; } }
it in database project have added app.config file.
try this:-
string connstring = configurationmanager.connectionstrings["acwconnectionstring"].connectionstring;
Comments
Post a Comment