Profiting from the WebForm designer generated code






2.71/5 (3 votes)
Nov 14, 2006
2 min read

22333
Create a class with DB access code, from the Webform designer generated code.
Introduction
Visual Studio IDE auto-generated code helps us see the way we could program with objects to create more complicated stuff and increase productivity, profiting from it. Here is a simple example to read from the database, just as when a Web Form is loaded in to the IDE, and all the code is still generated by the Visual Studio IDE.
Using the code
First, create a temporary Web Form:
- Right click in the Web project directory root in Solution Explorer
- Click in the contextual menu Add -> Add Web Form.
- Click Open.
Add a DataAdapter:
- Click on View -> Toolbox (or press Ctrl-Alt-X)
- Click on Data. Drag a
OleDbDataAdapter
with the mouse into the Web Form page. - Right click
oleDbDataAdapter1
. Click in the contextual menu Configure Data Adapter... Follow the wizard instructions. - Right click
oleDbDataAdapter1
. Click in the contextual menu Generate Dataset… Follow the wizard instructions. - Double click on the web form, to switch to the source code view.
From the class declaration:
public class _Default : System.Web.UI.Page
{
protected System.Data.OleDb.OleDbDataAdapter
oleDbDataAdapter_TABLE_NAME;
protected System.Data.OleDb.OleDbCommand oleDbSelectCommandX;
protected System.Data.OleDb.OleDbConnection
oleDbConnection_CONNECTION_NAME;
protected VERTICE.WebminPTPC._NAME_DATASET objDataSet;
Copy the declaration of the created objects.
Now, create your new class, which will have no GUI:
- Right click in the Web project directory root in Solution Explorer
- Click in the contextual menu Add -> Add Class.
- Click Open.
Now, to the class declaration:
public class Class1
{
Add the copied declarations from the Web Form page designer code view.
public class Class1
{
protected System.Data.OleDb.OleDbDataAdapter
oleDbDataAdapter_TABLE_NAME;
protected System.Data.OleDb.OleDbCommand oleDbSelectCommandX;
protected System.Data.OleDb.OleDbConnection
oleDbConnection_CONNECTION_NAME;
protected VERTICE.WebminPTPC._NAME_DATASET objDataSet;
From the Web Form Designer generated code region:
#region Web Form Designer generated code
…
#endregion
Copy all that is between the above and paste it into the Class1
declaration.
Delete the last lines from the InitializeComponent
method:
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.objdsSession)).EndInit();
Add the methods FillDataSet
and LoadDataSet
, change ASSEMBLY.DATASET_NAME
using your assembly and DataSet
names:
public void FillDataSet(ASSEMBLY.DATASET_NAME dataSet)
{
// Turn off constraint checking before the dataset is filled.
// This allows the adapters to fill the dataset without concern
// for dependencies between the tables.
dataSet.EnforceConstraints = false;
try
{
// Open the connection.
this.oleDbConnectionAdminPTPC.Open();
// Attempt to fill the dataset through the oleDbDataAdapter
this.oleDbDataAdapter_TABLE_NAME.Fill(dataSet);
}
catch (System.Exception fillException)
{
// Add your error handling code here.
throw fillException;
}
finally
{
// Turn constraint checking back on.
dataSet.EnforceConstraints = true;
// Close the connection whether or
// not the exception was thrown.
this.oleDbConnectionAdminPTPC.Close();
}
}
public void LoadDataSet()
{
// Create a new dataset to hold the records
// returned from the call to FillDataSet.
// A temporary dataset is used because
// filling the existing dataset would
// require the databindings to be rebound.
ASSEMBLY.DATASET_NAME objDataSetTemp;
objDataSetTemp = new ASSEMBLY.DATASET_NAME ();
try
{
// Attempt to fill the temporary dataset.
this.FillDataSet(objDataSetTemp);
}
catch (System.Exception eFillDataSet)
{
// Add your error handling code here.
throw eFillDataSet;
}
try
{
// Empty the old records from the dataset.
obj_NAME_DATASET.Clear();
// Merge the records into the main dataset.
obj_NAME_DATASET.Merge(objDataSetTemp);
}
catch (System.Exception eLoadMerge)
{
// Add your error handling code here.
throw eLoadMerge;
}
}
Points of Interest
That’s it! It works for me. I used this to create a custom Session variables reader, adding the LoadDataSet()
method to the Class1
constructor and calling it from every page.
History
- Nov. 13, 2006 - First version.