How to Create Multi User Login Form in VB.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 vb.net using SQL Server database and redirect to different form based on user role, then you are in the right tutorial.
First of all let’s understand what is multiuser login means; see a company may have a number of employees and each may have to access the same system from different computer that are connected through networking. And each employee have different designation. So, they need to access only their part in the system and should not access other’s account in that system. So the concept of multiuser in the system had evolved.
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
1. Open your Visual Studio and click on File>new project, From the left side of your window, select Visual Basic>Windows 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. 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, 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 (or right click over your form select view code) and write the following code:
a. import the namespace at the top of your code.
b. Inside the clicking event of that login button write the following code:
Dim con As SqlConnection = New SqlConnection("Data Source=RAN-PC;Initial Catalog=vbmultiuser;Integrated Security=True") Dim cmd As SqlCommand = New SqlCommand("select * from login where username='" &amp; txtUser.Text &amp; "' and password='" &amp; txtPass.Text &amp; "' and usertype='" &amp; cmbUserType.SelectedItem &amp; "'", con) Dim sda As SqlDataAdapter = New SqlDataAdapter(cmd) Dim dt As DataTable = New DataTable() sda.Fill(dt) If (dt.Rows.Count>0) Then MessageBox.Show(("you are login as " + dt.Rows(0)(2))) If (ComboBox1.SelectedIndex = 0) Then Dim a As New Admin a.Show() Me.Hide() Else Dim u As New User Me.Hide() u.Show() End If Else MessageBox.Show("error") End If
NOTE: Change this code in your context (because your connection string (Data Source = RAN-PC; Initial Catalog = vbmultiuser; 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. That’s it.
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: