Heard'em all, but yet another Object reference not set to an instance of an object in Linq-to-SQL
-
Friday, October 10, 2008 4:01 AM
This is the gist of my intent. I want to retrieve a single value from one of my tables in the database so I can then perform simple calculations with it. The value is an IQueryable<decimal> and I need it as a decimal. However, the compiler does not like what I am doing and it throws an Object reference not set to an instance of an object. Here is what I have hopefully it is apparent from the snippet:
WSCDataClassesDataContext dc = new WSCDataClassesDataContext(); //pull the item currently displayed in the item detailsview to help fill out the new row in the CART decimal i = dc.ITEMs.First(item => item.Item_Cat_No == (Guid)catItemDV.SelectedValue).Item_Unit_Price;I think the problem lies somewhere in the end of the last line. Probably my DetailsView.SelectedValue may be empty. Any thoughts?
This is how the ITEM class looks like
[
Column(Storage="_Item_Unit_Price", DbType="Money NOT NULL")] public decimal Item_Unit_Price{
get{
return this._Item_Unit_Price;}
set{
if ((this._Item_Unit_Price != value)){
this.OnItem_Unit_PriceChanging(value); this.SendPropertyChanging(); this._Item_Unit_Price = value; this.SendPropertyChanged("Item_Unit_Price"); this.OnItem_Unit_PriceChanged();}
}
}
Thanks
All Replies
-
Friday, October 10, 2008 4:12 AM
Never mind. My DV.SelectedValue was empty. I forgot to declare the DataKeyNames property on the DetailsView control.
Duh.
-
Friday, October 10, 2008 8:56 PM
If you expect a single result you could use the Single method rather than First. It will raise an exception if you don't get exactly one result making crystal clear you don't get what you expect...
--
Patrice
-
Saturday, October 11, 2008 5:47 PMThanks, Patrice. I will give that a try.

