0

I have this code

SqlConnection sqlConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["TestStoredProcedure"].ConnectionString);

This is my web.config

<add name="=TestStoredProcedure" connectionString="Data Source=.;Initial     Catalog=testSqlServer;Integrated Security=True"/>  

I got this exception:

Object reference not set to an instance of an object.

on the connection string line.

0

2 Answers 2

4

You're referencing ConnectionString on something that doesn't exist:

ConnectionStrings["TestStoredProcedure"].ConnectionString

Your config file has =TestStoredProcedure as the name. That doesn't match your code. Remove the =.

Sign up to request clarification or add additional context in comments.

Comments

0

Include following namespace line in your header of the page.

using System.Configuration;

 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestStoredProcedure"].ConnectionString);

Web.Confiq:

<connectionStrings>
    <add name="TestStoredProcedure" connectionString="Data Source=.;Initial     Catalog=testSqlServer;Integrated Security=True"/>
    </connectionStrings>

Also remove '=' from the name of the connection string.

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.