This example shows, how to create a very simple registration form in ASP.NET WebForms, using the traditional ADO.NET. Here’s, the glimpse of the page output:
Creating the Database
The following are the basic steps on how to create a simple database in SQL Server:
- Launch SQL Server Management Studio Express and then connect.
- Expand Databases folder from SQL Server object Explorer.
- Right click Databases folder and select “New Database”.
- From the pop-up Window, input the database name, which you would like and then click add.
- Expand Database folder, which you have just added.
- Right click on the Tables folder and select “New Table”.
- Add the following fields, given below:
Note: In this demo, we will set the Id to auto increment, so that the Id will be automatically generated for every new added row. To do this, select the Column name “Id” and in the column properties, set the “Identity Specification” to yes.
After adding all the necessary fields, name your database table to whatever you like. For this particular demo, we will just name it as “tblRegistration”.
Setting up the UI
For the simplicity of this demo, we will set up UI like:
Here’s, the equivalent ASPX Markup:
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head runat="server">
- <title>Sample Registration Page</title>
- <style type="text/css">
- .style1 {
- width: 100%;
- }
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <table class="style1">
- <tr>
- <td>Full Name:</td>
- <td>
- <asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>Username:</td>
- <td>
- <asp:TextBox ID="TxtUserName" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>Password:</td>
- <td>
- <asp:TextBox ID="TxtPassword" runat="server"
- TextMode="Password"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>Re Password:</td>
- <td>
- <asp:TextBox ID="TxtRePassword" runat="server"
- TextMode="Password"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>Address:</td>
- <td>
- <asp:TextBox ID="TxtAddress" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>Age:</td>
- <td>
- <asp:TextBox ID="TxtAge" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>Gender:</td>
- <td>
- <asp:DropDownList ID="DropDownList1" runat="server"
- AppendDataBoundItems="true">
- <asp:ListItem Value="-1">Select</asp:ListItem>
- <asp:ListItem>Male</asp:ListItem>
- <asp:ListItem>Female</asp:ListItem>
- </asp:DropDownList>
- </td>
- </tr>
- </table>
- </div>
- <asp:Button ID="Button1" runat="server" Text="Save"
- onclick="Button1_Click" />
- </form>
- </body>
- </html>
There’s nothing really fancy there. The markup, shown above, just contains some basic Server controls to compose the page.
Setting up the ConnectionString
Now, open the web.config file and configure the connection string, as shown below:
- <connectionStrings>
- <add name="DBConnectionString" connectionString="Data Source=YourDatabaseServerName;
- Initial Catalog=YourDatabase;
- Integrated Security=SSPI;"
- providerName="System.Data.SqlClient" />
- </connectionStrings>
Few things to note there: The “DBConnectionString” is the value, which we are going to use for referencing the connection string, we defined above. You need to change the value of Data Source and Initial Catalog, based on your Server name and database name.
The Member Class
Now, let’s create a new class, which would hold the following properties:
- public class Member{
- public string Name { get; set; }
- public string UserName { get; set; }
- public string Password { get; set; }
- public string Gender { get; set; }
- public string Age { get; set; }
- public string Address { get; set; }
- }
The Code Behind
Switch to your code behind the file and add the following method to get the connection string:
- public string GetConnectionString()
- {
- return ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
- }
We will be using ADO.NET objects to handle an insert operation to the database. If you are not familiar with ADO.NET, I would suggest you refer to the link, given below, to get started:
ADO.NET Tutorial
Here’s, the code block to insert the data to the database:
- private void ExecuteInsert(Member member) {
- using (SqlConnection sqlConn = new SqlConnection(GetConnectionString()))
- {
- string sql = @"INSERT INTO tblRegistration
- (Name, UserName, Password, Gender, Age, Address)
- VALUES
- (@Name,@UserName,@Password,@Gender,@Age,@Address)";
-
- using (SqlCommand sqlCmd = new SqlCommand(sql, sqlConn))
- {
- sqlCmd.Parameters.AddWithValue("@Name", member.Name);
- sqlCmd.Parameters.AddWithValue("@UserName", member.UserName);
- sqlCmd.Parameters.AddWithValue("@Password", member.Password);
- sqlCmd.Parameters.AddWithValue("@Gender", member.Gender);
- sqlCmd.Parameters.AddWithValue("@Age", member.Age);
- sqlCmd.Parameters.AddWithValue("@Address", member.Address);
-
- sqlConn.Open();
- sqlCmd.CommandType = CommandType.Text;
- sqlCmd.ExecuteNonQuery();
- }
- }
- }
The code, shown above, is the typical way of adding new data to the database, using ADO.NET. Notice, we pass the Member class as the parameter to the method. The Member class holds the data collected from the form. We’ll see this in the next step.
Now, let’s call the method, shown above, at button’s click event like:
- protected void Button1_Click(object sender, EventArgs e) {
- if (TxtPassword.Text == TxtRePassword.Text)
- {
-
- Member member = new Member();
- member.Name = TxtName.Text;
- member.UserName = TxtUserName.Text;
- member.Password = TxtPassword.Text;
- member.Gender = DropDownList1.SelectedItem.Text;
- member.Age = TxtAge.Text;
- member.Address = TxtAddress.Text;
-
- ExecuteInsert(member);
- Response.Write("Record was successfully added!");
- ClearControls(Page);
- }
- else
- {
- Response.Write("Password did not match");
- TxtPassword.Focus();
- }
- }
The code, shown above, checks if the password matches with the re-type password field. If it matches, it will call the method ExcuteInsert(), else, it will display an error message. You may have noticed the call to ClearControls() method from the code, shown above. Here’s the code for it:
- private static void ClearControls(Control Parent)
- if (Parent is TextBox)
- { (Parent as TextBox).Text = string.Empty; }
- else
- {
- foreach (Control c in Parent.Controls)
- ClearControls(c);
- }
- }
It's simple.