connectseekers.com

Sunday, 28 December 2014

Asp.Net Login Form With User Role Selection By Dropdownlist In Asp.net Using C#.Net

12/28/2014 - By Pranav Singh 0



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.



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.

 

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top