Jonathan
Jonathan

Reputation: 157

Visual Studio 2012 ASP.NET MVC Connection Strings Web.Config

I have a Visual Studio 2012 ASP.NET MVC application which queries a database. I've been told it's good practice to keep the connection string in the web.config file. The connection string called ConnString is located:

  <connectionStrings>
      <add name="ConnString" connectionString="Data Source=IP_OF_SERVER,PORT; Initial Catalog=DATABASE_NAME; UID=USERNAME; pwd=PASSWORD; Integrated Security=True;"/>
  </connectionStrings>

In C# where I want to get the connection string, I use:

String connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;

The app dies on this line and throws the following exception:

Object reference not set to an instance of an object. System.NullReferenceException: Object reference not set to an instance of an object.

I have included:

using System.Configuration;

at the top of the page, but it still fails. I tried using using System.WebConfiguration, but I can still not get the string. How do I get the string?

Upvotes: 7

Views: 34193

Answers (3)

Rajeev Bera
Rajeev Bera

Reputation: 2019

I have nothing new to answer this question but I would like to give some explanation,

 System.Data.SqlClient 

Its .NET Framework Data Provider for SQL Server. In the web.config you should have the System.Data.SqlClient as the value of the providerName attribute. It is the .NET Framework Data Provider you are using.

if you have to connect your application with MYSql then you may use

  MySql .Net Connector

Its required but in your case its missing, thats why you are getting an error message.

You can read more about (here)[http://msdn.microsoft.com/en-US/library/htw9h4z3(v=VS.80).aspx] Hope it will give you a better understanding what error you made and you yo fix it.

 <configuration>
  <connectionStrings>
    <add name="Northwind"
         connectionString="Data Source=Data Source=IP_OF_SERVER,PORT; Initial Catalog=DATABASE_NAME; UID=USERNAME; pwd=PASSWORD; Integrated Security=True;" 
         providerName="System.Data.SqlClient" />
   </connectionStrings>
 </configuration>

Upvotes: 1

BizApps
BizApps

Reputation: 6130

You've missed to add providerName="System.Data.SqlClient" on your connection string.

Change your connectiong string to:

  <connectionStrings>
          <add name="ConnString" connectionString="Data Source=IP_OF_SERVER,PORT; Initial Catalog=DATABASE_NAME; UID=USERNAME; pwd=PASSWORD; Integrated Security=True;" providerName="System.Data.SqlClient" />
      </connectionStrings>

Regards

Upvotes: 1

Ryan
Ryan

Reputation: 6517

Change your web.config file to include providerName="System.Data.SqlClient" as an attribute on the connection string like this:

  <connectionStrings>
      <add name="ConnString" connectionString="Data Source=IP_OF_SERVER,PORT; Initial Catalog=DATABASE_NAME; UID=USERNAME; pwd=PASSWORD; Integrated Security=True;" providerName="System.Data.SqlClient" />
  </connectionStrings>

Upvotes: 4

Related Questions