Initial Chamber
Step 1
Open your Visual Studio 2010 and create an Empty Website, provide it a suitable name (RegForm_demo).
Step 2
In the Solution Explorer you get your empty website, then add two web forms and a SQL Server database as in the following.
For Web Form
RegForm_demo (your empty website), right-click and Add New Item, then select Web Form and name it Reg_demo.aspx. Repeat the same process and add another web form and name it (Redirectpage.aspx)
For SQL Server Database
RegForm_demo (your empty website), right-click and Add New Item, then select SQL Server Database. (Add a database inside the App_Data_folder.)
Database Chamber
Step 3
In Server Explorer, click on your database (Database.mdf), go to Tables and Add New Table, create a table like the following:
Make a Stored Procedure for inserting data into the database and for checking the database for the same username or not.
- Sp_check:
- Sp_insert:
Design Code
Step 4
Now make some design for your application by going to Reg_demo.aspx and try the code like the following:
Your design will look like the following:
Fire the event for the username TextBox, then click on Text_changed and by going to the username_textbox, press F4, go to the Properties window and open up events, then click on Textbox_changed.
Code Chamber
Step 5
We will add some code to the Reg_demo.aspx.cs page so that our Register form works correctly.
Reg_demo.aspx.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.SqlClient;
-
- public partial class Default2 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
- protected void TextBox1_TextChanged(object sender, EventArgs e)
- {
- if (!string.IsNullOrEmpty(TextBox1.Text))
- {
- SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
- SqlCommand cmd = new SqlCommand("select * from tbl_data where username=@username", con);
- cmd.Parameters.AddWithValue("username", TextBox1.Text);
- con.Open();
- SqlDataReader sdr = cmd.ExecuteReader();
- if (sdr.HasRows)
- {
- checkusername.Visible = true;
- Label1.Text = "Stop Here UserName Exist";
- Label1.ForeColor = System.Drawing.Color.Red;
- TextBox2.Visible = false;
- TextBox3.Visible = false;
- TextBox4.Visible = false;
- TextBox5.Visible = false;
- Button1.Visible = false;
- Label2.Visible = false;
- Label3.Visible = false;
- Label4.Visible = false;
- Label5.Visible = false;
- }
- else
- {
- checkusername.Visible = true;
- Label1.Text = "Please Proceed";
- Label1.ForeColor = System.Drawing.Color.Green;
- TextBox2.Visible = true;
- TextBox3.Visible = true;
- TextBox4.Visible = true;
- TextBox5.Visible = true;
- Button1.Visible = true;
- Label2.Visible = true;
- Label3.Visible = true;
- Label4.Visible = true;
- Label5.Visible = true;
- }
- }
- else
- {
- checkusername.Visible = false;
- }
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
- SqlCommand cmd = new SqlCommand("sp_insert", con);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("username", TextBox1.Text);
- cmd.Parameters.AddWithValue("word", TextBox2.Text);
- cmd.Parameters.AddWithValue("email", TextBox4.Text);
- cmd.Parameters.AddWithValue("phoneno", TextBox5.Text);
- con.Open();
- cmd.ExecuteNonQuery();
- if (TextBox2.Text== TextBox3.Text)
- {
- Response.Redirect("Default.aspx");
- }
- else
- {
- Label6.Text = "word Dont Match";
- }
- con.Close();
- }
- }
In the Redirect page you can add anything, here we are adding a HTML website. Now, we are ready with the application and here is the output.
Output ChamberThen click on the register button, the data is saved into the database as “Rinku”, he is the new user here and also the user will get redirected to the other page.
Now by entering this data again you can't proceed to the next step of registration since it will disable all the textboxes until you change the username.
I hope you liked this. Thank you for reading. Have a nice day!