Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i'm trying to write Connectionstring to Web.config like this:
HTML
<connectionStrings>
    <add name="Dbconnection" connectionString="Server=localhost; Database=OnlineShopping ; Integrated Security=True"/>
  </connectionStrings >


and read from it like this:

C#
string strcon = ConfigurationManager.ConnectionStrings["Dbconnection"].ConnectionString;
SqlConnection DbConnection = new SqlConnection(strcon);

when run the progrom I get an error because of the null reference.
but when I use this code:
C#
SqlConnection DbConnection = new SqlConnection();
DbConnection.ConnectionString = "Server=localhost; Database=OnlineShopping ; Integrated Security=True";

I don't get any error and the program work correctly!
What is the problem?
Tanx!
Posted
Comments
abbaspirmoradi 25-Aug-13 12:57pm    
please put error here.
alifat92 25-Aug-13 13:14pm    
System.NullReferenceException: Object reference not set to an instance of an object.

C#
<connectionstrings>
		<add name="Your Connection string name" connectionstring="data source=server name;Database=database name;uid=Database Username;pwd=password;multipleactiveresultsets=true;" providername="System.Data.SqlClient" />
	</connectionstrings>

For Call Connection string write the below code

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection string name"].ConnectionString.ToString());

Connection string name means "Your Connection string name"
 
Share this answer
 
Check your Web.config Connection string.

XML
<connectionStrings>
    <add name="Dbconnection" connectionString="Data Source=localhost;Initial Catalog=OnlineShopping;Integrated Security=true" providerName="System.Data.SqlClient" />
 
Share this answer
 
Comments
alifat92 25-Aug-13 13:18pm    
I try this but still nut working!
Musakkhir 25-Aug-13 13:23pm    
add database from database explorer and put one sqldatasource on test page and configure it with one table. It automatically creates connection string in web.config.
There doesn't seem anything wrong with your connection string other than the 'Initial Catalog' missing from it. You have specified it as Database, but in a connection string, we specify it as Initial Catalog. So, re-write your connection string as below and try again:

XML
<connectionStrings>
    <add name="Dbconnection" connectionString="Server=localhost;Initial Catalog=OnlineShopping;Integrated Security=True"/>
  </connectionStrings >


For detailed information, you can visit http://msdn.microsoft.com/en-us/library/ms178411.aspx[^] as well.
 
Share this answer
 
Windows authentication :

HTML
<add name="windowsConnection" connectionString="Server=localhost; Database=databasename; Trusted_Connection=True" providername="System.Data.SqlClient" />


Sql server authentication :

HTML
<add name="Sqlserverconnection" connectionstring="server=localhost; Database=databasename;uid=username; password=123 " providername="System.Data.SqlClient"/>


Ref : Shortcut method for getting connection string
 
Share this answer
 
v2
Comments
CHill60 26-Feb-15 5:41am    
Question is over a year old and already resolved. Only bubbled back into the list because of Rockjaws "solution" 4
The web.config file must be added to the project/web site requiring the connection string.
 
Share this answer
 
Comments
CHill60 26-Feb-15 5:42am    
Answering old, resolved questions with this level of "detail" is likely to attract down-votes. I would avoid the practice if I was you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900