tags:

views:

33

answers:

4

I am trying to select "food_ItemName" and "food_UnitPrice" from "t_Food" table in SQL Server 2005.

I have the following code:

private void GetDatabaseConnection()
{
    string connectionString = @"Server = RZS-F839AD139AA\SQLEXPRESS; Integrated Security = SSPI; Database = HotelCustomerManagementDatabase";
    connection = new SqlConnection(connectionString);
    connection.Open();
}

and.....

public Food PopulateFoodItemListview()
{
    GetDatabaseConnection();
    string selectFoodItemQuery = @"SELECT food_ItemName, food_UnitPrice FROM t_Food";
    SqlCommand command = new SqlCommand(selectFoodItemQuery, connection);
    SqlDataReader reader = command.ExecuteReader();

    while (reader.Read())
    {
        food.ItemName.Add(reader.GetString(0));  // Exception is generated in this line
        food.UnitPrice.Add(reader.GetDouble(1));
    }
    connection.Close();
    return food;
}

In food class I have the following code:

public class Food
{
    private List<string> itemName;
    private List<double> unitPrice;
    private double itemUnit;
    private Customer foodCustomer = new Customer();

    public Food ()
    {
    }

    public Food(List<string> itemName, List<double> unitPrice) : this()
    {
        this.itemName = itemName;
        this.unitPrice = unitPrice;
    }

    public List<string> ItemName
    {
        get { return itemName; }
        set { itemName = value ; }
    }

    public List<double> UnitPrice
    {
        get { return unitPrice; }
        set { unitPrice = value; }
    }

    public double ItemUnit
    {
        get { return itemUnit; }
        set { itemUnit = value; }
    }
}

but it generating following exception. Why?

"Object reference not set to an instance of an object."

A: 

Have you looked at your stack trace to figure out which line the error is occurring on or which function?

From what I could see it look like it may be referring to your food object in the PopulateFoodItemListview() method. The food object is never created.

Mike737
I declared Food food = new Food() outside of method but inside in class. So "food" is global.
Thanks Mike737 for spending your valuable time to me.
+2  A: 

Well to start:

ConnectionStrings should go into the app.config/web.config.

ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString

Use the following code to open/close connection:

using(SqlConnection connection = new SqlConnection(ConnectionString))
{
  connection.Open();

}

And try to avoid having nulls in the reader...

SELECT ISNULL(food_ItemName, ''), ISNULL(food_UnitPrice, 0.0) 

EDIT:

You forgot to initialize the private member of the class Food

Edit to those lines:

private List<string> itemName = new List<string>; 
private List<double> unitPrice = new List<double>; 

Or do it like this

public List<string> ItemName 
{ 
   get 
   { 
     if (itemName == null) itemName = new List<string>; 
     return itemName; 
   } 
}


public List<double> UnitPrice 
{ 
  get 
  { 
    if (unitPrice== null) unitPrice= new List<double>; 
    return unitPrice; 
  } 
} 
Yves M.
I declared "Connection" as global.
Thanks Yves M. Its working now. My problem is solved.
In your code the connection is not closed if an exception is thrown. So you have to make sure about this in the context the object lives in...
Yves M.
+1  A: 

On which variable does it complain?

It looks like the problematic one (well, maybe there are more problems but this one is for sure can cause the exception) is ItemName which is not initiated.

rursw1
Thanks rursw1. My problem is solved.
+1  A: 

You need to create an instance of the 'itemName' and 'unitPrice' variables before you can add to either of them. You can go ahead and do that when you are declaring them.

private List<string> itemName = new List<string>();
private List<double> unitPrice = new List<string>();
Even Mien