How to Create Multiple User Login Form in ASP.NET using SQL Server Database?
In this video tutorials we are going to talk about how to create a multiple user login form in ASP.NET using SQL Server database and redirect to different form based on user role.
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 Visual Studio and then go to file>new>project. Select Visual C#>Web>ASP.NET Web Application. Give the project name and click OK. Then new window will popup asking you to choose the Templates and select empty and then click OK. Now, right click over your project from solution explorer and then click add>web form, click OK.
Now write the following code inside the body tag of the aspx page to design the UI.
<form id="form1" runat="server"> <div> <table align="center"> <tr> <td font size="30px">Username</td> <td> <asp:TextBox ID="txtUser" runat="server"></asp:TextBox></td> </tr> <tr> <td>Password</td> <td> <asp:TextBox ID="txtPass" runat="server" TextMode="Password"></asp:TextBox></td> </tr> <tr> <td>Select User Type</td> <td> <asp:DropDownList ID="DropDownList1" runat="server" Font-Size="Large"> <asp:ListItem>admin</asp:ListItem> <asp:ListItem>user</asp:ListItem> </asp:DropDownList></td> </tr> <tr align="right"> <td class="auto-style1"></td> <td class="auto-style1"> <asp:Button ID="Button1" runat="server" Text="Login" OnClick="Button1_Click" Font-Size="Large" Height="37px" Width="67px" /></td> </tr> </table> </div> </form>
now we need to connect our application to the database. 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.
nd 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.
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; 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=aspmuser;Integrated Security=True"); SqlDataAdapter sda = new SqlDataAdapter("select * from aspmuser where username = '" + txtUser.Text + "' and password = '" + txtPass.Text + "' and usertype='" + DropDownList1.SelectedItem.ToString() + "'", con); DataTable dt = new DataTable(); sda.Fill(dt); if (dt.Rows.Count > 0) { Response.Write("<script>alert('you are logined as " + dt.Rows[0][2] + "')</script>"); if (DropDownList1.SelectedIndex == 0) { Server.Transfer("admin.aspx"); }else if (DropDownList1.SelectedIndex == 1) { Server.Transfer("user.aspx"); } } else { Response.Write("error in your input"); }
NOTE: Change this code in your context (because your connection string (ata Source=RAN-PC;Initial Catalog=aspmuser;Integrated Security=True) is different than mine)
Run your project by click the green triangular button in any browser, fill the form and select user type and then click login and then you will go to the respective page if you enter correct username and password of each user type. 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
[/sociallocker]
if you have any confusion, watch the following video: