Solved

Control reference lost.

Posted on 2013-05-29
4
446 Views
Last Modified: 2013-05-31
lstEvalItemName is a ListBox.

Code behind:
lstEvalItemName = FindControl("lstEvalItemName") as ListBox;

     private void FillEvalItemNameList()
        {
            lstEvalItemName.Items.Clear();

Open in new window

Runtime error:

 HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=StockProNew
  StackTrace:
       at StockProNew.Default1.FillEvalItemNameList() in c:\Users\Dovberman\Documents\Visual Studio 2012\Projects\StockProNew\StockProNew\Default1.aspx.cs:line 298
       at StockProNew.Default1.Page_Load(Object sender, EventArgs e) in c:\Users\Dovberman\Documents\Visual Studio 2012\Projects\StockProNew\StockProNew\Default1.aspx.cs:line 158
       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:

I changed it to:
ListBox lstEvalItemName = new ListBox();
lstEvalItemName.Items.Clear();

Open in new window

Why was this necessary?

Thanks,
0
Comment
Question by:Dovberman
  • 2
4 Comments
 

Author Comment

by:Dovberman
ID: 39205493
There is no error.

However the listbox does not get populated.

private void FillEvalItemNameList()
        {
            lstEvalItemName = FindControl("lstEvalItemName") as ListBox;
            lstEvalItemName.Items.Clear();
                     
            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 query as expected.
0
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 300 total points
ID: 39207624
If your code-behind really is trying to grab a reference to the control at instance scope it's probably too early, my guess is that the control isn't even created yet at that point. Do it inside the method where you need the reference, i.e.

private void FillEvalItemNameList()
{
    ListBox lstReference = FindControl("lstEvalItemName") as ListBox;
    lstReference.Items.Clear();
}

Open in new window


Although, now that I think about it I'm not sure why you have to use FindControl at all. If this is a webforms application with a server-side listbox on it (i.e. <asp:ListBox runat="server">) you should have a reference to it already.

private void FillEvalItemNameList()
{
    lstEvalItemName.Items.Clear();
}

Open in new window

0
 

Author Closing Comment

by:Dovberman
ID: 39211363
It's my mistake.  I should have created a new project by selecting the Webform instead of the empty website project type.
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

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…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

735 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