RadGrid for ASP.NET

DataSourceHelper files used in some of the examples Send comments on this topic.
Populating the control with data > DataSourceHelper files used in some of the examples

Glossary Item Box

In some of the code samples in this section we reference DataSourceHelperCS/DataSourceHelperVB files which are part of the Telerik RadGrid installation (reside in the NET1/Grid/Examples folder). Below is the content of these files:

[DataSourceHelperCS.cs]

C# Copy Code
public class DataSourceHelperCS
{

 
public static string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + System.Web.HttpContext.Current.Server.MapPath( "~/Grid/Data/Access/Nwind.mdb");

 
public static DataTable GetDataTable(string query)
 {
  OleDbConnection MyOleDbConnection =
new OleDbConnection(connectionString);
  OleDbDataAdapter MyOleDbDataAdapter =
new OleDbDataAdapter();
  MyOleDbDataAdapter.SelectCommand =
new OleDbCommand(query, MyOleDbConnection);

  DataTable myDataTable =
new DataTable();

MyOleDbConnection.Open();
try { MyOleDbDataAdapter.Fill(myDataTable); } finally { MyOleDbConnection.Close(); }

  
return myDataTable;
 }

 
public static DataTable GetDataTable(string query, string dbFile)
 {
  OleDbConnection MyOleDbConnection =
new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + System.Web.HttpContext.Current.Server.MapPath(dbFile));
  OleDbDataAdapter MyOleDbDataAdapter =
new OleDbDataAdapter();
  MyOleDbDataAdapter.SelectCommand =
new OleDbCommand(query, MyOleDbConnection);

  DataTable myDataTable =
new DataTable();

MyOleDbConnection.Open();
try { MyOleDbDataAdapter.Fill(myDataTable); } finally { MyOleDbConnection.Close(); }



  
return myDataTable;
 }
}

[DataSourceHelperVB.vb]
VB.NET Copy Code
Public Class DataSourceHelperVB

  Public Shared connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & System.Web.HttpContext.Current.Server.MapPath( "~/Grid/Data/Access/Nwind.mdb")

  Public Shared Function GetDataTable(ByVal query As String) As DataTable
   Dim connection1 As New OleDbConnection(connectionString)
   Dim adapter1 As New OleDbDataAdapter
   adapter1.SelectCommand = New OleDbCommand(query, connection1)
   Dim table1 As New DataTable
   connection1.Open()
   Try
    adapter1.Fill(table1)
   Finally
    connection1.Close()
   End Try
   Return table1
  End Function

  Public Shared Function GetDataTable(ByVal query As String, ByVal dbFile As String) As DataTable
   Dim connection1 As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & System.Web.HttpContext.Current.Server.MapPath(dbFile))
   Dim adapter1 As New OleDbDataAdapter
   adapter1.SelectCommand = New OleDbCommand(query, connection1)
   Dim table1 As New DataTable
   connection1.Open()
   Try
    adapter1.Fill(table1)
   Finally
    connection1.Close()
   End Try
   Return table1
  End Function

 End Class

The purpose of our developers was to isolate the logic which makes the connection to the database and returns the record set for the grid in a separate file (apart from the other page content). To reference these files in your C# / VB.NET project, you merely need to copy them in the root of your web application and change the namespace which wraps them (if there is such) by replacing it with your application namespace.