473,383 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

ListBox.Items.Clear() causing System.NullReferenceException...

4 3730
Would you post your code for lstProduct_SelectedIndexChanged()?

bill

"Eeediot" <ee*****@hotmail.com> wrote in message
news:fI********************@giganews.com...
Hello, Folks!!

Here's the problem:
.. ASP.NET 1.1
.. Running on Windows Server 2000
.. Using WebMatrix to write the code...and sometimes Notepad!
.. 2 Web Objects (ListBoxes) in question
.. Information pulled from MS-SQL server through DataReader

In my application / page I have a listbox, lstCategory, that is
automatically populated when the page is loaded. Based on the selection
from lstCategory it will fill another listbox, lstProduct. When the user
selects from lstProduct it goes and does other things.

When my application tries to populate lstProduct after selecting fom
lstCategory I get the error, "System.NullReferenceException. Object
reference not set to an instance of an object. An unhandled exception..."

With the stack trace:
[NullReferenceException: Object reference not set to an instance of an
object.]
Product.lstProduct_SelectedIndexChanged(Object sender, EventArgs e) +49
System.Web.UI.WebControls.ListControl.OnSelectedIn dexChanged(EventArgs e)
+108

System.Web.UI.WebControls.ListBox.System.Web.UI.IP ostBackDataHandler.RaisePo
stDataChangedEvent() +26
System.Web.UI.Page.RaiseChangedEvents() +115
System.Web.UI.Page.ProcessRequestMain() +1081

What the Fudge!?!

I have tried searching for a solution on various sites but no luck, yet.
I know what triggers the problem. Before I populate lstProduct I perform
the command lstProduct.Items.Clear() to clear the list. When I comment out
the line it works fine but keeps adding to lstProduct with a whole bunch of
values that shouldn't be there. Any insights are welcome.

TIA.

Here's some of the code:

' Product.vb
'

Imports Microsoft.VisualBasic
Imports blah, blah, blah.

Public Class Product
'For PostBack
Inherits Page

Protected WithEvents lstCategory as ListBox
Protected WithEvents lstProduct as ListBox
Nov 18 '05 #2
Nov 18 '05 #3
Nov 18 '05 #4
Jeremy's explanation was a good one. If there is nothing selected in the
list box the SelectedIndex will be -1. That entails SelectedItem will be
Nothing, and Nothing does not have a property named Value so it blows up.

As far as what it takes to get a -1 for SelectedIndex in the SelectedIndex
changed event. I could only reproduce it two ways.

1) You have a multi select, and the user is ctrl-clicking the last one
selected, thus clearing out their selection.
2) Somewhere in your logic you are calling your FillProductList() or
otherwise repopulating the data.
or (I didn't test this one.)
3) If you turned off viewstate for the page or controls, I believe it will
hiccup.

bill
"Eeediot" <ee*****@hotmail.com> wrote in message
news:Ft********************@giganews.com...
Addendum:

Okay, I have placed an If statement in lstProduct_SelectedIndexChanged()
that reads as follows:

Private Sub lstProduct_SelectedIndexChanged()
If NOT(lstProduct.SelectedIndex = -1) Then
Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Patty O'Dors | last post by:
Hi I have some code to create an ownerdrawn listbox (derived), and when I add an item to it, the bold text of the first item (the title, 'Collections and Maturities') mysteriously seems to get...
2
by: xzzy | last post by:
I have an app that uses a listbox to display program status messages, and a public function to be used by the solution to update the listbox. I do not know how to make the listbox public so it...
10
by: yop | last post by:
All When I try to get the text from my listbox I am get an error which is listed below. Any ideas? Thanks Object reference not set to an instance of an object.
1
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What...
1
by: yamne | last post by:
I have a problem. When I click in edit datagrid button I show two listbox and two button. I use two button to move data between two listbox. My problem is that I can't call the listbox in the...
7
by: 00_ChInkPoIntD12 | last post by:
Can anyone confirm there isn't a Sort() method for WebControl Listbox in Asp.net? It is rather simple to write a method to do the sorting, but just wondering I shouldn't invent the wheel if...
3
by: Ali Chambers | last post by:
Hi, I have created a listbox called "dtlist1" on my VB.NET form. I call a procedure as follows: Private Sub openfile(flname As String) dtlist1.Items.Clear() etc..
3
by: papa smerf | last post by:
'Here is some of the code I have so far. Dim Contacts(25, 4) As String This is where I add to the listbox Private Sub btnAddToList_Click(ByVal sender As System.Object, ByVal e As...
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.