Solved

ListBox is null. How can I initialize it?

Posted on 2013-05-29
10
461 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 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
 

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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:…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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…

867 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

19 Experts available now in Live!

Get 1:1 Help Now