[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Control reference lost.

Posted on 2013-05-29
4
Medium Priority
?
451 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
3 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 1200 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

872 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