Reference datatable row without for loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Kelley

    Reference datatable row without for loop

    If I have a datatable with 1 record only, can I reference the data in the
    row without a for loop? I can get to the data by using the following code,
    but it would be nice if I didn't have to use the For loop.

    foreach (DataRow row in dataTable.Rows)
    {

    string name = row["Name"].ToString();

    string id = row["ID"].ToString();

    }




  • Marc Gravell

    #2
    Re: Reference datatable row without for loop

    DataRow row = dataTable.Rows[0];
    string name = row["Name"].ToString(), id = row["ID"].ToString();

    Marc


    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: Reference datatable row without for loop

      Hi,

      You could do dataTable.Rows[0]["ID"].ToString();

      Of course, a table that you know for sure will always have only one record
      is not a table but a record, you better use another structure to hold this
      info.


      --
      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation



      "Tim Kelley" <tkelley@compan y.comwrote in message
      news:%23VyMGpsw GHA.380@TK2MSFT NGP05.phx.gbl.. .
      If I have a datatable with 1 record only, can I reference the data in the
      row without a for loop? I can get to the data by using the following
      code, but it would be nice if I didn't have to use the For loop.
      >
      foreach (DataRow row in dataTable.Rows)
      {
      >
      string name = row["Name"].ToString();
      >
      string id = row["ID"].ToString();
      >
      }
      >
      >
      >
      >

      Comment

      • Peter Bromberg [C# MVP]

        #4
        Re: Reference datatable row without for loop


        "Of course, a table that you know for sure will always have only one record
        is not a table but a record, you better use another structure to hold this
        info. "

        ?-- What' wrong with using a DataTable that you know only has one DataRow?
        I do this all the time.
        Peter

        --
        Co-founder, Eggheadcafe.com developer portal:

        UnBlog:





        "Ignacio Machin ( .NET/ C# MVP )" wrote:
        Hi,
        >
        You could do dataTable.Rows[0]["ID"].ToString();
        >
        Of course, a table that you know for sure will always have only one record
        is not a table but a record, you better use another structure to hold this
        info.
        >
        >
        --
        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation
        >
        >
        >
        "Tim Kelley" <tkelley@compan y.comwrote in message
        news:%23VyMGpsw GHA.380@TK2MSFT NGP05.phx.gbl.. .
        If I have a datatable with 1 record only, can I reference the data in the
        row without a for loop? I can get to the data by using the following
        code, but it would be nice if I didn't have to use the For loop.

        foreach (DataRow row in dataTable.Rows)
        {

        string name = row["Name"].ToString();

        string id = row["ID"].ToString();

        }


        >
        >
        >

        Comment

        • Marc Gravell

          #5
          Re: Reference datatable row without for loop

          No; it's still a table with one record ;-p

          I seem to be one of the minority who just don't tend to use DataTable etc;
          but if I did, I would have no issue having a 1-row table, if only to keep
          related parts of the system simple and consistent.

          Marc


          Comment

          • Ignacio Machin \( .NET/ C# MVP \)

            #6
            Re: Reference datatable row without for loop

            Hi,


            "Peter Bromberg [C# MVP]" <pbromberg@yaho o.nospammin.com wrote in message
            news:834D35B5-B95F-4122-989D-BCB831D2C069@mi crosoft.com...
            >
            "Of course, a table that you know for sure will always have only one
            record
            is not a table but a record, you better use another structure to hold this
            info. "
            >
            ?-- What' wrong with using a DataTable that you know only has one DataRow?
            I do this all the time.
            Peter
            IMO this is usually used when you are keeping config info or when you have
            an unique instance of some piece of data.

            In both cases is more clear to define a class with properties to access the
            info instead of using a DataTable.

            Now the OP did not state that this is the case so my suggestion may not
            apply in this particular case.




            --
            --
            Ignacio Machin,
            ignacio.machin AT dot.state.fl.us
            Florida Department Of Transportation


            Comment

            • Peter Bromberg [C# MVP]

              #7
              Re: Reference datatable row without for loop

              Actually , I often get a row of information from a database that may be
              user-specific, and I take the one DataRow from the resulting DataTable and
              stick it in Session. Working with a DataRow is quite intuitive and it's a
              pretty lightweight object IMHO. So you could do

              string firstName=(stri ng)((DataRow)Se ssion["myRow"])["FirstName"];

              -- for example.
              I don't particularly feel compelled to create a special object for this
              stuff when I can get at it easily as above, especially if i don't need to
              access it often.

              Peter


              --
              Co-founder, Eggheadcafe.com developer portal:

              UnBlog:





              "Ignacio Machin ( .NET/ C# MVP )" wrote:
              Hi,
              >
              >
              "Peter Bromberg [C# MVP]" <pbromberg@yaho o.nospammin.com wrote in message
              news:834D35B5-B95F-4122-989D-BCB831D2C069@mi crosoft.com...

              "Of course, a table that you know for sure will always have only one
              record
              is not a table but a record, you better use another structure to hold this
              info. "

              ?-- What' wrong with using a DataTable that you know only has one DataRow?
              I do this all the time.
              Peter
              >
              IMO this is usually used when you are keeping config info or when you have
              an unique instance of some piece of data.
              >
              In both cases is more clear to define a class with properties to access the
              info instead of using a DataTable.
              >
              Now the OP did not state that this is the case so my suggestion may not
              apply in this particular case.
              >
              >
              >
              >
              --
              --
              Ignacio Machin,
              ignacio.machin AT dot.state.fl.us
              Florida Department Of Transportation
              >
              >
              >

              Comment

              Working...