Every web developer needs a page from which the user information can be easily stored in the database from that required field in the page. We call this page a registration page or a sign up page in general.
In this article, I will explain how to create a simple registration page for you in ASP.NET website using C#.Net.
Procedure
Use the following simple Visual Studio procedure to create a .cs file and provide code in that page as I explain below in this article:
public RegNewUserResult DoRegNewUser(String username, String word, String location, String occupation, String interests, Boolean gender, int age)
{
if (this.IsUsernameExists(username))
{
return RegNewUserResult.UsernameAlreadyExists;
}
SqlConnection conn = new SqlConnection(Global.ConnectionString);
SqlCommand cmd = new SqlCommand("insert into sampleUsers (Username, word, Location, Occupation, Interests, Age, Gender,LastLoginTime,DateCreated) values (@Username, @word, @Location, @Occupation, @Interests, @Age, @Gender,GETDATE(),GETDATE())", conn);
// establishing connection string
SqlParameter paraUsername = cmd.Parameters.Add("@Username", SqlDbType.NVarChar);
paraUsername.Value = username;
// for username
SqlParameter paraword = cmd.Parameters.Add("@word", SqlDbType.NVarChar);
paraword.Value = word;
// for word
SqlParameter paraLocation = cmd.Parameters.Add("@Location", SqlDbType.NVarChar);
paraLocation.Value = location;
// for location
SqlParameter paraOccupation = cmd.Parameters.Add("@Occupation", SqlDbType.NVarChar);
paraOccupation.Value = occupation;
// for occupation
SqlParameter paraInterests = cmd.Parameters.Add("@Interests", SqlDbType.NVarChar);
paraInterests.Value = interests;
// for interest
SqlParameter paraAge = cmd.Parameters.Add("@Age", SqlDbType.Int, 4);
paraAge.Value = age;
// for age
SqlParameter paraGender = cmd.Parameters.Add("@Gender", SqlDbType.Bit);
paraGender.Value = gender;
// for age
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (Exception x)
{
return RegNewUserResult.DatabaseFail;
}
finally
{
conn.Close();
}
// Add new user to "Members" role
Global.AddUserToRole("Members", username);
return RegNewUserResult.Success;
}
public Boolean IsUsernameExists(String username)
{
SqlConnection conn = new SqlConnection(Global.ConnectionString);
SqlCommand cmd = new SqlCommand("select Username from sampleUsers where Username=@Username", conn);
SqlParameter paraUsername = cmd.Parameters.Add("@Username", SqlDbType.NVarChar);
paraUsername.Value = username;
try
{
conn.Open();
Object result = cmd.ExecuteScalar();
return result != null;
}
finally
{
conn.Close();
}
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
this.Response.Redirect("~/Default.aspx");
}
private void btnReg_Click(object sender, System.EventArgs e)
{
if (!this.IsValid)
{
return;
}
String sUsername = this.txtUsername.Text;
String sword = this.txtword.Text;
String sLocation = this.txtLocation.Text;
String sOccupation = this.txtOccupation.Text;
String sInterests = this.txtInterests.Text;
int dAge = Int32.Parse(this.txtAge.Text);
bool male = true;
if (rbtGirl.Checked) { male = false;}
RegNewUserResult result = this.DoRegNewUser(sUsername, sword, sLocation, sOccupation, sInterests, male, dAge);
if (result == RegNewUserResult.UsernameAlreadyExists)
{
JSHelper.MsgBox("Username [" + sUsername + "] is already exists. !");
}
else if (result == RegNewUserResult.DatabaseFail)
{
JSHelper.MsgBox("Database error, please try later.");
}
else if (result == RegNewUserResult.Success)
{
FormsAuthentication.SetAuthCookie(sUsername, false);
this.Response.Redirect("Default.aspx");
}
}
public enum RegNewUserResult
{
Success,
UsernameAlreadyExists,
DatabaseFail
}
Output Window