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;
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);
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);
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
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.