Using colum name to get data from a datareader in C#

Discussion in 'ASP .Net' started by Bjorn Sagbakken, Sep 3, 2007.

  1. Well, that about the whole question.
    I'm trying to convert from VB to C#, and so far I have figured out most of
    the issues.
    But I haven't found a way to retrieve the column data from a datareader
    using the coumn name, only it's zero-based column ordinal (like
    myDataReader.GetString(8) ... ) and when I use several sql joins, I don't
    know the exact order of the colums in the result.

    Any tip?

    Bjorn
     
    Bjorn Sagbakken, Sep 3, 2007
    #1
    1. Advertising

  2. > But I haven't found a way to retrieve the column data from a datareader
    > using the coumn name, only it's zero-based column ordinal (like
    > myDataReader.GetString(8) ... ) and when I use several sql joins, I don't
    > know the exact order of the colums in the result.


    myDataReader.GetString(myDataReader.GetOrdinal("ColumnName"))
     
    Muhammad Naveed Yaseen, Sep 3, 2007
    #2
    1. Advertising

  3. "Bjorn Sagbakken" <> wrote in message
    news:p...

    > Well, that about the whole question.
    > I'm trying to convert from VB to C#, and so far I have figured out most of
    > the issues.
    > But I haven't found a way to retrieve the column data from a datareader
    > using the coumn name, only it's zero-based column ordinal (like
    > myDataReader.GetString(8) ... ) and when I use several sql joins, I don't
    > know the exact order of the colums in the result.
    >
    > Any tip?


    myDataReader.GetString(myDataReader.GetOrdinal["myColumn"]);


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
     
    Mark Rae [MVP], Sep 3, 2007
    #3
  4. "Muhammad Naveed Yaseen" <> wrote in message
    news:...
    >> But I haven't found a way to retrieve the column data from a datareader
    >> using the coumn name, only it's zero-based column ordinal (like
    >> myDataReader.GetString(8) ... ) and when I use several sql joins, I don't
    >> know the exact order of the colums in the result.

    >
    > myDataReader.GetString(myDataReader.GetOrdinal("ColumnName"))


    Thanks. I actually found out myself a few minutes ago.

    Bjorn
     
    Bjorn Sagbakken, Sep 3, 2007
    #4
  5. "Mark Rae [MVP]" <> wrote in message
    news:%...
    > "Bjorn Sagbakken" <> wrote in message
    > news:p...
    >
    >> Well, that about the whole question.
    >> I'm trying to convert from VB to C#, and so far I have figured out most
    >> of the issues.
    >> But I haven't found a way to retrieve the column data from a datareader
    >> using the coumn name, only it's zero-based column ordinal (like
    >> myDataReader.GetString(8) ... ) and when I use several sql joins, I don't
    >> know the exact order of the colums in the result.
    >>
    >> Any tip?

    >
    > myDataReader.GetString(myDataReader.GetOrdinal["myColumn"]);


    Thanks. I actually found out myself a few minutes ago.

    Bjorn
     
    Bjorn Sagbakken, Sep 3, 2007
    #5
  6. On Sep 3, 2:37 pm, "Bjorn Sagbakken" <> wrote:
    > "Mark Rae [MVP]" <> wrote in messagenews:%...
    >
    > > "Bjorn Sagbakken" <> wrote in message
    > >news:p...

    >
    > >> Well, that about the whole question.
    > >> I'm trying to convert from VB to C#, and so far I have figured out most
    > >> of the issues.
    > >> But I haven't found a way to retrieve the column data from a datareader
    > >> using the coumn name, only it's zero-based column ordinal (like
    > >> myDataReader.GetString(8) ... ) and when I use several sql joins, I don't
    > >> know the exact order of the colums in the result.

    >
    > >> Any tip?

    >
    > > myDataReader.GetString(myDataReader.GetOrdinal["myColumn"]);

    >
    > Thanks. I actually found out myself a few minutes ago.
    >
    > Bjorn


    Assuming the datareader is populated...

    dataValue = dr["columnName'];

    example:
    while(dr.Read())
    {
    string name = dr["PURCH_ID"];
    string addr = dr["ADDR"];
    }
     
    SE.Computerguy, Sep 3, 2007
    #6
  7. "SE.Computerguy" <> wrote in message
    news:...

    > dataValue = dr["columnName'];
    >
    > example:
    > while(dr.Read())
    > {
    > string name = dr["PURCH_ID"];
    > string addr = dr["ADDR"];
    > }


    That won't compile because dr[...] is not strongly typed i.e. it returns an
    object type. The above code will throw the following exception:
    Cannot implicitly convert type 'object' to 'string'. An explicit conversion
    exists (are you missing a cast?)

    That's why the OP was using the GetString() method. If you don't want to use
    that, you'll have to explicitly cast e.g.

    string addr = Convert.ToString(dr["ADDR"]);

    or

    string addr = dr["ADDR"].ToString();


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
     
    Mark Rae [MVP], Sep 3, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RP
    Replies:
    4
    Views:
    560
  2. xiaozozo

    retrieve value from select colum

    xiaozozo, Aug 11, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    573
    xiaozozo
    Aug 11, 2005
  3. xiaozozo

    retrieve value from select colum

    xiaozozo, Aug 11, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    361
    xiaozozo
    Aug 11, 2005
  4. genc_ymeri
    Replies:
    1
    Views:
    383
  5. John Tennan

    2 Colum Layout

    John Tennan, Jul 23, 2006, in forum: HTML
    Replies:
    1
    Views:
    637
    dorayme
    Jul 23, 2006
Loading...

Share This Page