• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 441
  • Last Modified:

ASP.NET - VB.NET - Need example syntax of how to create DataTable object from SQL table AND how to bind a DataGrid to that DataTable object

I'm working in ASP.NET, VB.NET.  I'm looking for example syntax of the following:

1. how to create DataTable object from SQL table

2. how to bind a DataGrid to that DataTable object

Also, I'm confused as to whether or not you must use a DataSet for my question #1, or if you can do this by creating a DataTable only.

thank you!!
0
sah18
Asked:
sah18
1 Solution
 
spdudeCommented:
Hi  sah18

how to create DataTable object from SQL table ?
1 create a datatable object
2 create a sqlconnection object and adapter

3 fill datatable with adapter.fill method

how to bind a DataGrid to that DataTable object ?

set datasource
and call bind method



Dim strCon As String
        strCon = "Data Source=TEK\sqlexpress;Initial Catalog=AdventureWorks;Integrated Security=True"
        Dim strSql As String
        strSql = "Select * from employee"

        Dim MysqlConnection As SqlConnection
        MysqlConnection = New SqlConnection(strCon)

        MysqlConnection.Open()

        Dim dataadapter As SqlDataAdapter
        dataadapter = New SqlDataAdapter(strSql, MysqlConnection)

        Dim mydatatable As DataTable
        mydatatable = New DataTable

        dataadapter.Fill(mydatatable)


        DataGrid1.DataSource = mydatatable
        DataGrid1.DataBind()

0
 
sah18Author Commented:
thank you!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now