Click here to Skip to main content
13,400,775 members (35,532 online)
Click here to Skip to main content
Add your own
alternative version


33 bookmarked
Posted 12 Sep 2011

Creating Code for Coding

, 18 Sep 2011
Rate this:
Please Sign up or sign in to vote.
Creating code to create code


Have you ever felt like doing the same tasks or writing similar code repetitive times? For example, creating a new web application, creating a database, running aspnet_regsql.exe, creating roles, writing web.config, creating a new form for login, coding the login...
What about having it at once with one click at all?


Some years ago, I was faced with an "easy request"; convert an Excel file into an application.
The deadline was one week, easy huh?
Opening the Excel file, it was nothing less than hundreds of sheets, that day I decided to create a virtual programmer, i.e., code to create code. In one week, the application was done.

How It Works?

Basically, the idea of the virtual programmer has the following features:

  • Read a template file, substitute tags, write an output file
  • Perform configuration operations

The current article shows up how to get in one click the following tasks/code:

  • Create a database
  • Run aspnet regsql to create membership tables
  • Create web.config
  • Create some roles and users
  • Create the Login form and code behind
  • Have a startup project with SQL Authentication

Note: Yes! There are many tools to perform the same task! But the idea here is the approach of creating code for coding helping to get rid of doing repetitive work and saving time. It means more time for coffee. :-)

Creating a Database

Our first task is to create a database, we can do this by the following code:

SqlCommand command = connection.CreateCommand();
command.CommandText = "CREATE DATABASE " + dbname;
// ! removed validations in the example above 

Executing aspnet.regsql

Now we should run aspnet_regsql manually once again. Instead, we will call SqlServices which will perform the same operation to create the aspnet tables in the DB.

SqlServices.Install(dbname, SqlFeatures.All, connectionstring);

Creating the web.config

The next task should be edit manually the web.config... But why?
We will read a template file, i.e. web_config.txt, perform a tag replacement and then save the output web.config file.

sContents = TextFileHelper.ReadTextFile("web_config.txt"); 
sContents = sContents.Replace("@CONNECTIONSTRING@", connectionstring);
sContents = sContents.Replace("@APPLICATIONNAME@", applicationname); 
TextFileHelper.WriteTextFile("web.config", sContents);  

Adding Roles and Users

To add roles and users to the application, we do have again template files. They are configure_aspx.txt and configure_aspx_cs.txt.
The concept is the same: Read the template file, substitute the tags and create the output files configure.aspx and configure.aspx.cs.

sContents = TextFileHelper.ReadTextFile("configure_aspx_cs.txt"); 
sContents = sContents.Replace("@ROLE1@", role1);
sContents = sContents.Replace("@ROLE2@", role2); 
sContents = sContents.Replace("@USER1@", user1);
sContents = sContents.Replace("@USER2@", user2); 
sContents = sContents.Replace("@USER1ROLE@", user1role);
sContents = sContents.Replace("@USER2ROLE@", user2role); 
TextFileHelper.WriteTextFile("configure.aspx.cs", sContents);  

The code on configure.aspx.cs:

UserHelper.CreateUser("@USER1@", "@PWD1@", "@ROLE1@");
UserHelper.CreateUser("@USER2@", "@PWD2@", "@ROLE2@"); 

After running the code above, it just calls the configure page configure.aspx through the browser and voila, you do have the skeleton of an ASP.NET application with SQL Authentication.

Running the Application


Fill up the application name and your connection string.

Click Proceed and voila, you do have a database and a functional website.


The website files are generated one folder under your application EXE / Output / chosen application name.

Now in VS, go to File / Open Website and open your new application.


Set your virtual path to /.


And run the configure.aspx. It will create the roles and an admin user.


Log in using the credentials admin adminpwd.

And here you are at default.aspx.


Points of Interest

This is only a sample where you can code to create your code. Think about the tasks you repetitively perform and create your own virtual programmer.



A subset of the code is being prepared to be placed online.


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Brazil Brazil
Senior Analyst

Linkedin Profile

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
WestieBoy24-Feb-13 18:11
memberWestieBoy24-Feb-13 18:11 
GeneralMy vote of 5 Pin
manoj kumar choubey26-Feb-12 22:47
membermanoj kumar choubey26-Feb-12 22:47 
Questionyour idea is good, but do you know "CodeSmith"? Pin
ZeChic19-Sep-11 18:56
memberZeChic19-Sep-11 18:56 
GeneralRe: your idea is good, but do you know "CodeSmith"? Pin
andalmeida19-Sep-11 22:31
memberandalmeida19-Sep-11 22:31 
AnswerRe: your idea is good, but do you know "CodeSmith"? Pin
P0110X27-Sep-11 5:36
memberP0110X27-Sep-11 5:36 
GeneralMy vote of 4 Pin
Abe Ek14-Sep-11 8:45
memberAbe Ek14-Sep-11 8:45 
GeneralMy vote of 5 Pin
fredatcodeproject13-Sep-11 23:32
memberfredatcodeproject13-Sep-11 23:32 
GeneralMy vote of 5 Pin
Milena Almeida13-Sep-11 0:18
memberMilena Almeida13-Sep-11 0:18 
GeneralRe: My vote of 5 Pin
Marcus Kramer13-Sep-11 9:42
memberMarcus Kramer13-Sep-11 9:42 
GeneralRe: My vote of 5 Pin
andalmeida13-Sep-11 10:36
memberandalmeida13-Sep-11 10:36 
GeneralRe: My vote of 5 Pin
Marcus Kramer15-Sep-11 6:15
memberMarcus Kramer15-Sep-11 6:15 
GeneralRe: My vote of 5 Pin
andalmeida16-Sep-11 0:33
memberandalmeida16-Sep-11 0:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180218.2 | Last Updated 18 Sep 2011
Article Copyright 2011 by andalmeida
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid