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. Web Development
  3. ASP.NET
  4. Index was out of range [modified]

Index was out of range [modified]

Scheduled Pinned Locked Moved ASP.NET
helpdatabasecssmysqlsysadmin
12 Posts 5 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.
  • A ASPnoob

    Hi, I'm trying to read a blob from my database table but ran into a little problem that I can't seem to solve. The following code is used in the page load and the button_click section of the code behind. I got the exception

    "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

    when I tried to view it in the browser. Dim i As Integer For i = 0 To ListBox2.Items.Count - 1 If ListBox2.Items(i).Selected Then Dim myCon As New OdbcConnection myCon = New OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=myServer;Database=myDB;User=myUserassword=myPass;Option=3;") Dim cmd As New OdbcCommand("SELECT * FROM myTable where Professions = '"& ListBox2.Items(i).Text &"' ") myCon.Open() cmd.Connection = myCon Dim ds As New DataSet Dim ad As New OdbcDataAdapter(cmd) ad.Fill(ds) Me.ListBox2.DataSource = ds Me.ListBox2.DataTextField = "Professions" Me.ListBox2.DataBind() End If Next I searched online for possible causes of this error but, none of the causes that I found has anything to do with my situation. Please take a look to see what I might of missed, thank you in advance for your help. -- modified at 3:46 Wednesday 11th July, 2007

    V Offline
    V Offline
    valerian precop
    wrote on last edited by
    #3

    I'm not sure exactly but you might take a look at "SELECT * FROM myTable where Professions = 'ListBox2.Items(i).Text' " shouldn't these be something like "SELECT * FROM myTable where Professions = '" + ListBox2.Items(i).Text + "' " (I used string concatenation from c# I'm not so good with VB :) ) Tell me if it worked... :)

    Just call me Valy... :)

    A 1 Reply Last reply
    0
    • V valerian precop

      I'm not sure exactly but you might take a look at "SELECT * FROM myTable where Professions = 'ListBox2.Items(i).Text' " shouldn't these be something like "SELECT * FROM myTable where Professions = '" + ListBox2.Items(i).Text + "' " (I used string concatenation from c# I'm not so good with VB :) ) Tell me if it worked... :)

      Just call me Valy... :)

      A Offline
      A Offline
      ASPnoob
      wrote on last edited by
      #4

      That mistake was already spotted when I last post a message a day ago but I forgot to change it when I paste the code in the message box. Unfortunately thats not the problem but thanks for your help. This call is made in the page load and in the button1_click sections of the code behind. The listbox is in multi-select mode.

      1 Reply Last reply
      0
      • A ASPnoob

        Hi, I'm trying to read a blob from my database table but ran into a little problem that I can't seem to solve. The following code is used in the page load and the button_click section of the code behind. I got the exception

        "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

        when I tried to view it in the browser. Dim i As Integer For i = 0 To ListBox2.Items.Count - 1 If ListBox2.Items(i).Selected Then Dim myCon As New OdbcConnection myCon = New OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=myServer;Database=myDB;User=myUserassword=myPass;Option=3;") Dim cmd As New OdbcCommand("SELECT * FROM myTable where Professions = '"& ListBox2.Items(i).Text &"' ") myCon.Open() cmd.Connection = myCon Dim ds As New DataSet Dim ad As New OdbcDataAdapter(cmd) ad.Fill(ds) Me.ListBox2.DataSource = ds Me.ListBox2.DataTextField = "Professions" Me.ListBox2.DataBind() End If Next I searched online for possible causes of this error but, none of the causes that I found has anything to do with my situation. Please take a look to see what I might of missed, thank you in advance for your help. -- modified at 3:46 Wednesday 11th July, 2007

        I Offline
        I Offline
        Imran Khan Pathan
        wrote on last edited by
        #5

        Hi What u want to do here? Your code is totally wrong. Best Regard Pathan

        GOD HELP THOSE WHO HELP THEMSELVES

        A 1 Reply Last reply
        0
        • A ASPnoob

          Hi, I'm trying to read a blob from my database table but ran into a little problem that I can't seem to solve. The following code is used in the page load and the button_click section of the code behind. I got the exception

          "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

          when I tried to view it in the browser. Dim i As Integer For i = 0 To ListBox2.Items.Count - 1 If ListBox2.Items(i).Selected Then Dim myCon As New OdbcConnection myCon = New OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=myServer;Database=myDB;User=myUserassword=myPass;Option=3;") Dim cmd As New OdbcCommand("SELECT * FROM myTable where Professions = '"& ListBox2.Items(i).Text &"' ") myCon.Open() cmd.Connection = myCon Dim ds As New DataSet Dim ad As New OdbcDataAdapter(cmd) ad.Fill(ds) Me.ListBox2.DataSource = ds Me.ListBox2.DataTextField = "Professions" Me.ListBox2.DataBind() End If Next I searched online for possible causes of this error but, none of the causes that I found has anything to do with my situation. Please take a look to see what I might of missed, thank you in advance for your help. -- modified at 3:46 Wednesday 11th July, 2007

          V Offline
          V Offline
          valerian precop
          wrote on last edited by
          #6

          Imran Khan Pathan is right... I didn't see it the first time but you are doing something like these You have a "for" that walks through the listBox2 elements and sees which is selected...then you make a query in the database (inside that "for") and you bind the result of the query with the same listBox2...these alters the containt of the listBox2 and the for is not working anymore (ListBox2.Items.Count is not the same after you have done the binding in the "for" statement and also the ListBox2.Items are changed)

          Just call me Valy... :)

          1 Reply Last reply
          0
          • A ASPnoob

            Hi, I'm trying to read a blob from my database table but ran into a little problem that I can't seem to solve. The following code is used in the page load and the button_click section of the code behind. I got the exception

            "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

            when I tried to view it in the browser. Dim i As Integer For i = 0 To ListBox2.Items.Count - 1 If ListBox2.Items(i).Selected Then Dim myCon As New OdbcConnection myCon = New OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=myServer;Database=myDB;User=myUserassword=myPass;Option=3;") Dim cmd As New OdbcCommand("SELECT * FROM myTable where Professions = '"& ListBox2.Items(i).Text &"' ") myCon.Open() cmd.Connection = myCon Dim ds As New DataSet Dim ad As New OdbcDataAdapter(cmd) ad.Fill(ds) Me.ListBox2.DataSource = ds Me.ListBox2.DataTextField = "Professions" Me.ListBox2.DataBind() End If Next I searched online for possible causes of this error but, none of the causes that I found has anything to do with my situation. Please take a look to see what I might of missed, thank you in advance for your help. -- modified at 3:46 Wednesday 11th July, 2007

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #7

            You are changing the items of the list box while you are looping through the items.

            --- single minded; short sighted; long gone;

            1 Reply Last reply
            0
            • I Imran Khan Pathan

              Hi What u want to do here? Your code is totally wrong. Best Regard Pathan

              GOD HELP THOSE WHO HELP THEMSELVES

              A Offline
              A Offline
              ASPnoob
              wrote on last edited by
              #8

              I am trying to read blobs from a field in a database table. The blobs hold resumes and each resume can have one or many corresponding professions in the Professions field. I am trying to enable users to look up resumes forcertain professions by choosing one or many professions in the listbox. The Select statement selects my table where the text of the listitem chosen by the users match the profession in the Professions field.

              I 1 Reply Last reply
              0
              • A ASPnoob

                I am trying to read blobs from a field in a database table. The blobs hold resumes and each resume can have one or many corresponding professions in the Professions field. I am trying to enable users to look up resumes forcertain professions by choosing one or many professions in the listbox. The Select statement selects my table where the text of the listitem chosen by the users match the profession in the Professions field.

                I Offline
                I Offline
                Imran Khan Pathan
                wrote on last edited by
                #9

                on which event u call this code? Best Regard Pathan

                GOD HELP THOSE WHO HELP THEMSELVES

                1 Reply Last reply
                0
                • A ASPnoob

                  Hi, I'm trying to read a blob from my database table but ran into a little problem that I can't seem to solve. The following code is used in the page load and the button_click section of the code behind. I got the exception

                  "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

                  when I tried to view it in the browser. Dim i As Integer For i = 0 To ListBox2.Items.Count - 1 If ListBox2.Items(i).Selected Then Dim myCon As New OdbcConnection myCon = New OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=myServer;Database=myDB;User=myUserassword=myPass;Option=3;") Dim cmd As New OdbcCommand("SELECT * FROM myTable where Professions = '"& ListBox2.Items(i).Text &"' ") myCon.Open() cmd.Connection = myCon Dim ds As New DataSet Dim ad As New OdbcDataAdapter(cmd) ad.Fill(ds) Me.ListBox2.DataSource = ds Me.ListBox2.DataTextField = "Professions" Me.ListBox2.DataBind() End If Next I searched online for possible causes of this error but, none of the causes that I found has anything to do with my situation. Please take a look to see what I might of missed, thank you in advance for your help. -- modified at 3:46 Wednesday 11th July, 2007

                  A Offline
                  A Offline
                  ASPnoob
                  wrote on last edited by
                  #10

                  I've used the for loop to determine the list item selected by the user. Then I used the select statement to select the profession that is equal to the text of the list item that was selected. If I change the contents of listbox2 during each iteration of the loop when I bind the data to listbox2, then does that mean I can't use the for loop like I did? Should I bind the data out side of the for loop? I'm going to try and change the sequence of my code, but please give me some suggestions on how to remedy this code.

                  R I 2 Replies Last reply
                  0
                  • A ASPnoob

                    I've used the for loop to determine the list item selected by the user. Then I used the select statement to select the profession that is equal to the text of the list item that was selected. If I change the contents of listbox2 during each iteration of the loop when I bind the data to listbox2, then does that mean I can't use the for loop like I did? Should I bind the data out side of the for loop? I'm going to try and change the sequence of my code, but please give me some suggestions on how to remedy this code.

                    R Offline
                    R Offline
                    Rhys Gravell
                    wrote on last edited by
                    #11

                    Try here...[^]

                    Rhys "The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it" They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance." Terry Pratchett

                    1 Reply Last reply
                    0
                    • A ASPnoob

                      I've used the for loop to determine the list item selected by the user. Then I used the select statement to select the profession that is equal to the text of the list item that was selected. If I change the contents of listbox2 during each iteration of the loop when I bind the data to listbox2, then does that mean I can't use the for loop like I did? Should I bind the data out side of the for loop? I'm going to try and change the sequence of my code, but please give me some suggestions on how to remedy this code.

                      I Offline
                      I Offline
                      Imran Khan Pathan
                      wrote on last edited by
                      #12

                      Try this code But dont call this function on Page Load Event if ur ListBox2'items are empty. Dim i As Integer Dim myCon As New OdbcConnection myCon = New OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=myServer;Database=myDB;User=myUserassword=myPass;Option=3;") For i = 0 To ListBox2.Items.Count - 1 If ListBox2.Items(i).Selected Then Dim cmd As New OdbcCommand("SELECT * FROM myTable where Professions = '"& ListBox2.Items(i).Text &"' ") End If Next myCon.Open() cmd.Connection = myCon Dim ds As New DataSet Dim ad As New OdbcDataAdapter(cmd) ad.Fill(ds) Me.ListBox2.DataSource = ds Me.ListBox2.DataTextField = "Professions" Me.ListBox2.DataBind() Best Regard Pathan

                      GOD HELP THOSE WHO HELP THEMSELVES

                      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