Data row array 'Index was outside the bounds of the array'
-
Friday, June 01, 2007 1:08 PM
I am trying to run the following code (VB.Net 2003) this is a simplified example of something I am trying to do in a real app:
Dataset11 is a dataset based on the Employee table of the Northwind database (SQL2000) and returns 'firstname', 'lastname' and 'employeeid' and is fill on form load via sqlDataAdapter:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim emptable As DataTable Dim myrow As DataRow()emptable = DataSet11.Tables("Employees")
myrow = emptable.Select("EmployeeID=" & TextBox3.Text)
TextBox1.Text = myrow(0).Item("Lastname")
TextBox2.Text = myrow(0).Item("FirstName")
End SubGet the error 'Index was outside the bounds of the array' when the code gets to the line 'Textbox1.Text = ...'
Have also tried
Dim myrow as datarow = emptable.Select("EmployeeID=" & TextBox3.Text)(0)
Textbox1.Text = myrow("Lastname")
with same result (except the error occurs on the line 'Dim myrow...'
Any guidance greatly appreciated.
Thanks
Alan
Answers
-
Friday, June 01, 2007 1:21 PM
Hi,
U are not checking whether the result set is having rows or not.
use myrow.Length to get the result set count. means how many rows in the array.
if(myrow.Length < 1)
Console.WriteLine("No rows found");
Hope this will solve your problem
Thanks and all the best
-
Friday, June 01, 2007 1:24 PM
Only thing I can think of is that the select is not returning anything because the table is empty. Are you sure that Employees has been filled by your data adapter. Your second attempt almost confirms this.
-
Friday, June 01, 2007 2:09 PM
Thanks that did seem to be the problem. In fact I was entering an invalid Id in Textbox3. In my real example I was using an array of objects I had filled from the original database, something was wrong with the definition of the Id property and so Id was never set to anything other than zero hence I got the error! I'd written the above code to test whether the data was the problem, then because I entered 0 didn't get the value. Your answers made me look a little closer.
All Replies
-
Friday, June 01, 2007 1:21 PM
Hi,
U are not checking whether the result set is having rows or not.
use myrow.Length to get the result set count. means how many rows in the array.
if(myrow.Length < 1)
Console.WriteLine("No rows found");
Hope this will solve your problem
Thanks and all the best
-
Friday, June 01, 2007 1:24 PM
Only thing I can think of is that the select is not returning anything because the table is empty. Are you sure that Employees has been filled by your data adapter. Your second attempt almost confirms this.
-
Friday, June 01, 2007 2:09 PM
Thanks that did seem to be the problem. In fact I was entering an invalid Id in Textbox3. In my real example I was using an array of objects I had filled from the original database, something was wrong with the definition of the Id property and so Id was never set to anything other than zero hence I got the error! I'd written the above code to test whether the data was the problem, then because I entered 0 didn't get the value. Your answers made me look a little closer.
-
Thursday, February 28, 2008 3:59 PM
i m also gettin the error "Index was outside the bounds of the array." even though i enter a valid input.
my code is as follows:
OleDbDataReader
dr1 = null; OleDbConnection con1 = new OleDbConnection("---a valid address;");con1.Open();
OleDbCommand com = null; string rno = regno.Text; String st = "select SUBJECT from COMPLAINT_MAST where REG_NO ='" + rno + "'";com =
new OleDbCommand(st, con1);dr1 = com.ExecuteReader(
CommandBehavior.CloseConnection); if(dr1.Read()){
Subject.Text =
"Subject";sub_val.Text = dr1.GetValue(2).ToString();
}
dr1.Close();
con1.Close();
plzzzz help soooon.
Thanxxx

