Click here to Skip to main content
15,894,540 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey Everyone,

I have a SQL procedure that does a join to return a bunch of objects and some secondary stored procedure executions that will fill up next results of the reference objects in the main class.

Everything is working great, but when I do a join to return the main class and reference class, I am trying to do next results and fill up the object but without luck. I have tried multiple things but nothing seems to work. Any help would be great as I would like to minimize the trips to the DB.

Some of the example code below
C#
//levelstamp - reference class pulled in the main SQL
if (reader.NextResult())
{
   if (returnLevel.levelStamps == null)
      returnLevel.levelStamps = new List<levelstamp>();

   returnLevel.levelStamps = reader.ToLevelStampList();
}

//method
public static List<levelstamp> ToLevelStampList(this IDataReader dataReader)
{
   var returnList = new List<levelstamp>();
   while (dataReader.Read())
   {
      LevelStamp levelStamps = new LevelStamp()
      {
         LevelStampApplyPreferences = Boolean.Parse(dataReader["LevelStampApplyPreferences"].ToString()),
         LevelId = Int32.Parse(dataReader["LevelId"].ToString()),
         LevelStampIsActive = Boolean.Parse(dataReader["LevelStampIsActive"].ToString()),
         StampId = Int32.Parse(dataReader["StampId"].ToString()),
         LevelStampSortOrder = Int32.Parse(dataReader["LevelStampSortOrder"].ToString()),
         stamp = dataReader.ToStampObject() //call to stamp object
      };

      returnList.Add(levelStamps);
   }
   return returnList;
}


The levelstamp class is pulled by the main store procedure while the stamp is called from the LevelStamp stored proc, so in essensce, the main store proc fills up the base class, then its secondary stored proc execution are called to fill up the levelstamp which lives in the base class, and that store proc calls another stored proc to fill up the stamp class

additional information copied from comment below
when this method is called, it is outside of the Index range
C#
public static Stamp ToStampObject(this IDataReader dataReader)
{
   Stamp stamp = null;
   while (dataReader.Read())
   {
      stamp = new Stamp()
      {
         //assign content
      };
   }
   return stamp;
}
Posted
Updated 25-Mar-14 13:39pm
v5
Comments
ZurdoDev 25-Mar-14 18:56pm    
Where are you stuck?
bobb024 25-Mar-14 18:57pm    
when this method is called, it is outside of the Index range
public static Stamp ToStampObject(this IDataReader dataReader)
{
Stamp stamp = null;
while (dataReader.Read())
{
stamp = new Stamp()
{
//assign content
};
}
return stamp;
}
Tomas Takac 26-Mar-14 17:49pm    
If Stamp and LevelStamp are populated from different result sets then you first need to create LevelStamp objects then call reader.NextResult() and only then create Stamp objects and assign them to respective LevelStamps. If the relation is 1-1 or N-1 (LevelStamp-Stamp) you can join the two on database level and only process one result set. Or get a proper ORM.
bobb024 26-Mar-14 18:01pm    
Thank you Tomas, it is a 1-1 relationship, and when I am doing the join the data populates back but I am unable to assign it because of that error, and that seems to be the issue. Any help or concept I need to look into would be great. I am processing with this type of SQL

PROCEDURE [dbo].GetTableByTableIdOnly
-- Add the parameters for the stored procedure here
@Id int

AS
BEGIN
SET NOCOUNT ON;
DECLARE @Type int;
DECLARE @Id int;
-- Insert statements for procedure here
SELECT TOP 1 @Type = y.type, @InfoId = r.InfoId
from Table y join Table r on
r.InfoId = y.InfoId
where r.Id = @Id


IF @Type = 4
BEGIN
SELECT * FROM Table (nolock) WHERE Id= @Id;
Execute StoredProc @Id;

END

where the Execute StoreProc is a secondary call and a join.

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