Results 1 to 4 of 4

Thread: [RESOLVED] IndexOutOfRange Exception

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2021
    Posts
    66

    Resolved [RESOLVED] IndexOutOfRange Exception

    Hi,

    I receive the error message in the below code:

    System.IndexOutOfRangeException: TenantName
    vb:line 709



    Code:
     Try
                Using con As New SqlConnection("Data Source=DESKTOP-O6TSDMJ;Initial Catalog=RCMS;Integrated Security=True")
                    DsUnitdetails.EnforceConstraints = False
                    Me.VwunitdetailsTableAdapter.Fill(Me.DsUnitdetails.vwunitdetails, TxtPropertyIDClick.Text)
    
                    con.Open()
    
                    Using cmd As New SqlCommand("Select tenantfullname from tbltenants order by firstname", con)
                        Using myreader As SqlClient.SqlDataReader = cmd.ExecuteReader
                            TenantName.Items.Clear()
                            While myreader.Read
                                 TenantName.Items.Add(myreader("TenantName"))
                            End While
                        End Using
                    End Using
    
                    con.Close()
    I have similar code as the above for another combobox and at runtime it doesn't give me the error, I am confused...

    The code is as follows:

    Code:
    Try
                Using con As New SqlConnection("Data Source=DESKTOP-O6TSDMJ;Initial Catalog=RCMS;Integrated Security=True")
                    DsUnitdetails.EnforceConstraints = False
                    Me.VwunitdetailsTableAdapter.Fill(Me.DsUnitdetails.vwunitdetails, TxtPropertyIDClick.Text)
    
                    con.Open()
    
                    Using cmd As New SqlCommand("Select flattype from lkupflattype", con)
                        Using myreader As SqlClient.SqlDataReader = cmd.ExecuteReader
                            FlatType.Items.Clear()
                            While myreader.Read
                                FlatType.Items.Add(myreader("FlatType"))
                            End While
                        End Using
                    End Using
    
                    con.Close()
    
                End Using
            Catch ex As Exception
                MessageBox.Show(ex.ToString())
            End Try

    What am I doing wrong?

    Thank you!

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jul 2021
    Posts
    66

    Re: IndexOutOfRange Exception

    Sorted---

    "Select tenantfullname AS TenantName from tbltenants order by firstname", con

    My missing...

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,648

    Re: [RESOLVED] IndexOutOfRange Exception

    That seems a slightly odd solution. Why add an alias just to use on that one line when you could just use the actual column name when adding the items:
    Code:
                    Using cmd As New SqlCommand("Select tenantfullname from tbltenants order by firstname", con)
                        Using myreader As SqlClient.SqlDataReader = cmd.ExecuteReader
                            TenantName.Items.Clear()
                            While myreader.Read
                                 TenantName.Items.Add(myreader("tenantfullname"))

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2021
    Posts
    66

    Re: [RESOLVED] IndexOutOfRange Exception

    Very true! Thanks

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