2

This is my GET Method to get my data from my DataTable

Private Function GetData() As PagedDataSource
' Declarations    
Dim dt As New DataTable
Dim dr As DataRow
Dim pg As New PagedDataSource

' Add some columns    
dt.Columns.Add("Column1")
dt.Columns.Add("Column2")

' Add some test data    
For i As Integer = 0 To 10
    dr = dt.NewRow
    dr("Column1") = i
    dr("Column2") = "Some Text " & (i * 5)
    dt.Rows.Add(dr)
Next

' Add a DataView from the DataTable to the PagedDataSource  
pg.DataSource = dt.DefaultView

' Return the DataTable    
Return pg 
End Function 

It returns the DataTable as "pg"

What changes must I make to this GET method to get the records from a table in my database?

C# examples will also do but would be great to see a reply with my code and then the changes....

2
  • For this kind of things I use Linq-To-Sql or on heavy projects NHibernate. Consider using a ORM...
    – Yves M.
    Oct 8, 2010 at 8:25
  • I cant use LINQ because it is a 2.0 Project....
    – Etienne
    Oct 8, 2010 at 8:35

1 Answer 1

13

If Linq to SQL is not an option then you can fall back to ADO.NET. Essentially you will need to create a connection to your database and create and run a command to retrieve the data you require and populate a DataTable. Here is an example if C#:

// Create a connection to the database        
SqlConnection conn = new SqlConnection("Data Source=MyDBServer;Initial Catalog=MyDB;Integrated Security=True");
// Create a command to extract the required data and assign it the connection string
SqlCommand cmd = new SqlCommand("SELECT Column1, Colum2 FROM MyTable", conn);
cmd.CommandType = CommandType.Text;
// Create a DataAdapter to run the command and fill the DataTable
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.