connectseekers.com

Thursday, 9 March 2017

Validate Username First & Then Password Like Gmail And Outlook Login In Asp.Net Using C#

3/09/2017 - By Pranav Singh 0

This article will show you validate username first & then password like gmail and outlook login in asp.net using c#. So in this login page first it will ask for user id if user enter correct user id after that it will ask for password.

So for this article first we will create a login table.




Now we will create a new asp.net application and add the below code.

<form id="form1" runat="server">
        <div>
            <table width="100%" border="1">
                <tr id="trUserId" runat="server">
                    <td style="text-align: right;">User Id</td>
                    <td style="text-align: left;">
                        <asp:TextBox ID="txtUSerId" runat="server"></asp:TextBox>
                        <asp:Label ID="lblUserId" runat="server" Style="color: #4800ff" Text="" Visible="false" Font-Bold="true"></asp:Label>
                    </td>
                </tr>
                <tr id="trPassword" runat="server">
                    <td style="text-align: right;">Password</td>
                    <td style="text-align: left;">
                        <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
                </tr>
                <tr>
                    <td style="text-align: center;" colspan="2">
                        <asp:Button ID="btnValidateUserId" runat="server" Text="Submit" OnClick="btnValidateUserId_Click" />
                        <asp:Button ID="btnLogin" runat="server" Text="Login" Visible="false" OnClick="btnLogin_Click" />
                        <br />
                        <asp:Label ID="lblmessage" runat="server" Style="color: #FF3300" Text=""></asp:Label></td>
                </tr>
            </table>


        </div>
    </form>

In above code I have taken tow textbox one for user id and other for password. After that I have added a label in which we will display the user id if user entered the correct user id. After displaying the password we will show password.

In above code I have provided id to tr’s and make it runat=”server”. To make it visible at server end.
In above code I have also taken two button one for login and other for validating the user id. Now we will check the code. First we will check the page load code.

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                trPassword.Visible = false;
            }
        }

In above code I have make the tr of password field visible false. Now lets check the user validation code.

    protected void btnValidateUserId_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = new DataSet();

                SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Demo;Integrated Security=True");
                string query = "select * from UserLoginDetail Where [UserId]='" + txtUSerId.Text + "' ;";
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(query, con);
                da.Fill(ds);
                con.Close();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    lblUserId.Text = txtUSerId.Text;
                    lblUserId.Visible = true;
                    txtUSerId.Visible = false;
                    trPassword.Visible = true;
                    btnValidateUserId.Visible = false;
                    btnLogin.Visible = true;
                }
                else
                {
                    lblmessage.Text = "Please enter a valid user Id.";
                }
            }
            catch (Exception ex)
            {
                lblmessage.Text = ex.Message.ToString();
            }
        }

In above code I have validated the user which user have entered. If user enter correct user id then we will display the password and login button. Now we will check the login code. On correct user id I have make the textbox of userid  visible false and assign the entered user id to label control and make the label visible true.

   protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = new DataSet();

                SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Demo;Integrated Security=True");
                string query = "select * from UserLoginDetail Where [UserId]='" + lblUserId.Text + "' and [Password]='" + txtPassword.Text + "';";
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(query, con);
                da.Fill(ds);
                con.Close();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    Session["userlogin"] = lblUserId.Text;
                    Response.Redirect("SuccessPage.aspx");
                }
                else
                {

                    lblmessage.Text = "Please enter a valid password for user.";
                }
            }
            catch (Exception ex)
            {
                lblmessage.Text = ex.Message.ToString();
            }
        }

In above code I have validated the user id and password. After successful login user will be redirected to success page. Now we have done run the application and 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 aspdotnet-pools.com Design By:
back to top