Duplicate fieldnames in Datareader

  • Thread starter Thread starter Beren
  • Start date Start date
B

Beren

Hello,

I have an sproc to query a couple of tables, that have the same fieldnames,
for example DateCreated.

-------------------------------------
Select a.* , b.* From tablea a
Join table b On b.keyfield = a.keyfield
-------------------------------------

Now table A and B both have a field called "DateCreated", and I need both
values in my application.

-------------------------------------
.....-- New DataReader dr --....
.....-- query --....
DateCreatedA = dr.Item("a.DateCreated")
DateCreatedB = dr.Item("b.DateCreated")
-------------------------------------

Doesn't seem to work, but

-------------------------------------
SomeVariable = dr.Item("DateCreated")
-------------------------------------

does work, but only for one value...

When I check the resultset in Query Analyzer, it shows duplicate fieldnames,
so that's probably why the DR doesn't swallow my brave attempts with
"a.DateCreated" and "b.Da..."

Can someone help me out getting both values ?

Beren.
 
Do a
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SchemaOnly);

Before running your real SqlQuery - browse thru the fields using a
combination of FieldCount, and GetOrdinal/GetName to fully understand "What
name lies at what ordinal" -- even if they are common.

And then access your data columns via ordinals instead.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 
Thanks for your help,

My apologies for crossposting, but I thought this might be better suitable
in this group.

Thanks,

Beren
 
You also could simply have different alias for both in Sql query to know
which one is which...

Select a.* ,a.DateCreated AS aDateCreated, b.*, b.DateCreated AS
bDateCreated
From tablea a INNER JOIN table b On b.keyfield = a.keyfield

(If you want to avoid having the original fields in the result, you'd need
to tweak this SQL still a bit, however you get the idea)
 
Back
Top