Introduction
This article shows how to create a user register page for your website and bind it to your database. We use the usercreate template provided by C#.
Background
There were times when people used to hand-code everything required to do a full user registration page. This involves creating an HTML interface,
adding validation (password and confirm password should match, email should be of format xxx@xx.xx etc.). The newer version of .NET provides
a template to quickly create one with less coding.
Using the code
Drop the 'CreateUserWizard
' control from the tool box. It drops the following code on your ASPX page:
<asp:CreateUserWizard ID="CreateUserWizard1" Runat="server"
ContinueDestinationPageUrl="~/Default.aspx"
DisableCreatedUser="True"
EmailRegularExpression="\S+@\S+\.\S+"
EmailRegularExpressionErrorMessage="The email format is invalid."
FinishDestinationPageUrl="~/Default.aspx" UserNameLabelText="Login Email:"
UserNameRequiredErrorMessage="Login Email required."
oncreateduser="CreateUserWizard1_CreatedUser">
<WizardSteps>
<asp:CreateUserWizardStep ID="MainStep" runat="server">
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep runat="server"></asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
Go to Design view and double click the Create User button to open a code-behind event place holder, and drop the following code which saves the new user content entered
in the template to the database.
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
int nMaxId = 0;
System.Data.SqlClient.SqlDataReader rdr = null;
System.Data.SqlClient.SqlConnection conn =
new System.Data.SqlClient.SqlConnection(
ConfigurationManager.ConnectionStrings[
"namodbConnectionString"].ConnectionString);
System.Data.SqlClient.SqlCommand selcmd =
new System.Data.SqlClient.SqlCommand(
"select max(usrid) maxi from usr", conn);
try
{
conn.Open();
rdr = selcmd.ExecuteReader();
while (rdr.Read())
{
nMaxId = (int)rdr["maxi"];
}
++nMaxId;
}
finally
{
if (rdr != null)
{
rdr.Close();
}
}
if (this.IsValid)
{
try
{
System.Data.SqlClient.SqlCommand insertCommand =
new System.Data.SqlClient.SqlCommand("Insert into [usr] (usrid," +
"usremail, roleid, activestat) Values ("+nMaxId+",
@UsrEmail, 2, 0)", conn);
TextBox UserNameTextBox =
(TextBox)MainStep.ContentTemplateContainer.FindControl(
"UserName");
insertCommand.Parameters.Add("UsrEmail",
SqlDbType.VarChar, 255).Value =
UserNameTextBox.Text;
int queryResult = insertCommand.ExecuteNonQuery();
if (conn != null)
{
conn.Close();
}
}
catch (Exception ex)
{
lblResult.Text = "Error: " + ex.Message;
}
}
}
Points of Interest
This tutorial guides you to quickly setup a user registration page in minutes. It also shows an easy database connection for reading data and writing data. Just to show the read data,
the tutorial uses the nMaxId
variable that gets its value from the max value of user ID in the table. This is an easy way to implement an auto-increment field.
Founder of www.namoona.com, a multilingual classifieds portal for indian cities. Working in open systems development since 1997 in Delphi, Java, C++ and C#.