Solved

ListBox is null. How can I initialize it?

Posted on 2013-05-29
10
459 Views
Last Modified: 2013-05-29
There is no error.

However the listbox does not get populated.

private void FillEvalItemNameList()
        {
            lstEvalItemName = FindControl("lstEvalItemName") as ListBox;

Error:  lstEvalItemName is null. How can I initialize it?
                     
            string selectSQL = "SELECT EvalItemID, EvalItemName FROM EvalItems WHERE isActive = 1 ORDER BY EvalItemName";

            string strConnection = connString.ToString();

            SqlConnection conStockSelect = new SqlConnection(strConnection);

            // Try to open the connection.
            conStockSelect.Open();

            SqlCommand cmd = new SqlCommand(selectSQL, conStockSelect);
            lstEvalItemName.DataSource = cmd.ExecuteReader();
            lstEvalItemName.DataTextField = "EvalItemName";
            lstEvalItemName.DataValueField = "EvalItemID";
            lstEvalItemName.DataBind();
            conStockSelect.Close();
            cmd.Dispose();
        }

The connection opens.

The string selectSQL  returns 9 rows in the SQL Server management studio as expected.
0
Comment
Question by:Dovberman
  • 6
  • 4
10 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39205574
I'll ask the obvious:  Are you sure lstEvalItemName is a ListBox? The as opertator will return null if it cannot cast the object to the target type. Set a breakpoint on that line and try running the following in the Immediate Window:

?FindControl("lstEvalItemName")

Open in new window

0
 

Author Comment

by:Dovberman
ID: 39205831
I am sure that lstEvalItemName is a Listbox.

lstEvalItemName = FindControl("lstEvalItemName") as ListBox;

The properties list is correct.

BTW. What happened to the Immediate Window in VS 2012?  I have not yet found it.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39205850
BTW. What happened to the Immediate Window in VS 2012?  I have not yet found it.
Ctrl-Alt-I (eye)  or Debug->Windows->Immediate Window (while in debug mode--i.e. on a breakpoint).
0
 

Author Comment

by:Dovberman
ID: 39205863
I found the immediate window.  It's called quick watch now.

lstEvalItemName = FindControl("lstEvalItemName") as ListBox;
 
lstEvalItemName is a list box, but how do I keep it from being a null object?

I need a statement to create a new instance of lstEvalItemName

lstEvalItemName.Items.Clear(); //Error when lstEvalItemName is null.

Thanks,
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39205872
I found the immediate window.  It's called quick watch now.
Quick Watch is something different. You need the actual Immediate Window (see above).

I am not asking about the lstEvalItemName variable as to whether or not it is a ListBox. I am asking if the "lstEvalItemName" in FindControl("lstEvalItemName") is a ListBox. That is why I am asking you to run my previous suggestion in the Immediate Window.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Dovberman
ID: 39205906
The watch list and the immediate window both return:

      lstEvalItemName      null      System.Web.UI.WebControls.ListBox

Yes, it is really a ListBox.

lstEvalItemName is a list box, but how do I keep it from being a null object?

I need a statement to create a new instance of lstEvalItemName
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39205935
I need a statement to create a new instance of lstEvalItemName
Well, you can do:

lstEvalItemName = new ListBox();

...but I don't think it's going to help you. You need to investigate why FindControl("lstEvalItemName") is not returning you a ListBox.
0
 

Author Comment

by:Dovberman
ID: 39205968
FindControl("lstEvalItemName") is returning a ListBox that is null.
0
 

Author Comment

by:Dovberman
ID: 39205992
lstEvalItemName = FindControl("lstEvalItemName") as ListBox;
            lstEvalItemName = new ListBox();


            lstEvalItemName.Items.Clear();  // This works now.  I created a new instance of lstEvalItemName which is not null.

A lot of code that worked in VS 2008 and Windows 7, needs to be modified to run in VS 2012 and Windows 8.

I expect to see patches and new SP's.

MSFT  is obviously planning on competing for mobile app development.
0
 

Author Closing Comment

by:Dovberman
ID: 39205996
Thanks for your help.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now