CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2007
    Posts
    405

    error index out of range exception was unhandled by user code

    You can for example enter the length and width of the web (VS.NET2005), when it runs out of range index error exception was unhandled by user code for chieudai

    Code:
    public class Tinh
        {
             private Int32 _quanhuyen;
             private String _diachiNha;
             private Double _chieudai;
             private Double _chieurong;         
             private Double _dientichDat;
        }
       
          public Int32 Quanhuyen
           {
                get { return _quanhuyen; }
                set { _quanhuyen = value; }
            }
     
           public String DiachiNha
            {
                 get { return _diachiNha; }
                 set { _diachiNha = value; }
             }
    
             public Double Chieudai
             {
                 get { return _chieudai; }
                 set { _chieudai = value; }
             }
    
             public Double Chieurong
             {
                 get { return _chieurong; }
                 set { _chieurong = value; }
             }
    
            public Double DientichDat
            {
               get { return _dientichDat; }
               set { _dientichDat = value; }
            }
    
        private Tinh DienTich(IDataReader dr, bool GetTotalRecords)
        {
           Tinh obj = new Tinh();
                      obj.Quanhuyen = (dr["Quanhuyen"] is DBNull) ? Int32.MinValue : (Int32)dr["Quanhuyen"];
                      obj.DiachiNha = (dr["DiachiNha"] is DBNull) ? String.Empty : (String)dr["DiachiNha"];
                      // bottom  line this error index out of range exception was unhandled by user code
                      obj.Chieudai = (dr["Chieudai"] is DBNull) ? Double.MinValue : (Double)dr["Chieudai"];   
                      obj.Chieurong = (dr["Chieurong"] is DBNull) ? Double.MinValue : (Double)dr["Chieurong"];
                 obj.DientichDat = (dr["DientichDat"] is DBNull) ? Double.MinValue :  (Double)dr["DientichDat"];
                      if (GetTotalRecords)
                         obj.TotalRecords = (dr["TotalRecords"] is DBNull) ? -1 : Globals.ConvertToInt32(dr["TotalRecords"]);
    
            return obj;
            }
    error index out of range exception was unhandled by user code
    Last edited by dongtrien; July 11th, 2012 at 02:06 AM. Reason: added code tags..

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: error index out of range exception was unhandled by user code

    Dongtrien .. you've been around long enough to know how to use code tags... Please use them....

    to your problem ... are you sure that the IDataReader 'dr', is properly set and has been moved to the first record.. IE. dr.Read...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Sep 2007
    Posts
    405

    Re: error index out of range exception was unhandled by user code

    I have set up IDataReader 'dr' right program because the program runs normally, I added two field Chieurong and field Chieudai in table data SQL 2005, the faulty on and of course the function or procedure corresponding to 2 field, if I delete the two field Chieurong and Chieudai database in SQL 2005, the program runs normally again, I would like to add two field to receive an error message but I do not know how handle?! if you can only help

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: error index out of range exception was unhandled by user code

    Okay - now that you've changed the Location of where the error occurs.. I can say that your SQL Query does not return these two columns...

    You using "Select Quanhuyen, DiachiNha, DientichDat ..... from .... ". Make sure to add the two new columns to the SQL Query (Or stored proc) else change it to "Select * from..... "..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  5. #5
    Join Date
    Sep 2007
    Posts
    405

    Re: error index out of range exception was unhandled by user code

    Quote Originally Posted by GremlinSA View Post
    Okay - now that you've changed the Location of where the error occurs.. I can say that your SQL Query does not return these two columns...

    You using "Select Quanhuyen, DiachiNha, DientichDat ..... from .... ". Make sure to add the two new columns to the SQL Query (Or stored proc) else change it to "Select * from..... "..
    OK, thank you very much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured