deloused
asked on
Object reference not set to an instance of an object.
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(Curre ntProject. Id);
}
}
protected void SelectProjectClients(int projectId)
{
ClientListBox.DataBind();
List<string> clientList = Project.GetProjectClients( projectId) ;
foreach (string client in clientList)
{
ListItem item = ClientList.Items.FindByVal ue(client) ;
ERROR----------> item.Selected = true;
}
}
//Project.GetProjectClient s
public static List<string> GetProjectClients(int Id)
{
if (Id <= DefaultValues.GetProjectId MinValue() )
return (new List<string>());
DataAccess DALLayer = DataAccessHelper.GetDataAc cess();
return (DALLayer.GetProjectClient s(Id));
}
//DALLayer.GetProjectClien ts
public override List<string> GetProjectClients(int Id)
{
if (Id <= 0)
throw (new ArgumentOutOfRangeExceptio n("Id"));
SqlCommand sqlCmd = new SqlCommand();
AddParamToSQLCmd(sqlCmd, "@ProjectId", SqlDbType.Int, 0, ParameterDirection.Input, Id);
SetCommandType(sqlCmd, CommandType.StoredProcedur e, SP_PROJECT_GETPROJECTCLIEN TS);
List<string> clientList = new List<string>();
TExecuteReaderCmd<string>( sqlCmd, TGenerateClientListFromRea der<string >, ref clientList);
return clientList;
}
private const string SP_PROJECT_GETPROJECTCLIEN TS = "GetProjectClients";
//storedprocedure
CREATE PROCEDURE [dbo].[GetProjectClients]
@ProjectId INT
AS
SELECT
Clients.Name
From
aspnet_starterkits_Project Clients
INNER JOIN Clients ON aspnet_starterkits_Project Clients.Cl ientId = Clients.ClientId
WHERE
ProjectId=@ProjectId
SET QUOTED_IDENTIFIER OFF
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(Curre
}
}
protected void SelectProjectClients(int projectId)
{
ClientListBox.DataBind();
List<string> clientList = Project.GetProjectClients(
foreach (string client in clientList)
{
ListItem item = ClientList.Items.FindByVal
ERROR----------> item.Selected = true;
}
}
//Project.GetProjectClient
public static List<string> GetProjectClients(int Id)
{
if (Id <= DefaultValues.GetProjectId
return (new List<string>());
DataAccess DALLayer = DataAccessHelper.GetDataAc
return (DALLayer.GetProjectClient
}
//DALLayer.GetProjectClien
public override List<string> GetProjectClients(int Id)
{
if (Id <= 0)
throw (new ArgumentOutOfRangeExceptio
SqlCommand sqlCmd = new SqlCommand();
AddParamToSQLCmd(sqlCmd, "@ProjectId", SqlDbType.Int, 0, ParameterDirection.Input, Id);
SetCommandType(sqlCmd, CommandType.StoredProcedur
List<string> clientList = new List<string>();
TExecuteReaderCmd<string>(
return clientList;
}
private const string SP_PROJECT_GETPROJECTCLIEN
//storedprocedure
CREATE PROCEDURE [dbo].[GetProjectClients]
@ProjectId INT
AS
SELECT
Clients.Name
From
aspnet_starterkits_Project
INNER JOIN Clients ON aspnet_starterkits_Project
WHERE
ProjectId=@ProjectId
SET QUOTED_IDENTIFIER OFF
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!