public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public virtual List<ProductPicture> Picture { get; set; }
public virtual List<Supplier> Supplier { get; set; }
}
Here my Picture Repository
public List<ProductPicture> GetProductPhotos(string pid)
{
sp = new SqlProvider();
List<ProductPicture> piclist = new List<ProductPicture>();
sp.AddParamatersString("@pid", pid);
DataTable dt = sp.ExecuteMethod("select * from Products as p join ProductPictures as pic on p.ID=pic.Product_ID where p.ID=@pid");
foreach (DataRow item in dt.Rows)
{
ProductPicture pic = new ProductPicture();
pic.ID = (int)item["ID"];
pic.ProductID= Convert.ToInt16(pid);
pic.Path = item["Path"].ToString();
pic.IsDefault =Convert.ToBoolean( item["IsDefault"]);
piclist.Add(pic);
}
return piclist.ToList<ProductPicture>();
}
Here Product Repository
public IList<Product> GetAllProduct()
{
sp = new SqlProvider();
ProductPictureRepository pPictureRepository = new ProductPictureRepository();
List<ProductPicture> piclist = new List<ProductPicture>();
CategoryRepository catRepository = new CategoryRepository();
List<Product> plist = new List<Product>();
DataTable dt = sp.ExecuteMethod("select * from Products");
foreach (DataRow item in dt.Rows)
{
Product p = new Product();
p.ID = (int)item["ID"];
piclist.AddRange((List<ProductPicture>)pPictureRepository.GetProductPhotos(p.ID.ToString()));
p.Brandid = (int)item["Brand_ID"];
p.Category = catRepository.GetCategorywithPid(p.ID.ToString()) as Category;
p.IsNew = Convert.ToBoolean(item["IsNew"]);
p.IsRecommended = Convert.ToBoolean(item["IsRecommended"]);
p.IsSpecial = Convert.ToBoolean(item["IsSpecial"]);
p.Name = item["Name"].ToString();
p.Picture.AddRange(piclist);
p.OnOrder = Convert.ToInt32(item["OnOrder"]);
p.CreatedDate = Convert.ToDateTime(item["CreatedDate"]);
plist.Add(p);
}
return plist;
}
I am Getting error in the underlined Line "NullReferenceException was unhandled by user code"
I cant Add any item on P.Picture i try every way But i cant add any item on this p.picture
In the constructor for the product do you initialize the list? If not, try Picture = new List<ProductPicture>() in the constructor.
Usually this error comes about when you are trying to work with a property or variable that isn't initialized yet. In other words, p.Picture is a null reference exception because p.Picture hasn't been initialized with a new list.
Not guaranteed that this is it, but it's worth a shot.
Don't forget to mark useful responses as Answer if they helped you towards a solution.
In the constructor for the product do you initialize the list? If not, try Picture = new List<ProductPicture>() in the constructor.
Usually this error comes about when you are trying to work with a property or variable that isn't initialized yet. In other words, p.Picture is a null reference exception because p.Picture hasn't been initialized with a new list.
Not guaranteed that this is it, but it's worth a shot.
p.Picture = piclist.ToList();
i equalize that 2 list (no to add ) and this work .. For now..
Marked as answer by Mstkk on Feb 25, 2013 01:27 PM
Mstkk
Member
9 Points
42 Posts
I cant add any item to the list I cant add list to list NullReferenceException
Feb 24, 2013 09:28 PM|LINK
public class Product { public int ID { get; set; } public string Name { get; set; } public virtual List<ProductPicture> Picture { get; set; } public virtual List<Supplier> Supplier { get; set; } }Here my Picture Repository
public List<ProductPicture> GetProductPhotos(string pid) { sp = new SqlProvider(); List<ProductPicture> piclist = new List<ProductPicture>(); sp.AddParamatersString("@pid", pid); DataTable dt = sp.ExecuteMethod("select * from Products as p join ProductPictures as pic on p.ID=pic.Product_ID where p.ID=@pid"); foreach (DataRow item in dt.Rows) { ProductPicture pic = new ProductPicture(); pic.ID = (int)item["ID"]; pic.ProductID= Convert.ToInt16(pid); pic.Path = item["Path"].ToString(); pic.IsDefault =Convert.ToBoolean( item["IsDefault"]); piclist.Add(pic); } return piclist.ToList<ProductPicture>(); }Here Product Repository
public IList<Product> GetAllProduct() { sp = new SqlProvider(); ProductPictureRepository pPictureRepository = new ProductPictureRepository(); List<ProductPicture> piclist = new List<ProductPicture>(); CategoryRepository catRepository = new CategoryRepository(); List<Product> plist = new List<Product>(); DataTable dt = sp.ExecuteMethod("select * from Products"); foreach (DataRow item in dt.Rows) { Product p = new Product(); p.ID = (int)item["ID"]; piclist.AddRange((List<ProductPicture>)pPictureRepository.GetProductPhotos(p.ID.ToString())); p.Brandid = (int)item["Brand_ID"]; p.Category = catRepository.GetCategorywithPid(p.ID.ToString()) as Category; p.IsNew = Convert.ToBoolean(item["IsNew"]); p.IsRecommended = Convert.ToBoolean(item["IsRecommended"]); p.IsSpecial = Convert.ToBoolean(item["IsSpecial"]); p.Name = item["Name"].ToString(); p.Picture.AddRange(piclist); p.OnOrder = Convert.ToInt32(item["OnOrder"]); p.CreatedDate = Convert.ToDateTime(item["CreatedDate"]); plist.Add(p); } return plist; }I am Getting error in the underlined Line "NullReferenceException was unhandled by user code"
I cant Add any item on P.Picture i try every way But i cant add any item on this p.picture
Mstkk
Member
9 Points
42 Posts
Re: I cant add any item to the list I cant add list to list NullReferenceException
Feb 24, 2013 09:34 PM|LINK
That Lines Doesnt Work !
This Line Doesnt Work They are same codes ! just different p.picture on my Product class like this
public virtual List<ProductPicture> Picture { get; set; }markfitzme
All-Star
15367 Points
2365 Posts
Re: I cant add any item to the list I cant add list to list NullReferenceException
Feb 24, 2013 11:27 PM|LINK
In the constructor for the product do you initialize the list? If not, try Picture = new List<ProductPicture>() in the constructor.
Usually this error comes about when you are trying to work with a property or variable that isn't initialized yet. In other words, p.Picture is a null reference exception because p.Picture hasn't been initialized with a new list.
Not guaranteed that this is it, but it's worth a shot.
Mstkk
Member
9 Points
42 Posts
Re: I cant add any item to the list I cant add list to list NullReferenceException
Feb 25, 2013 01:27 PM|LINK
i equalize that 2 list (no to add ) and this work .. For now..