Click here to Skip to main content
15,942,094 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have got an error when try the following code.
Error Message: Value cannot be null. Parameter name: source

These are the lines where I got the error.

FoodTypeResponseDto selectedFoodTypes = this.FoodTypes.FirstOrDefault(r => r.FoodTypeId == foodTypeID);

How to sort it out?

Thanks in advance.

C#
protected void RecipeManagementUserControl_RepeaterItemClick(object sender, CommandEventArgs e)
{

        if (e.CommandName == WellKnownCommandNames.Edit.ToString())
        {
           
            int foodTypeID = 0;
            if (!string.IsNullOrEmpty(e.CommandArgument.ToString()))
            {
                int.TryParse(e.CommandArgument.ToString(), out foodTypeID);

                this.FoodTypeID = foodTypeID;
                this.FoodType = ((LinkButton)(((RepeaterCommandEventArgs)(e)).CommandSource)).Text;

                LinkButton linkButton = (LinkButton)FindControl("foodTypeActionLinkButton");

                FoodTypeResponseDto selectedFoodTypes = this.FoodTypes.FirstOrDefault(r => r.FoodTypeId == foodTypeID);

                this.FoodType = selectedFoodTypes.Name;
                this.FoodTypeIndexID = selectedFoodTypes.IndexID;

                this.GetMultipleFoodData();
            }
        }
}
Posted
v2

1 solution

First or default is just that, it returns the first item or default for the type if it cannot find it. I'm guessing it can't find it, and the default value for reference types is null. On your "this.foodType = selectedFoodTypes.Name", the selectedFoodTypes is null and giving you the error there, just before that line you need to check if its null before that.
 
Share this answer
 
Comments
Krishnananthan Vijayaretnam 13-Jun-13 23:31pm    
Thanks. I've sort it out. This is how I achieved it. Instead of using the RepeaterItemClick, I've used the ItemCommand...

protected void FoodTypeLeftNavigationRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{

if (e.CommandName == WellKnownCommandNames.Edit.ToString())
{
this.selectedFoodTypeNameDisplayLabel.Text = string.Empty;

int foodTypeID = 0;

int.TryParse(e.CommandArgument.ToString(), out foodTypeID);

if (foodTypeID > -1)
{

this.FoodTypeID = foodTypeID;

this.FoodType = ((LinkButton)(e.CommandSource)).Text;
string foodTypeIndex = ((Label)e.Item.Controls[1]).Text;

int foodTypeIndexId = this.FoodTypeIndexID;
if (!string.IsNullOrEmpty(foodTypeIndex))
{
int.TryParse(foodTypeIndex, out foodTypeIndexId);
}

this.FoodTypeIndexID = foodTypeIndexId;

string foodMealType = ((Label)e.Item.Controls[3]).Text;
int foodMealTypeID = 1;

if (!string.IsNullOrEmpty(foodMealType))
{
int.TryParse(foodMealType, out foodMealTypeID);
}

this.FoodMealType = foodMealTypeID;

this.GetMultipleFoodData();

this.foodTypeLeftNavigationRepeater.DataSource = (List<foodtyperesponsedto>)this.FoodTypeList;
this.foodTypeLeftNavigationRepeater.DataBind();

}
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900