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. System.IndexOutOfRangeException

System.IndexOutOfRangeException

Scheduled Pinned Locked Moved C#
databasehelpquestion
10 Posts 4 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.
  • J Offline
    J Offline
    joost versteegen
    wrote on last edited by
    #1

    Hi, I have a weird problem, hopefully someone can shine a light on it? The user gets a list of defects on a product from the database. I put them in a List, like so:

    while (rdr.Read())
    {
    DrumDefect defect = new DrumDefect()
    {
    AVI_Defect = new AVIDefect()
    {
    SequenceNumber = (rdr["AVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["AVIProdUDefectSeqNr"].ToString()) : (rdr["MVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["MVIProdUDefectSeqNr"].ToString()) : -1,
    Code = (rdr["AVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["AVIProdUDefectCode"].ToString(),
    Name = (rdr["AVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["AVIProdUDefectName"].ToString(),
    Severity = (rdr["AVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["AVISeverityLevelID"].ToString())
    },
    MVI_Defect = new Defect()
    {
    Code = (rdr["MVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["MVIProdUDefectCode"].ToString(),
    Name = (rdr["MVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["MVIProdUDefectName"].ToString(),
    Severity = (rdr["MVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["MVISeverityLevelID"].ToString())
    },
    FVI_Defect = new Defect()
    };
    list.Add(defect);
    }

    Then I import them in a datagridview like so:

        \_SelectedDrum.Defects = database.GetList();
    
        dataGridViewDefects.DataSource = null;
        dataGridViewDefects.DataSource = \_SelectedDrum.Defects;
        dataGridViewDefects.Refresh();
    

    The user can add a defect, so I add one like this:

     DrumDefect defect = new DrumDefect()
     { 
        AVI\_Defect = new AVIDefect()
        {
          SequenceNumber = -1,
          Code = "# 0",
          Name = "NODEFECT",
          Severity = DefectSeverity.NODEFECT
        },
        MVI\_Defect = new Defect()
        {
          Code = "# 0",
          Name = "NODEFECT",
          Severity = DefectSeverity.NODEFECT
        },
        FVI\_Defect = new Defect()
      };
      \_SelectedDrum.Defects.Add(defect);
    

    it all works fine, except when the database list is empty, the user adds a def

    OriginalGriffO Richard DeemingR L 3 Replies Last reply
    0
    • J joost versteegen

      Hi, I have a weird problem, hopefully someone can shine a light on it? The user gets a list of defects on a product from the database. I put them in a List, like so:

      while (rdr.Read())
      {
      DrumDefect defect = new DrumDefect()
      {
      AVI_Defect = new AVIDefect()
      {
      SequenceNumber = (rdr["AVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["AVIProdUDefectSeqNr"].ToString()) : (rdr["MVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["MVIProdUDefectSeqNr"].ToString()) : -1,
      Code = (rdr["AVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["AVIProdUDefectCode"].ToString(),
      Name = (rdr["AVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["AVIProdUDefectName"].ToString(),
      Severity = (rdr["AVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["AVISeverityLevelID"].ToString())
      },
      MVI_Defect = new Defect()
      {
      Code = (rdr["MVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["MVIProdUDefectCode"].ToString(),
      Name = (rdr["MVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["MVIProdUDefectName"].ToString(),
      Severity = (rdr["MVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["MVISeverityLevelID"].ToString())
      },
      FVI_Defect = new Defect()
      };
      list.Add(defect);
      }

      Then I import them in a datagridview like so:

          \_SelectedDrum.Defects = database.GetList();
      
          dataGridViewDefects.DataSource = null;
          dataGridViewDefects.DataSource = \_SelectedDrum.Defects;
          dataGridViewDefects.Refresh();
      

      The user can add a defect, so I add one like this:

       DrumDefect defect = new DrumDefect()
       { 
          AVI\_Defect = new AVIDefect()
          {
            SequenceNumber = -1,
            Code = "# 0",
            Name = "NODEFECT",
            Severity = DefectSeverity.NODEFECT
          },
          MVI\_Defect = new Defect()
          {
            Code = "# 0",
            Name = "NODEFECT",
            Severity = DefectSeverity.NODEFECT
          },
          FVI\_Defect = new Defect()
        };
        \_SelectedDrum.Defects.Add(defect);
      

      it all works fine, except when the database list is empty, the user adds a def

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Do you have an event handler method set up for the click event?

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      J 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Do you have an event handler method set up for the click event?

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

        J Offline
        J Offline
        joost versteegen
        wrote on last edited by
        #3

        yes i have, but it does not reach the breakpoint.

        private void dataGridViewDefects\_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
        }
        

        in fact i have the following event procedures, but none of them is the source of the problem (breakpoint not hit when the error occures)

          this.dataGridViewDefects.CellMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dataGridViewDefects\_CellMouseClick);
          this.dataGridViewDefects.CellMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dataGridViewDefects\_CellMouseDoubleClick);
          this.dataGridViewDefects.RowEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridViewDefects\_RowEnter);
          this.dataGridViewDefects.Enter += new System.EventHandler(this.dataGridViewDefects\_Enter);
          this.dataGridViewDefects.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dataGridViewDefects\_KeyDown);
          this.dataGridViewDefects.Leave += new System.EventHandler(this.dataGridViewDefects\_Leave);
        
        OriginalGriffO 1 Reply Last reply
        0
        • J joost versteegen

          yes i have, but it does not reach the breakpoint.

          private void dataGridViewDefects\_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
          {
          }
          

          in fact i have the following event procedures, but none of them is the source of the problem (breakpoint not hit when the error occures)

            this.dataGridViewDefects.CellMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dataGridViewDefects\_CellMouseClick);
            this.dataGridViewDefects.CellMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dataGridViewDefects\_CellMouseDoubleClick);
            this.dataGridViewDefects.RowEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridViewDefects\_RowEnter);
            this.dataGridViewDefects.Enter += new System.EventHandler(this.dataGridViewDefects\_Enter);
            this.dataGridViewDefects.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dataGridViewDefects\_KeyDown);
            this.dataGridViewDefects.Leave += new System.EventHandler(this.dataGridViewDefects\_Leave);
          
          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Are any of the methods in the stack trace yours, or are they all framework?

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          J 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Are any of the methods in the stack trace yours, or are they all framework?

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

            J Offline
            J Offline
            joost versteegen
            wrote on last edited by
            #5

            they are all framework, except maybe OnRowEnter, but the breakpoint i set did not get hit.

            private void dataGridViewDefects_RowEnter(object sender, DataGridViewCellEventArgs e)
            {
            try
            {
            if (_SelectedDrum != null && e.RowIndex > -1) _SelectedDefect = _SelectedDrum.Defects[e.RowIndex];
            }
            catch (Exception exc)
            {
            Logger.LogException(exc);
            }
            }

            the application crashes in the line:

            Application.Run(new TwisterForm());

            OriginalGriffO 1 Reply Last reply
            0
            • J joost versteegen

              they are all framework, except maybe OnRowEnter, but the breakpoint i set did not get hit.

              private void dataGridViewDefects_RowEnter(object sender, DataGridViewCellEventArgs e)
              {
              try
              {
              if (_SelectedDrum != null && e.RowIndex > -1) _SelectedDefect = _SelectedDrum.Defects[e.RowIndex];
              }
              catch (Exception exc)
              {
              Logger.LogException(exc);
              }
              }

              the application crashes in the line:

              Application.Run(new TwisterForm());

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Can you try enabling all exceptions? From the menu, "Debug...Exceptions", and tick everything in the "Thrown" column. That way, even if an exception is being handled, the code should break (and the reference sources should help you locate why). Trouble is that if I do a "minimum setup" to mimic your problem, I get no exception:

              public class MyObject
              {
              public string Text { get; set; }
              public int Value { get; set; }
              }
              private void button1_Click(object sender, EventArgs e)
              {
              MyObject mo = new MyObject();
              mo.Text = "hello";
              mo.Value = 666;
              myList.Add(mo);
              myDataGridView.Refresh();
              }
              private List<MyObject> myList = new List<MyObject>();
              private void MyButton_Click(object sender, EventArgs e)
              {
              myDataGridView.DataSource = null;
              myList = new List<MyObject>();
              myDataGridView.DataSource = myList;
              myDataGridView.Refresh();

              So I can't get any additional info to work from.

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              J 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Can you try enabling all exceptions? From the menu, "Debug...Exceptions", and tick everything in the "Thrown" column. That way, even if an exception is being handled, the code should break (and the reference sources should help you locate why). Trouble is that if I do a "minimum setup" to mimic your problem, I get no exception:

                public class MyObject
                {
                public string Text { get; set; }
                public int Value { get; set; }
                }
                private void button1_Click(object sender, EventArgs e)
                {
                MyObject mo = new MyObject();
                mo.Text = "hello";
                mo.Value = 666;
                myList.Add(mo);
                myDataGridView.Refresh();
                }
                private List<MyObject> myList = new List<MyObject>();
                private void MyButton_Click(object sender, EventArgs e)
                {
                myDataGridView.DataSource = null;
                myList = new List<MyObject>();
                myDataGridView.DataSource = myList;
                myDataGridView.Refresh();

                So I can't get any additional info to work from.

                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                J Offline
                J Offline
                joost versteegen
                wrote on last edited by
                #7

                ok, thanks. I will try.

                1 Reply Last reply
                0
                • J joost versteegen

                  Hi, I have a weird problem, hopefully someone can shine a light on it? The user gets a list of defects on a product from the database. I put them in a List, like so:

                  while (rdr.Read())
                  {
                  DrumDefect defect = new DrumDefect()
                  {
                  AVI_Defect = new AVIDefect()
                  {
                  SequenceNumber = (rdr["AVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["AVIProdUDefectSeqNr"].ToString()) : (rdr["MVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["MVIProdUDefectSeqNr"].ToString()) : -1,
                  Code = (rdr["AVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["AVIProdUDefectCode"].ToString(),
                  Name = (rdr["AVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["AVIProdUDefectName"].ToString(),
                  Severity = (rdr["AVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["AVISeverityLevelID"].ToString())
                  },
                  MVI_Defect = new Defect()
                  {
                  Code = (rdr["MVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["MVIProdUDefectCode"].ToString(),
                  Name = (rdr["MVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["MVIProdUDefectName"].ToString(),
                  Severity = (rdr["MVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["MVISeverityLevelID"].ToString())
                  },
                  FVI_Defect = new Defect()
                  };
                  list.Add(defect);
                  }

                  Then I import them in a datagridview like so:

                      \_SelectedDrum.Defects = database.GetList();
                  
                      dataGridViewDefects.DataSource = null;
                      dataGridViewDefects.DataSource = \_SelectedDrum.Defects;
                      dataGridViewDefects.Refresh();
                  

                  The user can add a defect, so I add one like this:

                   DrumDefect defect = new DrumDefect()
                   { 
                      AVI\_Defect = new AVIDefect()
                      {
                        SequenceNumber = -1,
                        Code = "# 0",
                        Name = "NODEFECT",
                        Severity = DefectSeverity.NODEFECT
                      },
                      MVI\_Defect = new Defect()
                      {
                        Code = "# 0",
                        Name = "NODEFECT",
                        Severity = DefectSeverity.NODEFECT
                      },
                      FVI\_Defect = new Defect()
                    };
                    \_SelectedDrum.Defects.Add(defect);
                  

                  it all works fine, except when the database list is empty, the user adds a def

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #8

                  That looks like a bug in the framework. According to this post from 2011[^], you should be able to work around it by creating a new list if the existing list is empty when you add the new item:

                  if (_SelectedDrum.Defects.Count == 0)
                  {
                  _SelectedDrum.Defects = new List<DrumDefect> { defect };

                  dataGridViewDefects.DataSource = null;
                  dataGridViewDefects.DataSource = \_SelectedDrum.Defects;
                  dataGridViewDefects.Refresh();
                  

                  }
                  else
                  {
                  _SelectedDrum.Defects.Add(defect);
                  }

                  Or there's this StackOverflow answer[^] which suggests using a BindingList<T> instead of a List<T>. If you can reproduce the problem with a simple example application, it might be worth reporting the bug on Connect[^] to see if it can be fixed in a future version.


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  J 1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    That looks like a bug in the framework. According to this post from 2011[^], you should be able to work around it by creating a new list if the existing list is empty when you add the new item:

                    if (_SelectedDrum.Defects.Count == 0)
                    {
                    _SelectedDrum.Defects = new List<DrumDefect> { defect };

                    dataGridViewDefects.DataSource = null;
                    dataGridViewDefects.DataSource = \_SelectedDrum.Defects;
                    dataGridViewDefects.Refresh();
                    

                    }
                    else
                    {
                    _SelectedDrum.Defects.Add(defect);
                    }

                    Or there's this StackOverflow answer[^] which suggests using a BindingList<T> instead of a List<T>. If you can reproduce the problem with a simple example application, it might be worth reporting the bug on Connect[^] to see if it can be fixed in a future version.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    J Offline
                    J Offline
                    joost versteegen
                    wrote on last edited by
                    #9

                    very interesting, thanks a lot!

                    1 Reply Last reply
                    0
                    • J joost versteegen

                      Hi, I have a weird problem, hopefully someone can shine a light on it? The user gets a list of defects on a product from the database. I put them in a List, like so:

                      while (rdr.Read())
                      {
                      DrumDefect defect = new DrumDefect()
                      {
                      AVI_Defect = new AVIDefect()
                      {
                      SequenceNumber = (rdr["AVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["AVIProdUDefectSeqNr"].ToString()) : (rdr["MVIProdUDefectSeqNr"] != DBNull.Value) ? int.Parse(rdr["MVIProdUDefectSeqNr"].ToString()) : -1,
                      Code = (rdr["AVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["AVIProdUDefectCode"].ToString(),
                      Name = (rdr["AVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["AVIProdUDefectName"].ToString(),
                      Severity = (rdr["AVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["AVISeverityLevelID"].ToString())
                      },
                      MVI_Defect = new Defect()
                      {
                      Code = (rdr["MVIProdUDefectCode"] == DBNull.Value) ? "# 0" : rdr["MVIProdUDefectCode"].ToString(),
                      Name = (rdr["MVIProdUDefectName"] == DBNull.Value) ? "NODEFECT" : rdr["MVIProdUDefectName"].ToString(),
                      Severity = (rdr["MVISeverityLevelID"] == DBNull.Value) ? DefectSeverity.NODEFECT : (DefectSeverity)int.Parse(rdr["MVISeverityLevelID"].ToString())
                      },
                      FVI_Defect = new Defect()
                      };
                      list.Add(defect);
                      }

                      Then I import them in a datagridview like so:

                          \_SelectedDrum.Defects = database.GetList();
                      
                          dataGridViewDefects.DataSource = null;
                          dataGridViewDefects.DataSource = \_SelectedDrum.Defects;
                          dataGridViewDefects.Refresh();
                      

                      The user can add a defect, so I add one like this:

                       DrumDefect defect = new DrumDefect()
                       { 
                          AVI\_Defect = new AVIDefect()
                          {
                            SequenceNumber = -1,
                            Code = "# 0",
                            Name = "NODEFECT",
                            Severity = DefectSeverity.NODEFECT
                          },
                          MVI\_Defect = new Defect()
                          {
                            Code = "# 0",
                            Name = "NODEFECT",
                            Severity = DefectSeverity.NODEFECT
                          },
                          FVI\_Defect = new Defect()
                        };
                        \_SelectedDrum.Defects.Add(defect);
                      

                      it all works fine, except when the database list is empty, the user adds a def

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

                      When modifying the contents of "list controls", you have to be sensitive to where the "selected index" and "selected item" wind up. Often, it needs to be "reset" to an existing item (if there are any) from "-1" (after some operation); which signifies no items OR "nothing selected". (In WPF, for example, the "selected event" will fire during control initializing; which can foul up your app if you don't anticipate it).

                      "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

                      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