1,105,402 Community Members

VB.NET - Populating a datagrid using datatable and sqlServer express

Member Avatar
jacobenade
Newbie Poster
2 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

PLease help!!!!!
How the ... do i fill my dataset with data using sql server, datatable, and adapter...

I've passed the select statement [ Select * from [TableName] and a ref of my datagrid to this procedure.

Public Sub FillDataGrid(ByRef DataGrid As DataGrid, ByVal SelectCommand As String)

Try

Dim adSQLAdapter As New SqlDataAdapter(SelectCommand, mSQLConnection)
Dim commands As New SqlCommandBuilder(adSQLAdapter)
Dim dtTable As New DataTable()

'POPULATE THE DATATABLE WITH DATA.
adSQLAdapter.Fill(dtTable)
DataGrid.DataSource = dtTable

Catch ex As Exception
CreateErrLog(ex.ToString)
End Try
End Sub

Member Avatar
TomW
Posting Whiz
342 posts since Sep 2009
Reputation Points: 73 [?]
Q&As Helped to Solve: 48 [?]
Skill Endorsements: 3 [?]
 
0
 
Member Avatar
jacobenade
Newbie Poster
2 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Nevermind...after laying in bed i realized that i needed to send a BYVAL reference and not BYRef. Sorted....Thanks myself!!!!

Member Avatar
pauloludele
Newbie Poster
5 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

PLease help!!!!!
How the ... do i fill my dataset with data using sql server, datatable, and adapter...

I've passed the select statement [ Select * from [TableName] and a ref of my datagrid to this procedure.

Public Sub FillDataGrid(ByRef DataGrid As DataGrid, ByVal SelectCommand As String)

Try

Dim adSQLAdapter As New SqlDataAdapter(SelectCommand, mSQLConnection)
Dim commands As New SqlCommandBuilder(adSQLAdapter)
Dim dtTable As New DataTable()

'POPULATE THE DATATABLE WITH DATA.
adSQLAdapter.Fill(dtTable)
DataGrid.DataSource = dtTable

Catch ex As Exception
CreateErrLog(ex.ToString)
End Try
End Sub

Try this

Public Function GetRecordDB(ByVal table As String) As DataSet
        Dim con As SqlConnection = GetconnectionDB()

        Try
            Dim query As String = "SELECT * from " & table
            Dim ds As New DataSet
            Dim da As SqlDataAdapter = New SqlDataAdapter(query, con)
            Try
                da.Fill(ds, table)
            Finally
                da.Dispose()
            End Try
            Return ds
        Finally
            con.Close()
        End Try
    End Function

Then u can call this method from your Form_Load Event as follows

Dim ds As DataSet
ds = SqlDB.GetRecordDB("tablename")
        Me.dgAdmin.DataSource = ds
        Me.dgAdmin.DataMember = "admission"
        dgAdmin.ReadOnly = 1
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article