I am having an issue that keeps giving me grief. I am working on this E-Commerce project and the way that it's set up is to use a table variable to create it to do some comparison functions for a wish list feature I am working on. Pretty much if the best price coming from the MasterProduct table is less then the currently stored price, get that value and store that ID.
The issue is that I am always getting a null reference error when I try to use this. I am very new to creating table variables in SQL so I am completely lost as to what to do. Could anyone be able to help me understand how I could fix this issue?
CREATE PROCEDURE [dbo].[DAL_Wishlist_SelectAllPersonIDByPriceDrop]
(@iErrorCode INT OUTPUT)
AS
SET NOCOUNT ON
DECLARE @MasterProductIDTable TABLE
(
MasterProductID [int] NOT NULL,
PRIMARY KEY (MasterProductID)
)
INSERT INTO @MasterProductIDTable (MasterProductID)
SELECT DISTINCT [MasterProduct].[MasterProductID]
FROM [dbo].[Wishlist], [dbo].[MasterProduct]
WHERE [MasterProduct].[MasterProductID] = [Wishlist].[MasterProductID]
AND [MasterProduct].[BestPrice] < [Wishlist].[PreviousCost]
SELECT DISTINCT [Wishlist].[PersonID]
FROM [dbo].[Wishlist], @MasterProductIDTable mpt
WHERE [Wishlist].[MasterProductID] = mpt.MasterProductID
SELECT @iErrorCode = @@ERROR
GO
The stored procedure is rather simple in that it's:
private List<Wishlist> _loadAllFromDataTable(DataTable dt)
{
List<Wishlist> list = new List<Wishlist>();
for (int index = 0; index < dt.Rows.Count; index++)
{
list.Add(_loadFromDTRow(index, dt));
}
return list;
}
private Wishlist _loadFromDTRow(int row, DataTable dt)
{
if (dt.Rows.Count > 0)
{
Wishlist wishlist = new Wishlist();
wishlist.ID = (int)dt.Rows[row]["ID"];
wishlist.PersonID = (int)dt.Rows[row]["PersonID"];
wishlist.MasterProductID = (int)dt.Rows[row]["MasterProductID"];
wishlist.DateAdded = (DateTime)dt.Rows[row]["DateAdded"];
wishlist.PreviousCost = (decimal)dt.Rows[row]["PreviousCost"];
return wishlist;
}
else
return null;
}
And:
public List<int> SelectAllPersonIDByPriceDrop()
{
SqlCommand scmCmdToExecute = new SqlCommand();
scmCmdToExecute.CommandText = "dbo.[DAL_Wishlist_SelectAllPersonIDByPriceDrop]";
scmCmdToExecute.CommandType = CommandType.StoredProcedure;
scmCmdToExecute.Parameters.Add(new SqlParameter("@iErrorCode", SqlDbType.Int, 4, ParameterDirection.Output, true, 10, 0, "", DataRowVersion.Proposed, (SqlInt32)0));
DataTable dt = SqlDB.ExecuteQuery(scmCmdToExecute);
List<int> masterProductIDList = new List<int>();
for (int index = 0; index < dt.Rows.Count; index++)
{
int masterProductID = (int)dt.Rows[index]["PersonID"];
masterProductIDList.Add(masterProductID);
}
return masterProductIDList;
}
Any help would be GREATLY appreciated.
Thanks.