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. Now we have to create Database in SQL Server Management Studio. 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 following picture after filling the column name and selecting the datatype:
save that table by pressing ctrl+s and give the table name whatever you want.
4. 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.
5. 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;
6. 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)
7. 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: