Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ListBox is null. How can I initialize it?

Posted on 2013-05-29
10
Medium Priority
?
471 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
Independent Software Vendors: 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 2000 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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

773 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