Note that there are some explanatory texts on larger screens.

plurals
  1. POListbox is null, when passing a query string and processing data on load
    text
    copied!<p>Hi guys got a wired problem (well I find it a wired problem :P)</p> <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</p> <blockquote> <p>System.NullReferenceException: Object reference not set to an instance of an object.</p> <p>on CompletedSandwiches.SelectedItem.Text</p> </blockquote> <p>the only difference is that I pass a query string "?EDIT=1"</p> <p>and then on page_load I check</p> <pre><code> if (!Page.IsPostBack) { if ((Request.QueryString["EDIT"] != null) &amp;&amp; (Request.QueryString["EDIT"] == "1")) { GetCurrentSandwiches(); } RemoveFilling.Enabled = false; RemoveCondiment.Enabled = false; CID = int.Parse(Session["CID"].ToString()); } </code></pre> <p>GetCurrentSandwiches does the following:</p> <pre><code> protected void GetCurrentSandwiches() { sandwichOnOrder.Clear(); OrderManagement order = new OrderManagement(); List&lt;SarnieIngredients&gt; bob = order.GetSandwichesOnOrder(int.Parse(Session["OID"].ToString())); List&lt;int&gt; ingredientIDs = new List&lt;int&gt;(); foreach (SarnieIngredients sarnie in bob) { List&lt;Ingredients&gt; batchOfIngredients = new List&lt;Ingredients&gt;(); 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()); } </code></pre> <p>and my remove buttons code does</p> <pre><code> bool found = false; int i = 0; while (i &lt; sandwichOnOrder.Count || found == false) { SandwichConentent content = sandwichOnOrder[i]; if (content.ToString() == CompletedSandwiches.SelectedItem.Text) { CompletedSandwiches.Items.Remove(CompletedSandwiches.SelectedItem); sandwichOnOrder.Remove(content); found = true; } } </code></pre> <p>does anyone have any idea why I am having this problem?</p> <p>thanks in advance Matt</p> <p>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. </p> <p>edit 2 ~ sorry again the error occurs on this line</p> <pre><code>if (content.ToString() == CompletedSandwiches.SelectedItem.Text) </code></pre> <p>and when ?EDIT=1 is passed, it is a new page call from the main page of the web app</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload