Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I have defined a class "Seller" in this I am using a public string variable as
C#
public string _connectionString = ConfigurationManager.ConnectionStrings["seller"].ConnectionString.ToString();

The above connection string is defined in App.config file used in class library Seller.

When I am calling class Seller
C#
Seller _seller = new Seller();

It throws error
" Object reference not set to an instance of an object" when the cursor reads the above string object??

Its very irritating Error. I tried every thing but didn,t found any solution.
Please help on this.

Thanks
Posted
Updated 14-Aug-15 23:16pm
v3

1 solution

So start with the debugger and look at exactly what is going on.
Initially, change that line to just the declaration:
C#
public string _connectionString = null;

and in your Seller class constructor add this:
C#
var o1 = ConfigurationManager.ConnectionStrings["seller"];
var o2 = o1.ConnectionString;
_connectionString = o2.ToString();
Then put a breakpoint on the first line and step through, using the debugger to look at the values - when you know which one is null, you can start looking at why - but until you know that, you are just guessing.

We can't do this for you - we don't have access to your machine or the web.config file.
 
Share this answer
 
Comments
mayank.bhuvnesh 15-Aug-15 5:53am    
I tried the above, now I am receiving error at
var o2 = o1.ConnectionString;

Please find below the App.config File

<!--?xml version="1.0" encoding="utf-8" ?-->
<configuration>
<connectionstrings>
<add name="seller" connectionstring="server=SQLSERVER; database=seller; uid=sa; password=xxxx" providername="Sysm.Data.SqlClient">
</add></connectionstrings>
</configuration>
OriginalGriff 15-Aug-15 6:11am    
So you need to look at why your app.config file is not being read correctly.
Start with the example on MSDN:
https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings(v=vs.110).aspx
And see what that gives you.
We can't do any of this for you - we can't access your system!
F-ES Sitecore 15-Aug-15 15:46pm    
app.config isn't used on asp.net sites, your connection settings need to be in the web.config file.
mayank.bhuvnesh 16-Aug-15 3:02am    
Dear I am using class library project in MVC application and in class library project I am using app.config file only. I even tried the same with web.config file an passing the parameter for connection string to class library but the error is still same.
Now I have hardcoded it but its not the actual solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900