Click here to Skip to main content
15,911,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Server Error in '/ODSeg1' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:

C#
Line 22:  List<Product> products = new List<Product>();
Line 23:         string cs = "Data Source=SERVER\\SERVERDB;Initial Catalog=lp;User ID=sa;Password=Pentag0n";
Line 24:         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[cs].ConnectionString);
Line 25:         SqlCommand cmd = new SqlCommand("select * from Products", con);
Line 26:         con.Open();
Posted
Updated 7-Dec-11 20:02pm
v2
Comments
uspatel 8-Dec-11 2:02am    
[Edit]:pre tag added

 
Share this answer
 
try to debug through break point and find line that generates errors
use like
C#
string cs = "Data Source=SERVER\\SERVERDB;Initial Catalog=lp;User ID=sa;Password=Pentag0n";
       SqlConnection con = new SqlConnection();
        con.ConnectionString=cs;
       con.Open();
        SqlCommand cmd = new SqlCommand("select * from Products", con);
 
Share this answer
 
Actually, You have used wrong "KEY" for connections string in Config file.

C#
List<product> products = new List<product>();
string cs = "Data Source=SERVER\\SERVERDB;Initial Catalog=lp;User ID=sa;Password=Pentag0n";
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[cs].ConnectionString);
SqlCommand cmd = new SqlCommand("select * from Products", con);
con.Open();</product></product>


here you have used "cs" as KEY. rather use it as directly as connection string. like this..

C#
SqlConnection con = new SqlConnection(cs);


or change in config file like this..

HTML
<connectionstring>
   <add key="cs" value="Data Source=SERVER\\SERVERDB;Initial Catalog=lp;User ID=sa;Password=Pentag0n">
</add></connectionstring>


then use like this..

C#
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[cs].ConnectionString);


hope it works..
 
Share this answer
 
This part ConfigurationManager.ConnectionStrings[cs].ConnectionString gives you NULL value

point to workaround.
1. check your web.config. have you fill key and value properly in connectionstring tag
2. You have connection sting in CS variable so why you used ConfigurationManager ?

check following code
C#
List<Product> products = new List<Product>();
Line 23:         string cs = "Data Source=SERVER\\SERVERDB;Initial Catalog=lp;User ID=sa;Password=Pentag0n";
Line 24:         SqlConnection con = new SqlConnection(cs); //corrected here
Line 25:         SqlCommand cmd = new SqlCommand("select * from Products", con);
Line 26:         con.Open();
 
Share this answer
 
You need to check first if cs in ConfigurationManager.ConnectionStrings[cs].ConnectionString is not null and is valid.

Regards,
Eduard
 
Share this answer
 
You have the connection string in a string, but you are trying to access the connection string from the configuration. Change your code like so and try -
C#
List<product> products = new List<product>();
string cs = "Data Source=SERVER\\SERVERDB;Initial Catalog=lp;User ID=sa;Password=Pentag0n";
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("select * from Products", con);
con.Open();</product></product>
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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