Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Index was outside the bounds of the array

Index was outside the bounds of the array

Scheduled Pinned Locked Moved C#
questiondatabasedata-structureshelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NewToAspDotNet
    wrote on last edited by
    #1

    Hi All, I have a datatable and i want to get the first and second characters of a column of each row of my datatable:

    foreach (DataRow dr in dt.Rows)
    {
    string str = dr[5].ToString();
    char first = str[0];
    char second = str[1];
    }

    Bud i m getting a error: Index was outside the bounds of the array on the 5e line : char second = str[1]; can anyone tell me why and what is the solution? Thanx

    L L 2 Replies Last reply
    0
    • N NewToAspDotNet

      Hi All, I have a datatable and i want to get the first and second characters of a column of each row of my datatable:

      foreach (DataRow dr in dt.Rows)
      {
      string str = dr[5].ToString();
      char first = str[0];
      char second = str[1];
      }

      Bud i m getting a error: Index was outside the bounds of the array on the 5e line : char second = str[1]; can anyone tell me why and what is the solution? Thanx

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      KamarBand wrote:

      string str = dr[5].ToString();

      You should use the value inside the DataColumn, not the DataColumn itself (let me guess the column name is only 1 character).

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 alpha 4a out now (29 May 2008)

      N 1 Reply Last reply
      0
      • N NewToAspDotNet

        Hi All, I have a datatable and i want to get the first and second characters of a column of each row of my datatable:

        foreach (DataRow dr in dt.Rows)
        {
        string str = dr[5].ToString();
        char first = str[0];
        char second = str[1];
        }

        Bud i m getting a error: Index was outside the bounds of the array on the 5e line : char second = str[1]; can anyone tell me why and what is the solution? Thanx

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        KamarBand wrote:

        Bud i m getting a error: Index was outside the bounds of the array on the 5e line : char second = str[1]; can anyone tell me why and what is the solution?

        Yeah, you don't do any bounds checking on the string. In this case it's exactly one character long. If the string is empty, then it'll also crash in char first = str[0]; regards

        1 Reply Last reply
        0
        • L leppie

          KamarBand wrote:

          string str = dr[5].ToString();

          You should use the value inside the DataColumn, not the DataColumn itself (let me guess the column name is only 1 character).

          xacc.ide - now with TabsToSpaces support
          IronScheme - 1.0 alpha 4a out now (29 May 2008)

          N Offline
          N Offline
          NewToAspDotNet
          wrote on last edited by
          #4

          Hi Leppie, thanx fory reply, i found the problem, one of the rows had a empty string, that's why i got the error.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups