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)
{
dataSet.EnforceConstraints = false;
try
{
this.oleDbConnectionAdminPTPC.Open();
this.oleDbDataAdapter_TABLE_NAME.Fill(dataSet);
}
catch (System.Exception fillException)
{
throw fillException;
}
finally
{
dataSet.EnforceConstraints = true;
this.oleDbConnectionAdminPTPC.Close();
}
}
public void LoadDataSet()
{
ASSEMBLY.DATASET_NAME objDataSetTemp;
objDataSetTemp = new ASSEMBLY.DATASET_NAME ();
try
{
this.FillDataSet(objDataSetTemp);
}
catch (System.Exception eFillDataSet)
{
throw eFillDataSet;
}
try
{
obj_NAME_DATASET.Clear();
obj_NAME_DATASET.Merge(objDataSetTemp);
}
catch (System.Exception eLoadMerge)
{
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.