Question Find Data

tqmd1

Well-known member
Joined
Dec 5, 2009
Messages
60
Programming Experience
Beginner
Dear Sir,

Table Employees has data as follows

sno----name
1------Eric
2------Boris
3------Bill

Whien I enter "B" in text5 and run following codes then it show error message as
VB.NET:
IndexOutOfRangeException was unhandeled
There is no row at position 0

I have two question
1) Why it does not select name begins with "B"
2) If it has no row then why it does not exit sub.

Please Help


VB.NET:
 str = "select sno,name from employees "
        str &= "where Name Like '" & (TextBox5.Text) & "%'"

        cmd2 = New SqlClient.SqlCommand(str, con)
        da2 = New SqlClient.SqlDataAdapter(cmd2)
        dt3 = New DataTable
        da2.Fill(dt3)

        Dim REC As Integer

        If dt3.Rows.Count = 0 Then
            Exit Sub
        Else
            For REC = 0 To dt3.Rows.Count - 1
                REC = DataGridView1.Rows.Add()
                DataGridView1.Rows(REC).Cells(0).Value = (IIf(IsDBNull(dt3.Rows(REC).Item("sno")), 0, dt3.Rows(REC).Item("sno")))
                DataGridView1.Rows(REC).Cells(1).Value = (IIf(IsDBNull(dt3.Rows(REC).Item("Name")), "", dt3.Rows(REC).Item("Name")))
            Next
        End If
 
Are you sure that dt3 has rows? Are you sure that the DataGridView has rows?
If your DataGridView has no rows in it and dt3 does then I would expect you to get that error as you cannot assign values to nonexistent rows.
 
Dear Sir,

d3 has 2 records, displyaing in debug window

to add new rows in datagrid, I use following codes
VB.NET:
  For REC = 0 To dt3.Rows.Count - 1
                    DataGridView1.Rows.Add()

Then what is going wrong?
 
You can't use REC as your loop counter and the index tracker for the DataGridView added row.

Change
VB.NET:
If dt3.Rows.Count = 0 Then
            Exit Sub
        Else
            For REC = 0 To dt3.Rows.Count - 1
                REC = DataGridView1.Rows.Add()
                DataGridView1.Rows(REC).Cells(0).Value = (IIf(IsDBNull(dt3.Rows(REC).Item("sno")), 0, dt3.Rows(REC).Item("sno")))
                DataGridView1.Rows(REC).Cells(1).Value = (IIf(IsDBNull(dt3.Rows(REC).Item("Name")), "", dt3.Rows(REC).Item("Name")))
            Next
        End If

to this
VB.NET:
If dt3.Rows.Count = 0 Then
            Exit Sub
        Else
            Dim intIndex As Integer
            For REC = 0 To dt3.Rows.Count - 1

                intIndex = DataGridView1.Rows.Add()
                DataGridView1.Rows(REC).Cells(0).Value = (IIf(IsDBNull(dt3.Rows(REC).Item("sno")), 0, dt3.Rows(REC).Item("sno")))
                DataGridView1.Rows(REC).Cells(1).Value = (IIf(IsDBNull(dt3.Rows(REC).Item("Name")), "", dt3.Rows(REC).Item("Name")))
            Next
        End If
 
Sorry about that tqmd1

it should be changed to

VB.NET:
If dt3.Rows.Count = 0 Then
            Exit Sub
        Else
            Dim intIndex As Integer
            For REC = 0 To dt3.Rows.Count - 1

                intIndex = DataGridView1.Rows.Add()
                DataGridView1.Rows(intIndex ).Cells(0).Value = (IIf(IsDBNull(dt3.Rows(REC).Item("sno")), 0, dt3.Rows(REC).Item("sno")))
                DataGridView1.Rows(intIndex ).Cells(1).Value = (IIf(IsDBNull(dt3.Rows(REC).Item("Name")), "", dt3.Rows(REC).Item("Name")))
            Next
        End If

The intIndex is the index for the datagridview and REC is the index of which datarow in the datatable you are using. I missed that one. :eek:
 
No difference foud in Previous Error Message.
THE SITUATION IS STILL SAME
May this is chellenging problem in vb.net?
If you want the I can send you the FORM
 
I have tried the exact code that you have provided after modifying it and it works perfectly. I created a datatable with 2 columns and added a few rows of data and the code below added all the rows to the datagridview with no errors.

VB.NET:
Dim intIndex As Integer

        For REC As Integer = 0 To dt.Rows.Count - 1
            intIndex = DataGridView1.Rows.Add()
            DataGridView1.Rows(intIndex).Cells(0).Value = (IIf(IsDBNull(dt3.Rows(REC).Item("Col1")), 0, dt3.Rows(REC).Item("Col1")))
            DataGridView1.Rows(intIndex).Cells(1).Value = (IIf(IsDBNull(dt3.Rows(REC).Item("Col2")), 0, dt3.Rows(REC).Item("Col2")))

        Next

I think your exception must be happening someplace else in the code. Have you stepped through in debug mode to make sure that it is or is not happening in this particular section of code?
Can you post the full error message?
 
This is full error message screen shot
 

Attachments

  • Find Data.JPG
    Find Data.JPG
    104.8 KB · Views: 21
Back
Top