zzprog
zzprog

Reputation: 113

Insert data from SQL Server table into datagrid vb 2003

I want to insert data from a SQL Server table into a data grid.

Here is my code:

    Dim cnConnect As New SqlConnection
    cnConnect.ConnectionString = conn.ConnectionString
    cnConnect.Open()

    Dim cm As New SqlCommand
    cm.CommandText = "SELECT * FROM user"

    Dim Adpt As New SqlDataAdapter(cm.CommandText, cnConnect)
    Dim ds As New DataSet
    Adpt.Fill(ds, "user")

    DataGrid2.DataBind()

I try to bind the data to data grid. but the result is blank

Upvotes: 1

Views: 81

Answers (2)

Ramgy Borja
Ramgy Borja

Reputation: 2458

make it simple, try this coding style

    Dim cnConnect As New SqlConnection
    cnConnect.ConnectionString = conn.ConnectionString
    cnConnect.Open()

    Dim cm As New SqlCommand
    Dim dt AS DataTable

    cm.CommandText = "SELECT * FROM user"
    cm.Connection = cnConnect
    dt.Load(cm.ExecuteReader())
    DataGrid2.DataSource = dt
    DataGrid2.DataBind()

Upvotes: 0

jmcilhinney
jmcilhinney

Reputation: 54487

You haven't actually provided any data to the grid to bind to. You have to set the DataSource first so that the grid has a source to bind to when you call DataBind.

Upvotes: 3

Related Questions