Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

in my IDataReader I am reading results from my stored procedure with help of the enterprise library. The problem is I have two columns on different tables that hold different values but are named the same thing, so when I have AddressID coming back with the same value when person.AddressID and Address.AddressID are different as the join is on personID.

C#
public static List<person> ToPersonListFull(this IDataReader dataReader)
    {
        var returnList = new List<person>();
        while (dataReader.Read())
        {
            var Person = new Person()
            {
                PersonID = Int32.Parse(dataReader["PersonID "].ToString()),
                Name = dataReader["Name"].ToString(),
                AddressID = Int32.Parse(dataReader["AddressID"].ToString()),
                Address = new Address ()
                {
                    AddressID = Int32.Parse(dataReader["AddressID "].ToString()),
                    PersonID = Int32.Parse(dataReader["PersonID "].ToString()),
                    Address1 = dataReader["Address1"].ToString(),
                    ZipCode = Int32.Parse(dataReader["ZipCode"].ToString()),
                    
                },
                

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


Any help would be great! Thank you
Posted
Updated 12-Dec-14 6:15am
v3
Comments
Kornfeld Eliyahu Peter 11-Dec-14 16:14pm    
You should give an example data of the error - it is not too clear now...
I think that adding you SP may help also...
Tomas Takac 11-Dec-14 16:30pm    
You should show us the SP as well. What is stopping you from renaming the column in that SP?
Jörgen Andersson 12-Dec-14 2:37am    
Int32.Parse together with .ToString doesn't make sense.
bobb024 12-Dec-14 12:15pm    
you are right, i fixed above

public static List<person> ToPersonListFull(this IDataReader dataReader)
{
var returnList = new List<person>();
while (dataReader.Read())
{
var Person = new Person()
{
PersonID = Int32.Parse(dataReader["PersonID "].ToString()),
Name = dataReader["Name"].ToString(),
AddressID = Int32.Parse(dataReader["AddressID"].ToString()),
Address = new Address ()
{
AddressID = Int32.Parse(dataReader["tAddressID "].ToString()),
PersonID = Int32.Parse(dataReader["tPersonID "].ToString()),
Address1 = dataReader["Address1"].ToString(),
ZipCode = Int32.Parse(dataReader["ZipCode"].ToString()),

},


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

I was able to set t.AddressID as tAddressId and t.PersonID as tPersonId and easily able to find and link it in the code, so i can access AddressID and tAddressID, PersonID and t.PersonID and get the right values.
 
Share this answer
 
hey everyone, i figured it out, i just did some renaming in the stored proc, ie
select t.addressid as tAddressId and all is well
 
Share this answer
 
Comments
TheRealSteveJudge 12-Dec-14 3:23am    
Thou shalt not abuse solution for comments.
bobb024 12-Dec-14 12:15pm    
you are correct, posted correctly now

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