RunCodesRunCodes - Programming & Tech

  • Home
  • C#.NET
  • PHP
  • JAVA
  • Python
  • ASP.NET
  • VB.NET
  • Android
  • C
  • Tech
    • Tech News
  • Patreoner
RunCodes
  • Home
  • Programming
  • C#.NET
  • How to Create Multiple User Login Form in C#.NET using SQL Server Database?
C#.NET

How to Create Multiple User Login Form in C#.NET using SQL Server Database?

By Er. Ran Bahadur B.K. Last updated Aug 15, 2021
0 6,816
Share

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:

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.

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:

 

 

 

Continue Reading
C# multi-user winforms applicationHow to multi user login c#Multi account login C#Multiple Users from SQL Database for Login Form in C#.NETMultiple users login form for starting application in C#.NETsingle login form for multiple user in c#Single Login page for multiple user in c#Visual C# multi user windows form
0 6,816
Share FacebookTwitterReddItWhatsAppPinterestEmail
Er. Ran Bahadur B.K.60 posts 56 comments

Ran Bahadur B.K. is the owner of TechSupportNep who is working with the aim of providing the quality Technology content. He is creating Tech and Programming videos, you can see all them in his YouTube Channel(https://www.youtube.com/TechSupportNep). He is also working with some company.

Prev Post

How to Create Quiz Application in C#.NET using SQL Server Database and Visual Studio? [With Source Code]

Next Post

How to Create Quiz Application in Java Using MySQL Database and NetBeans IDE?[With Source Code]

Leave a comment
Help Needed

This website is free of annoying ads. We want to keep it like this. You can help with your support:

Donate!

The need for Support! Why to Support?

If you need your own website? contact me!
erranbahadurbk
Fiverr
Seller
Hello there,This is me Ran Bahadur B.K., A graduated computer engineer. I have 3+ years of work experience in wordpress design and development. I have strong knowledge of HTML, CSS and PHP which are the three ingredients of web development. Currently i am working with the software company so i also have experience in desktop apps and mobile apps development.

Latest

Python

How to Create Login Application in Python using SQLite Database?

How to use View Binding in Android?

Windows 10 Build 1909 Features that you should know.

How to Create a Complete Login and Forgot Password System in java ?

Prev Next 1 of 15
  • About us
  • Contact us
  • Privacy Policy
  • Disclaimer
© 2022 All Rights Reserved.
Website By: Ran Bahadur B.K.