Index was out of range [modified]
-
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 -
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, 2007Where are you making this call in code? Is this Single or Multi select? Have a look at MSDN[^], and the ListBox class, specifically the OnSelectedIndexChanged event and the SelectedIndex, SelectedItem and SelectedValue properties, ListBox members[^].
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
-
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, 2007I'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... :)
-
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... :)
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.
-
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, 2007Hi What u want to do here? Your code is totally wrong. Best Regard Pathan
GOD HELP THOSE WHO HELP THEMSELVES
-
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, 2007Imran 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... :)
-
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 -
Hi What u want to do here? Your code is totally wrong. Best Regard Pathan
GOD HELP THOSE WHO HELP THEMSELVES
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 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.
on which event u call this code? Best Regard Pathan
GOD HELP THOSE WHO HELP THEMSELVES
-
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, 2007I'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'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'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.
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
PathanGOD HELP THOSE WHO HELP THEMSELVES