Thursday, May 5, 2011

Databind() on gridview not working VB.net ASP.Net

I have a datatable with verified 10 rows named dtable. I attempt to bind it to my gridview and nothing shows up on my asp.net page. Why do these relatively simple few lines not populate my gridview?

    Dim con As SqlConnection = Nothing
    Dim dTable As DataTable
    Dim dataAdapter As SqlDataAdapter
    Dim sqlcmd As String
    .......
    dTable = New DataTable
    dataAdapter = New SqlDataAdapter(sqlcmd, con)
    dataAdapter.Fill(dTable)

    'DATATABLE at this point has 10 rows in it, I verified that at runtime

    gvUnitSystem.DataSource = dTable
    gvUnitSystem.DataBind()

End Sub

What am I doing wrong?

From stackoverflow
  • Have you made sure that the GridView has AutoGenerateColumns set to true?

    gvUnitSystem.AutoGenerateColumns = True
    
  • This looks fine. Are you auto-populating columns or have you manually added columns in the designer? If it is the latter maybe none of the column fields match fields in your table.

  • You need to set the con (connection string).

0 comments:

Post a Comment