?
Solved

Control reference lost.

Posted on 2013-05-29
4
Medium Priority
?
449 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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 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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

777 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