8 Replies - 773 Views - Last Post: 30 January 2013 - 02:15 AM Rate Topic: -----

#1 navzgustav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 29-January 13

trying to populate datagrid with data from database

Posted 29 January 2013 - 08:51 AM

hi am trying to populate a datagrid with data from my database using a search function but i get this error
"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

this my code
  conn.Open()
        searchresults = False
        Dim sql, ids As String
        ids = "%" + id + "%"
        sql = "SELECT * FROM products WHERE productid like  '" & ids & "'"
        cmd = New MySqlCommand(sql, conn)
        Dim rd As MySqlDataReader
        rd = cmd.ExecuteReader
        Dim i As Integer

        While rd.Read
            DataGridView1.Rows(i).Cells(1).Value = rd.Item("productname")
            DataGridView1.Rows(i).Cells(0).Value = rd.Item("productid")
            DataGridView1.Rows(i).Cells(2).Value = rd.Item("sellingprice")

            i = i + 1
        End While
        rd.Close()
        conn.Close()
        searchresults = True



Is This A Good Question/Topic? 0
  • +

Replies To: trying to populate datagrid with data from database

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9258
  • View blog
  • Posts: 34,735
  • Joined: 12-June 08

Re: trying to populate datagrid with data from database

Posted 29 January 2013 - 08:54 AM

I would suggest putting a breakpoint at line 11 and stepping into that while loop. You are probably assuming their are more rows or columns than actually returned.
Was This Post Helpful? 0
  • +
  • -

#3 navzgustav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 29-January 13

Re: trying to populate datagrid with data from database

Posted 29 January 2013 - 09:02 AM

@ #2 modi123_1 what is a breaking point?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9258
  • View blog
  • Posts: 34,735
  • Joined: 12-June 08

Re: trying to populate datagrid with data from database

Posted 29 January 2013 - 09:06 AM

It's part of debugging 101.

Here's a tutorial on it:
http://www.dreaminco...4249-debugging/
Was This Post Helpful? 0
  • +
  • -

#5 navzgustav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 29-January 13

Re: trying to populate datagrid with data from database

Posted 29 January 2013 - 09:32 AM

@ #4 modi123_1 tried it out .it loop once then gives this error
"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"
at this line
 While rd.Read
            DataGridView1.Rows(i).Cells(1).Value = rd.Item("productname")
            DataGridView1.Rows(i).Cells(0).Value = rd.Item("productid")
            DataGridView1.Rows(i).Cells(2).Value = rd.Item("sellingprice")

            i = i - 1


Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9258
  • View blog
  • Posts: 34,735
  • Joined: 12-June 08

Re: trying to populate datagrid with data from database

Posted 29 January 2013 - 09:34 AM

Notice the subtraction sign...

 i = i - 1


Think how that affects the row you want to index. Do you feel you can access a row indexed as -1?
Was This Post Helpful? 0
  • +
  • -

#7 navzgustav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 29-January 13

Re: trying to populate datagrid with data from database

Posted 29 January 2013 - 09:39 AM

still bring the same error when i change it to
i=i+1


Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: trying to populate datagrid with data from database

Posted 29 January 2013 - 09:41 AM

it's out of range because you need to add rows to a dgv.

They currently don't exist.

Take a look at the add method for rows

Datagridview1.Rows.Add...

Or, you could just fill a dataset and set it as the datasource to the DGV making a lot less code.

This post has been edited by CharlieMay: 29 January 2013 - 09:42 AM

Was This Post Helpful? 0
  • +
  • -

#9 navzgustav  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 29-January 13

Re: trying to populate datagrid with data from database

Posted 30 January 2013 - 02:15 AM

@#8 CharlieMay thenx.that really worked out perfectly
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1