Sorry about the title, I don’t think I could explain it right:
This is a simplified example class, this is working ok. (I also have a Save() method)
public class busItem
{
public Item vItem;
public busItem(int pItem_Id)
{
DBDataContext db = new DBDataContext();
Item vItemQuery = (from i in db.Items
where i.Id == pItem_Id
select i).FirstOrDefault();
vItem = new Item();
vItem.Id = vItemQuery.Id;
vItem.Desc = vItemQuery.Desc;
}
}
And this is my code-behind call:
busItem item = new busItem(1);
item.vItem.Desc = "new description";
the problem is that when I try passing the “new description”, i get a “null reference” exception. How can I do that?
First of all, that code doesn’t look right for at least 2 reasons. pItem in the query is not declared; did you mean pItem_Id? Also, the line with “new description” doesn’t end with a semicolon. Unless I’m looking at the exact code you have, there’s a good chance the actual problem win’t be visible. Secondly, I suspect the error is not actually when you assign “new description” but when you assign vItem.Desc = vItemQuery.Desc. I don’t see how the “new description” line could be a problem, but if the query returned null because it couldn’t find the requested object, you’d get an error when trying to get the original/default description.
Edit: Are you sure you didn’t exclude some significant piece of code like declaring a local instance of vItem within the constructor?