Solved

Control reference lost.

Posted on 2013-05-29
4
442 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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now