In this tutorial we are going to talk about how to create user registration form in C#.NET using SQL Server database.
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 your Visual Studio and click on File>new project, From the left side of your window, select Visual C#>Window and from the middle side select Windows Forms Application and then give the name of your project and then click OK which is shown in below.
2. From the Toolbox Drag and Drop eight label, seven Textbox, one Combo Box and one button in your form and change the properties of these controls like name, text etc. by selecting each control and right click that particular control and click on properties.(You can change the color of each control, increase the font of each control)
NOTE: And change the password Text Box property PasswordChar to any character so that you can hide your password.
The final design of user registration form will looks like this:
3.We need to connect our application to the database for this, 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.
4. Double click on register button to go to the .cs section of your project and scroll up and then add the following namespace to your program as:
using System.Data; using System.Data.SqlClient;
5. Now write the following code inside the clicking event of the register button.
SqlConnection con = new SqlConnection("Data Source=RAN-PC;Initial Catalog=userregcs;Integrated Security=True"); SqlCommand cmd = new SqlCommand(@"INSERT INTO [dbo].[register] ([firstname] ,[lastname] ,[address] ,[gender] ,[email] ,[phone] ,[username] ,[password]) VALUES ('" + txtFname.Text + "', '" + txtLname.Text + "', '" + txtAdd.Text + "', '" + cmbGender.SelectedItem.ToString() + "', '" + txtEmail.Text + "', '" + txtPhone.Text + "', '" + txtUser.Text + "', '" + txtPass.Text + "')", con); con.Open(); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Register Successfully");
NOTE: Change this code in your context (because your connection string (Data Source=RAN-PC;Initial Catalog=userregcs;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: