This article will show you how you can create a login form login
form with user role selection by dropdownlist in asp.net using c#.net.
Some of my previous articles are as follows: Populate
Data in GridView on DropdownList Selected Role in Asp.net Using C#.net, Random
Character and String Generation Using C#.Net, Windows Application and Linq ,
Strong
Random String Password Generation Using C#.Net, Display
Calendar Control Selected Date Into Javascript Alert Message In Asp.Net, Display
Calendar Control Selected Date Into Javascript Alert Message In Asp.Net, Use
Of Css To Show Image However Effect In Asp.Net, Display
ModalPopupExtender on Page Load In Asp.Net Using AjaxControlToolkit, C#.Net,
Dragable
ModalPopupExtender In Asp.Net Using AjaxControlToolkit, Add
Select text in Dropdownlist in Asp.Net OR Adding Default Select Option in the
DropDownList Using C#.Net in Asp.Net, Bind
DropdownList Using Sql Server and Datatable in Asp.Net and C#.
So for this article first we will create a new sql table for
managing user detail.
After creating table we will create a new asp.net
application. After creating application we will add the below code into the
page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Asp.Net
Login Form With User Role Selection By Dropdownlist In Asp.net Using
C#.Net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%" cellpadding="3" cellspacing="4">
<tr>
<td align="right">
User Id :
</td>
<td align="left">
<asp:TextBox ID="txtuserid" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
Password :
</td>
<td align="left">
<asp:TextBox ID="txtpassword" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
Role :
</td>
<td align="left">
<asp:DropDownList ID="ddltype" runat="server">
<asp:ListItem Value="1">Admin</asp:ListItem>
<asp:ListItem Value="2">User</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click"
/>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Label ID="lblmessage" runat="server" style="color: #FF3300; font-weight: 700"
Text=""></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
|
Now in you web.config we will make some configuration
setting for connection string.
<connectionStrings>
<add name="con" connectionString="Data Source=DELL-PC;Initial Catalog=Demo;Integrated
Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
|
After this we will add the code for validating user on the
bases of the user type.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication1
{
public partial class WebForm2 : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
}
protected
void btnSubmit_Click(object
sender, EventArgs e)
{
SqlConnection
con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString());
try
{
DataTable
objdt = new DataTable();
string
query = "select * from UserLogin where
UserId='" + txtuserid.Text + "'
and Password='" + txtpassword.Text + "'
and UserType='" + ddltype.SelectedValue + "';";
SqlDataAdapter
da = new SqlDataAdapter(query,
con);
con.Open();
da.Fill(objdt);
con.Close();
if
(objdt.Rows.Count > 0)
{
lblmessage.Text = "Login successfull.";
}
else
{
lblmessage.Text = "Invalid userid or password ot role type.";
}
}
catch
{
con.Close();
}
}
}
}
|
Please check the button click event code. In this code I have
prepare the query for validating userid and password as per the role of the user.
Now if we are getting any record on that case user has provided correct userid
and password as per the selected role.
Now we have done run the application to check the output.
0 comments:
Please let me know your view