ayha
ayha

Reputation: 181

Object not set to an instance of object

I have the following connection string in the web.config file, when I run I get an error

"Object not set to an instance of object"

at the following line. I am using sql server 2012 db and visual studio 2010.

dbConnectionString = ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;

Config

<connectionStrings>
<add name="myConnection" connectionString="Data Source=sag-pc\;Initial Catalog=BalloonShop;Persist Security Info=True;User ID=sa;Password=pwd" providerName="System.Data.SqlClient" />

Upvotes: 1

Views: 144

Answers (2)

Revan
Revan

Reputation: 1144

Try this

Config :

  <appSettings>
<add key="myConnection" value="Data Source=sag-pc;Initial Catalog=BalloonShop;Persist Security Info=True;User ID=sa;Password=pwd;providerName=System.Data.SqlClient"/>
  </appSettings>

In Code :

string _connection=ConfigurationManager.AppSettings["myConnection"].ToString();

Upvotes: 1

Pierre
Pierre

Reputation: 417

Try the following:

ConfigurationManager.ConnectionStrings["myConnection"].ToString()

Upvotes: 0

Related Questions