Solved

ListBox is null. How can I initialize it?

Posted on 2013-05-29
10
466 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 75

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 75

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 75

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
 

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 75

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

628 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