If you are searching for creating simple user registration form using ASP.NET with the use of SQL Server database, then you are in the right article. So without further more explanation let’s start.
First of all you have to have a Visual Studio and SQL Server installed on your system. If you don’t have, download and install from below links:
Visual Studio: HERE
SQL Server: HERE
1. Open Visual Studio and then go to file>new>project. Select Visual C#>Web>ASP.NET Web Application. Give the project name and click OK. Then new window will popup asking you to choose the Templates and select empty and then click OK. Now, right click over your project from solution explorer and then click add>web form, click OK.
2. Now write the following code inside the body tag of the aspx page to design the UI.
<form id="form1" runat="server"> <div> <table align="center"> <tr> <td>Full Name</td> <td><asp:TextBox ID="txtName" runat="server" placeholder="enter full name"></asp:TextBox></td> </tr> <tr> <td>Address:</td> <td><asp:TextBox ID="txtAdd" runat="server" placeholder="enter your address"></asp:TextBox></td> </tr> <tr> <td>Gender:</td> <td> <asp:RadioButton GroupName="user" ID="RadioButton1" runat="server" Text="Male" OnCheckedChanged="RadioButton1_CheckedChanged"/><br /> <asp:RadioButton GroupName="user" ID="RadioButton2" runat="server" Text="Female" OnCheckedChanged="RadioButton2_CheckedChanged" /><br /> <asp:RadioButton GroupName="user" ID="RadioButton3" runat="server" Text="others" OnCheckedChanged="RadioButton3_CheckedChanged" /> </td> </tr> <tr> <td>Email:</td> <td><asp:TextBox ID="txtEmail" runat="server" placeholder="firstname.lastname@example.org" TextMode="Email"></asp:TextBox></td> </tr> <tr> <td>Phone:</td> <td><asp:TextBox ID="txtPhone" runat="server" placeholder="+977-123-456-7890"></asp:TextBox></td> </tr> <tr> <td>Username:</td> <td><asp:TextBox ID="txtUser" runat="server" placeholder="username"></asp:TextBox></td> </tr> <tr> <td>Password</td> <td> <asp:TextBox ID="txtPass" runat="server" placeholder="********" TextMode="Password"></asp:TextBox></td> </tr> <tr> <td></td> <td align="right"><asp:Button ID="btnReg" runat="server" Text="Register" OnClick="btnReg_Click"/></td> </tr> </table> </div> </form>
3.Now we need to connect our application to the database. To create a database and table in SQL Server Database, open SQL Server application, and Right click over Database and then click new, give the database name whatever you want and click OK. After then expand that Database directory which you can see in the left pane of the SQL Server Studio Software and expand that Database directory and then expand your recently created Database. And then you will see Table directory. Right click that Table Directory click on Table and click OK. You will see the new window which looks like this:
Note: Give the column name like name, phone, email, username, password, gender, address etc and select the data type of each column name. varchar for all column name and char for gender column name.
4. We need to connect our application to the database for this go back to visual studio, click on the Server Explore which is in the right side of the Toolbox in the left pane of your visual studio. And then right click on the Data Connection>Add connection. New window will appear select your Data Source; in our Case Data Source is Microsoft SQL Server. To do this click on change and select Microsoft SQL Server, Click OK. And then New window will appear, select your Server Name your Database Name and click OK.
5.Now change webform’s view from source to design view. Then you will be able to see your designed form.
Now Double Click in that Register button and write the following code:
a.import the namespace at the top of the class.
using System.Data; using System.Data.SqlClient;
b. Declare the variable inside the Class which store the RadioButton value after clicking it.
c. Double Click each Radio Button and inside each Radio Button clicking event write the following code:
gender="male"; gender="female"; gender="others";
d. Finally write the logic of the program inside the Register button clicking event by double clicking the Register Button of your web form.
SqlConnection con = new SqlConnection("Data Source=RAN-PC;Initial Catalog=userreg;Integrated Security=True"); SqlCommand cmd = new SqlCommand(@"INSERT INTO [dbo].[userreg] ([name] ,[address] ,[gender] ,[phone] ,[email] ,[username] ,[password]) VALUES ('" +txtName.Text+ "', '" + txtAdd.Text + "', '" + gender + "', '" + txtPhone.Text + "', '" + txtEmail.Text + "', '" + txtUser.Text + "', '" + txtPass.Text+"')",con); con.Open(); cmd.ExecuteNonQuery(); con.Close(); Response.Write("<script>alert('user is registered successfully')<script>");
NOTE: Change this code in your context (because your connection string (Data Source=RAN-PC;Initial Catalog=userreg;Integrated Security=True) is different than mine)
6. Run your project by click the green triangular button in any browser, fill the form and click that register button, you will get the successful message. That’s it. And if you like this tutorial don’t forget to share this with your friends and write your perception about this article in your comment section. Thank you 🙂
Watch the following video if you have any confusion: