Solved

ListBox is null. How can I initialize it?

Posted on 2013-05-29
10
463 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

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.

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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