7

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?

6
  • 1
    In your web.config file, is the <connectionStrings> node under the <configuration> one? Commented Jul 8, 2013 at 14:23
  • Try just ConfigurationManager.ConnectionStrings["ConnString"].ToString() Commented Jul 8, 2013 at 14:24
  • @RyanWeir - Yes, it's a direct child of the <configuration> node. Commented Jul 8, 2013 at 14:26
  • @ckv - When I try that, I get "Object reference not set to an instance of an object". Commented Jul 8, 2013 at 14:27
  • Which web.config did you add it to? The one in the root of your project? Or the one in your Views directory? Commented Jul 8, 2013 at 14:37

3 Answers 3

4

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the issue was actually the fact I was using the wrong web.config file!
1

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

1 Comment

This shouldn't affect the ConfigurationManager, as it ignores provider. That's only used by ADO.net
1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.