How to Create Multiple User Login Form in C#.NET using SQL Server Database?
Hello guyz, if you are looking for the tutorial that shows how to create a multiple user login form in c#.net using SQL Server database and redirect to different form based on user role, then you are in the right tutorial.
In the process of solving real world problem, you may need to develop such a system which may have multiple number of user of a single system and when each user login to the system they must need to access only the certain features of an application which means they don’t need to access all the feature. In general the administrator have access to all the features and normal user may not have. So, this tutorial is all about this.
First of all you need to have Visual Studio and SQL Server Studio. you all can download these software from microsoft official website.
SQL Server: From Here
Visual Studio: From Here
Steps:
[sociallocker]
1. Open your visual studio of any version and then go to file new and then click on project. Then new window will appear and from the left pane select visual C#. under visual C# select window and then select window form application from the middle pane and give the name of your project and click OK.
2. Drag and drop three Label (for username, password and user type), two TextBox (for letting user to input username and password), one ComboBox (letting user to select the types of user like admin, user etc) and one Button(For login) from the toolbox and change the text and name of these control from the properties of these controls. And from the item properties of ComboBox add the items like admin, user etc. This complete the designing part.
3. 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:
and then give the column name such as username, password, usertype, set the data type as varchar for each column and save that table by pressing ctrl+s and give the table name whatever you want.
And then Right click your Table and then select Edit top 200 Rows click OK and then write the username, password and user type(like admin, user) and click close button; this will save the username password and user type in your table.
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(may be left 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 Double Click in that Login Button and write the following code:
a. import the namespace at the top of your code.
using System.Data.SqlClient;
b. Inside the clicking event of that login button write the following code:
SqlConnection con = new SqlConnection(@"Data Source = RAN-PC; Initial Catalog = cmblogin; Integrated Security = True"); SqlCommand cmd = new SqlCommand("select * from login where username='"+txtuser.Text+"' and password='"+txtpass.Text+"'", con); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); string cmbItemValue = comboBox1.SelectedItem.ToString(); if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { if (dt.Rows[i]["usertype"].ToString() == cmbItemValue) //you can use 2 instead of usertype in that index because usertype column is in 2 index { MessageBox.Show("you are login as " + dt.Rows[i][2]); if (comboBox1.SelectedIndex == 0) { Form2 f = new Form2(); f.Show(); this.Hide(); } else { Form3 ff = new Form3(); this.Hide(); ff.Show(); } } } } else { MessageBox.Show("error"); }
NOTE: Change this code in your context (because your connection string (Data Source = RAN-PC; Initial Catalog = cmblogin; Integrated Security = True) is different than mine)
6. Run your project by click the green triangular button, input username and password, select any comboxBox item and click Login. If your provided input is match with the database value you will be able to go to the new form otherwise get message box saying incorrect.
[/sociallocker]
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 🙂
If you have any confusion, you can watch the following videos: