title: How to insert data into SQL Server database to a datagridview using vb.net
body: <p>I want to retrieve data from a SQL Server 2012 database into a datagridview in vb. I created a connection class for connecting an each an every form to the db. I also created a method call <code>getData</code> to execute the SQL statement and fill the data for dataset. This is my code for the data <code>GetData</code> button</p>
  
  <pre><code>Private Sub btnGetData_Click(sender As Object, e As EventArgs) Handles btnGetData.Click
      Dim tmpDS As DataSet
  
      Try
  
          For rowIndex As Integer = 0 To DataGridView1.Rows.Count - 1
  
              Dim objcon As New clsCon
              objcon.GetData("SELECT * FROM StockMF")
  
              If objcon.DS.Tables(0).Rows.Count &gt; 0 Then
                  tmpDS = objcon.DS
  
                  DataGridView1.Rows(0).Cells(0).Value = tmpDS.Tables(0).Rows(0).Item("StockId")
                  DataGridView1.Rows(0).Cells(1).Value = tmpDS.Tables(0).Rows(0).Item("ItemNo")
                  DataGridView1.Rows(0).Cells(2).Value = tmpDS.Tables(0).Rows(0).Item("ItemNm")
                  DataGridView1.Rows(0).Cells(3).Value = tmpDS.Tables(0).Rows(0).Item("UnitPrice")
                  DataGridView1.Rows(0).Cells(4).Value = tmpDS.Tables(0).Rows(0).Item("AvailQty")
  
              End If
             Next rowIndex
  
      Catch ex As Exception
          MsgBox(ex.Message)
      End Try
  
  End Sub
  </code></pre>
  
  <p>But I only get one record from the database. How to correct this code retrieve all the records recorded in the database? I also need to know how to clear the datagridview</p>