Hi guys got a wired problem (well I find it a wired problem :P)
I have an order page done in asp.net c#, and which a user adds ingredients to a set of list boxes, once the user and finished adding items, they get combined to make a sandwich then added to another list box that shows the sandwiches and their ingredients. I also have a button to allow the user to remove the highlighted sandwich from the order, this all works fine until the page loads up with a query string that skips the adding of ingredients and just shows the sandwiches on a order, the listbox of sandwiches populates fine but I then get an error when the remove button is clicked which is
System.NullReferenceException: Object reference not set to an instance of an object.
on CompletedSandwiches.SelectedItem.Text
the only difference is that I pass a query string "?EDIT=1"
and then on page_load I check
if (!Page.IsPostBack)
{
if ((Request.QueryString["EDIT"] != null) && (Request.QueryString["EDIT"] == "1"))
{
GetCurrentSandwiches();
}
RemoveFilling.Enabled = false;
RemoveCondiment.Enabled = false;
CID = int.Parse(Session["CID"].ToString());
}
GetCurrentSandwiches does the following:
protected void GetCurrentSandwiches()
{
sandwichOnOrder.Clear();
OrderManagement order = new OrderManagement();
List<SarnieIngredients> bob = order.GetSandwichesOnOrder(int.Parse(Session["OID"].ToString()));
List<int> ingredientIDs = new List<int>();
foreach (SarnieIngredients sarnie in bob)
{
List<Ingredients> batchOfIngredients = new List<Ingredients>();
double amount = 0.00;
ingredientIDs = sarnie.IngredientIDs;
foreach (int i in ingredientIDs)
{
Ingredients contents = ingredient.GetDesiredIngredientByID(i);
batchOfIngredients.Add(contents);
amount += contents.Price;
}
SandwichConentent sandwich = new SandwichConentent(batchOfIngredients, amount);
sandwichOnOrder.Add(sandwich);
CompletedSandwiches.Items.Add(sandwich.ToString());
}
and my remove buttons code does
bool found = false;
int i = 0;
while (i < sandwichOnOrder.Count || found == false)
{
SandwichConentent content = sandwichOnOrder[i];
if (content.ToString() == CompletedSandwiches.SelectedItem.Text)
{
CompletedSandwiches.Items.Remove(CompletedSandwiches.SelectedItem);
sandwichOnOrder.Remove(content);
found = true;
}
}
does anyone have any idea why I am having this problem?
thanks in advance Matt
edit ~ sorry I forgot to mention I know I haven't put validation in but I check manually that there's a item selected before I pass and when looking at the listbox in debug it is apparently null even though visually it is not.
edit 2 ~ sorry again the error occurs on this line
if (content.ToString() == CompletedSandwiches.SelectedItem.Text)
and when ?EDIT=1 is passed, it is a new page call from the main page of the web app