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

IndexOutOfRangeException was unhandeled
There is no row at position 0I 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

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

Hi,

I tried with your sample code and i am getting an error saying that

No row can be added to a DataGridView control that does not have columns. Columns must be added first.

The above error raises in the following line of code.

REC = DataGridView1.Rows.Add()

which means that the query works fine and it results 2 rows as output to datatable.

On a other hand, if displaying data in a grid is your target means,
why not give a try with following line of code;

DataGridView1.DataSource = dt3

Good luck.

Hi,

I tried with your sample code and i am getting an error saying that

No row can be added to a DataGridView control that does not have columns. Columns must be added first.

The above error raises in the following line of code.

REC = DataGridView1.Rows.Add()

which means that the query works fine and it results 2 rows as output to datatable.

On a other hand, if displaying data in a grid is your target means,
why not give a try with following line of code;

DataGridView1.DataSource = dt3

Good luck.

Dear Sir,

I do not want to use DATASOURCE

Ok. Thank you for the update.

I have added following lines to the below code;

'adding headers
For REC = 0 To dt3.Columns.Count - 1
                    DataGridView1.Columns.Add(dt3.Columns(REC).ColumnName, dt3.Columns(REC).ColumnName)
                Next

Now the code is working fine and datagrid is displaying the 2 records.

Still you are getting the same problem means, you can add null checking to the data table, saying that like if datatable is null then exit.

I have attached the screenshot also.

Please update if you are done with the above or not.

Thank you.

Attachments grid.gif 4.34 KB

Ok. Thank you for the update.

I have added following lines to the below code;

'adding headers
For REC = 0 To dt3.Columns.Count - 1
                    DataGridView1.Columns.Add(dt3.Columns(REC).ColumnName, dt3.Columns(REC).ColumnName)
                Next

Now the code is working fine and datagrid is displaying the 2 records.

Still you are getting the same problem means, you can add null checking to the data table, saying that like if datatable is null then exit.

I have attached the screenshot also.

Please update if you are done with the above or not.

Thank you.

Sir,

I do not want to add columns programatically.
I want to put data in existing defined columns.

I wonder, there are two record in table but why grid does not accept them.

Ok. Thank you for the update.

I have added following lines to the below code;

'adding headers
For REC = 0 To dt3.Columns.Count - 1
                    DataGridView1.Columns.Add(dt3.Columns(REC).ColumnName, dt3.Columns(REC).ColumnName)
                Next

Now the code is working fine and datagrid is displaying the 2 records.

Still you are getting the same problem means, you can add null checking to the data table, saying that like if datatable is null then exit.

I have attached the screenshot also.

Please update if you are done with the above or not.

Thank you.

Sir,

I do not want to add columns programatically.
I want to put data in existing defined columns.

I wonder, there are two record in table but why grid does not accept them.

This article has been dead for over six months. Start a new discussion instead.