Object reference not set to an instance of an object.

Posted on 2007-03-30
Medium Priority
Last Modified: 2008-02-01
I have a webpage with which I'm loading data into a listbox called "ClientListBox".  The page loads without a problem when I open a Client that does not have any items that are supposed to be selected in the ClientListBox.  If there is supposed to be a client Selected, then I get the error:  Object reference not set to an instance of an object.

I've listed all of the code that this function involves and highlighted the line that gives me the error.  I'm hoping there's a simple solution that I've somehow overlooked doing something.  Thank you!

void Page_LoadComplete(object sender, EventArgs e)
        if (CurrentProject != null)

protected void SelectProjectClients(int projectId)
        List<string> clientList = Project.GetProjectClients(projectId);

        foreach (string client in clientList)
                           ListItem item = ClientList.Items.FindByValue(client);
 ERROR----------> item.Selected = true;

        public static List<string> GetProjectClients(int Id)
            if (Id <= DefaultValues.GetProjectIdMinValue())
                return (new List<string>());

            DataAccess DALLayer = DataAccessHelper.GetDataAccess();
            return (DALLayer.GetProjectClients(Id));

        public override List<string> GetProjectClients(int Id)
            if (Id <= 0)
                throw (new ArgumentOutOfRangeException("Id"));

            SqlCommand sqlCmd = new SqlCommand();
            AddParamToSQLCmd(sqlCmd, "@ProjectId", SqlDbType.Int, 0, ParameterDirection.Input, Id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SP_PROJECT_GETPROJECTCLIENTS);
            List<string> clientList = new List<string>();
            TExecuteReaderCmd<string>(sqlCmd, TGenerateClientListFromReader<string>, ref clientList);
            return clientList;

private const string SP_PROJECT_GETPROJECTCLIENTS = "GetProjectClients";

CREATE  PROCEDURE [dbo].[GetProjectClients]
      @ProjectId INT

INNER JOIN Clients ON aspnet_starterkits_ProjectClients.ClientId = Clients.ClientId
Question by:deloused
LVL 14

Accepted Solution

steveberzins earned 2000 total points
ID: 18827382
a few problems.

-you're attempting to look for a value that there is no way it's gonna be there from what I can tell
-you need to select the client id, and name in the select (in the proc), and tell the list which column is going to be the value source, and which will be the display value source
-even if the value is there, and should be found, you should code defensively, and check to make sure an object is not null before accessing/setting a property on it.
if (null != item) item.Selected = true;

Author Comment

ID: 18827432
Aggh!! woo!  

Sorry, your reply gave me the solution....thank you.  I forgot to mention that the ClientListBox has a sql data source which gets ALL of the clients in the database table.  Of course I had set the DataTextField to display"Name" DataValue Field save "ClientId".  I changed the value field to "Name" and now it works beautifully.  

thank you!

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
From store locators to asset tracking and route optimization, learn how leading companies are using Google Maps APIs throughout the customer journey to increase checkout conversions, boost user engagement, and optimize order fulfillment. Powered …
Suggested Courses

597 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