Wednesday, 4 February 2015

Simple Login Form example in ASP.Net - Dotnetprograms

Hai friends now i am going to explain about Login page by checking availability in database or not.If the user enter the username and password it check in the database if it is valid it redirect to ("HomePage.aspx") otherwise it dispaly the message as "Invalid User Name or Password".

Step 1: Login Page which is give below:



 

To implement this one first design table like this:

 

 




Step :2  Writing Stored Procedure For Login Control

The COUNT() function returns the number of rows that matches a specified criteria

Stored Procedure for Login Form:-
create procedure splogin
@spemailid varchar(30),@sppassword varchar(30)
as
Begin
Select COUNT(*)from student1 where email=@spemailid and password=@sppassword
End



Step 3: Designing  Login Page using asp.net and html controls


<div align="center">
            <fieldset style="width: 350px">
                <legend>Login Page   </legend>
                &nbsp;
   


            <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
                <br />
                <br />
                <table>

                    <tr>
                        <td><b>Enter User Name</b>
                        </td>
                        <td>:
                        </td>
                        <td>
                            <asp:TextBox ID="TextBox1" runat="server" BackColor="#FF5050"></asp:TextBox></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td><b>Enter password</b>
                        </td>
                        <td>:
                        </td>
                        <td>
                            <asp:TextBox ID="TextBox2" runat="server" TextMode="Password"
                                BackColor="#FF5050"></asp:TextBox></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td></td>
                    </tr>

                    <tr>
                        <td></td>
                        <td>
                            <br />
                        </td>
                    </tr>

                    <tr>
                        <td></td>
                        <td>
                            <br />
                        </td>

                        <td>
                            <asp:Button ID="Button1" runat="server" Text="Submit"  OnClick="Button1_Click1"/>&nbsp;&nbsp;&nbsp;

<asp:Button ID="Button2"  runat="server" Text="Cancel" />
                        </td>


                    </tr>
                </table>
                <h3>Check Username and Password availability in database</h3>

            </fieldset>
        </div>


 Step :4  Code For Login Page.aspx.cs page
 
protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connection);
            SqlCommand cmd = new SqlCommand("splogin", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@spemailid", TextBox1.Text);
            cmd.Parameters.AddWithValue("@sppassword", TextBox2.Text);


            con.Open();

            int usercount = (Int32)cmd.ExecuteScalar();// for taking single value

            if (usercount == 1)  // comparing users from table
            {
                Response.Redirect("HomePage.aspx");  //for sucsseful login
            }
            else
            {
                con.Close();
                Label1.Text = "Invalid User Name or Password";  //for invalid login
            }
        }