Khushali Acharya
Khushali Acharya

Reputation: 11

Null reference exception in asp .net

we are working on a project in which we need to show places on google map. For places, we are providing latitude and longitude from database. we are facing null reference exception error in the following place:

using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.
        ConnectionStrings["Data Source=KHUSHALI\\SERVER;Initial Catalog=gis;
                   Integrated Security=True"].ConnectionString))

How to resolve this error please guide me.

Upvotes: 1

Views: 642

Answers (3)

A_Sk
A_Sk

Reputation: 4630

Your code,

using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.
        ConnectionStrings["Data Source=KHUSHALI\\SERVER;Initial Catalog=gis;
                   Integrated Security=True"].ConnectionString))

is Invalid, this is not the way to declare connection string and access them.

How can we declare Connection Strings and can Access them??

No1:>In a Page

string strConnectionString="server=localhost;database=myDb;uid=myUser;password=myPass;;
                   Integrated Security=True";
using (SqlConnection con = new SqlConnection(strConnectionString))
{
}

No2.>Web.Config you can declare then under configuration and appSeting

And Can Access Like:

using (SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings("myConnectionString")))
{
}

No3>Web.Config you can declare then under configuration and connectionStrings

<connectionStrings>
<add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>

And Can Access Like:

 using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString))
    {
    }

Upvotes: 0

Rahul Singh
Rahul Singh

Reputation: 21825

Cause of Exception:-

When you say:

System.Configuration.ConfigurationManager.
        ConnectionStrings["Data Source=KHUSHALI\\SERVER;Initial Catalog=gis;
                   Integrated Security=True"]

Since there is no connection string with name Data Source=KhUSHAL.., thus ConnectionStrings will return null and on that you are trying to access ConnectionString property which will result in Null reference exception. Read about this error here.

Basically you are mixing both, either do this:-

string CS ="Data Source=KHUSHALI\\SERVER;Initial Catalog=gis;Integrated Security=True";
using (SqlConnection con = new SqlConnection(CS))
{
   //Your code
}

Or fetch it from Web.Config(Preferred way):-
First define the connection in Web.Config:

<connectionStrings>
   <add name="Test" connectionString="Data Source=KHUSHALI\\SERVER;Initial Catalog=gis;
         Integrated Security=True"  providerName="System.Data.SqlClient" />
</connectionStrings>

Then read it like this:-

using (SqlConnection con = new SqlConnection(System.Configuration
                     .ConfigurationManager.ConnectionStrings["Test"].ConnectionString))
{
   //Your code
}

Upvotes: 2

Sajeetharan
Sajeetharan

Reputation: 222722

Do you have the ConnectionString in the Web.Config of the UI project?

Fix: Copy that ConnectionString and Paste in your Web.Config

Upvotes: 0

Related Questions