Need help with passing NextResult SafeDataReader to Child Collection

Need help with passing NextResult SafeDataReader to Child Collection

Old forum URL: forums.lhotka.net/forums/t/4497.aspx


Gorreskime posted on Thursday, March 13, 2008

I am just getting familiar with the newest framework (3.0) and am having problems with passing the SafeDataReader to my child collection.

I have a simple Client (editable root) that contains Addresses (editable child collection) which contains a collection of Address objects (editable child).

My Client objects DataPortal_Fetch uses a stored proc that returns both the client data and client addresses.

My Code looks like this:

Client DataPortal_Fetch method:

            dr.Read();
            _id = dr.GetInt32("ClientID");
            _firstName = dr.GetString("FName");
            _lastName = dr.GetString("LName");

            dr.NextResult();

            _addresses = Addresses.GetAddresses(dr);

 Addresses class GetAddresses method:

internal static Addresses GetAddresess(SafeDataReader dr)
    {
      return new Addresses(dr);
    }

When I am in the Client object the SafeDataReader has data after the NextResult. When I check it again in the Addresses class, it is empty.

I am guessing it is something silly that I am missing but am stumped.

Thanks for any help,

Mike

 

mr_lasseter replied on Thursday, March 13, 2008

Don't know if this is your problem or not but somewhere after the dr.NextResult(); you are going to need a dr.Read();


Mike

JohnB replied on Thursday, March 13, 2008

Mike,

Adding to the previous post, you should have at a minimum, the following code in your Addresses class:

private New(SafeDataReader dr)
{
    MarkAsChild();
    Fetch(dr); 
}


private void Fetch(SafeDataReader dr)
{
    this.RaiseListChangedEvents = false;
    while (dr.Read()) {
        this.Add(Addresses.GetAddress(dr));
    }
    this.RaiseListChangedEvents = true;  
}

John

Gorreskime replied on Friday, March 14, 2008

John,

I have those methods in my addresses class. When it gets to the "while (dr.Read())" it never gets into the add because there are no records. I have backtracked the location where the SafeDataReader is empty and it is at the boundry between my Client and Addresses classes (the code I posted).

Thanks for the suggestion though.

Mike

JohnB replied on Friday, March 14, 2008

Are you certain that your stored proc is returning two results sets? If you run the stored proc in SQL Mgmt Studio you should see two result sets. Let's eliminate the easy stuff first.

John

Gorreskime replied on Friday, March 14, 2008

The SP does return 2 results sets and I an even see the second data set right after I do the dr.NextResult(). By "see" I mean hovering over the object and drilling down to the data. I just loose that data when I check it in the Addresses class.

I've even done:

if (dr.NextResult())

   _addresses = Addresses.GetAddresses(dr);

and it does return true and calls the GetAdresses

Mike

Gorreskime replied on Friday, March 14, 2008

Ok. I found something weird. As I step through the code and check the DataSet as I go it looses the data. If I run it without any breaks, it works.

Maybe a bug with VS2008? That would explain why it was so hard to figure out what I did wrong Stick out tongue [:P]

Thanks for everyones help.

Mike

Copyright (c) Marimer LLC