Solved

Control reference lost.

Posted on 2013-05-29
4
445 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…

808 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