Solved

Object reference not set to an instance of an object.

Posted on 2007-03-30
2
341 Views
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)
        {
            SelectProjectClients(CurrentProject.Id);
        }
    }

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

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


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

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


//DALLayer.GetProjectClients
        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";

//storedprocedure
CREATE  PROCEDURE [dbo].[GetProjectClients]
      @ProjectId INT
AS

SELECT
 Clients.Name
From
 aspnet_starterkits_ProjectClients
INNER JOIN Clients ON aspnet_starterkits_ProjectClients.ClientId = Clients.ClientId
WHERE
 ProjectId=@ProjectId
SET QUOTED_IDENTIFIER OFF
0
Comment
Question by:deloused
2 Comments
 
LVL 14

Accepted Solution

by:
steveberzins earned 500 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;
0
 

Author Comment

by:deloused
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!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

18 Experts available now in Live!

Get 1:1 Help Now