LearnFast.Ninja

Get DataTable No Parameters – ASP.NET get table data

Put this function in your static data access (dal) class. This function get a SQL query as parameter and return DataTable with data from the database. Note if the connection is close before Fill was call it is opened to retrieve data and then closed. If the connection is open before Fill is called then it will remain open.

Instructions: Put this function in your static data access (dal) class.

public static DataTable GetDataTableNoParameters(string Query)

{

    string myConnectionString = WebConfigurationManager.ConnectionStrings["Connection_Name"].ConnectionString;

    SqlConnection oSqlConnection = new SqlConnection(myConnectionString);

    try

    {

        SqlCommand cmd = new SqlCommand(Query, oSqlConnection);

        SqlDataAdapter oSqlDataAdapter = new SqlDataAdapter(cmd);

        DataTable DataTable_To_Fill = new DataTable();

        oSqlDataAdapter.Fill(DataTable_To_Fill);

        return DataTable_To_Fill;

    }

    finally

    {

        oSqlConnection.Close();

    }

}// end function


Tags

asp.net

dal dataaccess datatable fill adapter sqldataadapter sqlcommand sqlconnection webconfigurationmanager