Registration or Sign up is an important option present in almost all login forms/Pages. The new user or first time user has to fill the details in the registration form before they can use the sign in option. (In most cases).
How a registration page works?
The registration page/form contains a number of fields/columns to be filled by the new user. The user is asked to fill details like username, password, mobile number, DoB, gender,email and so on. These fields are different for different applications. Once, the user enters all these values clicks the submit button. The soon the submit button is clicked, all these entered values are inserted into the table present in the Database.(use one DBMS software such as Windows SQL server management studio).
Hence, you should create a table containing the same columns which are given in the registration page. The username is made as a primary key.(usually). The primary key is an attribute which uniquely identifies an entity in an entity set. (i.e. it doesn't allow duplicate values). In other words, two users cannot use the same username for registration. Hence, Username should be used as primary key and necessary code should be written in the coding part to display a message if the user enters an existing username.
Creating a simple registration page:
1. open visual studio 2013. Open a new form. Name it as Registration form.
2. Drag and drop the required controls from the toolbox onto the form. For example, here i have included 5 textBoxes for five fields such as username, password, Full name, mobile no and email id. Remember you should have a table in your database with these five attributes to store these values. Design your form as shown below.
![]() |
Registration Form |
Name of the controls are, textbox1, textbox2, textbox3,textbox4,textbox5 for the five textboxes username, password, full name, mobile no and email id respectively.
The name of the register button is button1.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Collections.Specialized;
namespace studentdata
{
public partial class RegistrationForm : Form
{
public RegistrationForm()
{
InitializeComponent();
}
//here UsernameCheck is a user defined function which checks whether the
//entered username already exists. This function is called or invoked in the
// textbox1_Text Changed event shown at the end of the code.
public void UserNameCheck()
{
string constring = ConfigurationManager.ConnectionStrings
["ConnData"] .ConnectionString;
SqlConnection con = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand("Select * from registration
where Username= @Username", con);
cmd.Parameters.AddWithValue("@Username", this.textBox1.Text);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr.HasRows == true)
{
MessageBox.Show("Username = "+ dr[1].ToString() +" Already exist");
clearall();
break;
}
}
}
// This is another user defined function which clears all values in the textboxes. This function is called in the
// UsernameCheck function.
public void clearall()
{
textBox1.Text = " ";
textBox2.Text = " ";
textBox3.Text = " ";
textBox4.Text = " ";
textBox5.Text = " ";
}
// This function is used to store the inserted data into the table. This is called in the button1_click event.
public void datastore()
{
string Uname, Password, Fullname, MobileNO, EmailID;
Uname = textBox1.Text;
Password = textBox2.Text;
Fullname = textBox3.Text;
MobileNO = textBox4.Text;
EmailID = textBox5.Text;
try
{
string constring = ConfigurationManager.ConnectionStrings
["ConnData"].ConnectionString;
/* Declaring Connection Variable */
SqlConnection con = new SqlConnection(constring);
if (con.State != ConnectionState.Open)
con.Open();
// here command type is stored procedure. Hence, the sql query is written as a //strored procedure in the SQL server mangement studio. The statement in the //stored procedure is given at the end of this article.
SqlCommand cmd = new SqlCommand("regis", con);
cmd.CommandType = CommandType.StoredProcedure;
/* Passing Input Parameters with Command */
cmd.Parameters.AddWithValue("@Username", Username);
cmd.Parameters["@Username"].Direction=ParameterDirection. Input;
cmd.Parameters.AddWithValue("@Password", Password);
cmd.Parameters["@Password"].Direction =ParameterDirection.Input;
cmd.Parameters.AddWithValue("@Fullname", Fullname);
cmd.Parameters["@Fullname"].Direction = ParameterDirection.Input;
cmd.Parameters.AddWithValue("@MobileNO", MobileNO);
cmd.Parameters["@MobileNO"].Direction =ParameterDirection.Input;
cmd.Parameters.AddWithValue("@EmailID", EmailID);
cmd.Parameters["@EmailID"].Direction = ParameterDirection.Input;
/* Executing Stored Procedure */
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Data Inserted Succesfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
datastore();
this.Hide();
LoginForm lf = new LoginForm();
lf.Show();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
UserNameCheck();
}
}
}
The SQL statement use in the stored procedure may look like,
insert into registration(Username, Password,Fullname, Mobileno,EmailId )values ( @Username,@Password, @Fullname,@Mobileno,@EmailId);
This code may contain a few errors as it is manually typed here. Thank you.
Enjoy Programming.
Click here to know "How to create login form".
No comments:
Post a Comment