Click here to Skip to main content
Licence 
First Posted 14 Nov 2006
Views 15,269
Bookmarked 8 times

Profiting from the WebForm designer generated code

By | 14 Nov 2006 | Article
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:

  1. Right click in the Web project directory root in Solution Explorer
  2. Click in the contextual menu Add -> Add Web Form.
  3. Click Open.

Add a DataAdapter:

  1. Click on View -> Toolbox (or press Ctrl-Alt-X)
  2. Click on Data. Drag a OleDbDataAdapter with the mouse into the Web Form page.
  3. Right click oleDbDataAdapter1. Click in the contextual menu Configure Data Adapter... Follow the wizard instructions.
  4. Right click oleDbDataAdapter1. Click in the contextual menu Generate Dataset… Follow the wizard instructions.
  5. 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:

  1. Right click in the Web project directory root in Solution Explorer
  2. Click in the contextual menu Add -> Add Class.
  3. 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

rmortega77

Software Developer
CIMEX S.A.
Cuba Cuba

Member

Rodolfo Ortega is a Cuban Computer Scientist. He works as IT Auditor for the CIMEX S.A. subsidiary in Holguin, Cuba. He lives and works in Holguin, in the eastern part of the island of Cuba.

You can contact him at rodolfom[]cimex.com.cu for any personal message: Ideas on new articles, bibliography about new APIs, questions, are wellcome.

Submit questions related with current article to the article forum.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 14 Nov 2006
Article Copyright 2006 by rmortega77
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid