Asp.net webform and ADO.Net Simple Login and Registration

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="CompanyPage.Login" %>

<!DOCTYPE html>


///Login Page///

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

    <title></title>
        <style type="text/css">
         .center 
         {
    margin-left:400px; 
    margin-right:auto;
    margin-top:150px;

          }
            </style>
   </head>
  <body style="background-color:#E6E6FA">
   <form id="form1" runat="server">
        
 
   <table class="center">

        <tr>
            <td colspan="2"><h3 align="center">Login Page</h3></td>
        </tr>
         <tr>
            <td colspan="2"><asp:Label ID="lblResult" runat="server" Text=""></asp:Label></td>
        </tr>
        
         <tr>
            <td>Email Id :</td>
            <td><asp:TextBox ID="txtLogin" runat="server"></asp:TextBox></td>
                
        </tr>
         <tr>
            <td>Password :</td>
            <td><asp:TextBox ID="txtPassword" runat="server" ></asp:TextBox></td>
        </tr>
         <tr>
            <td><asp:Button ID="btnSubmit" runat="server" Text="Submit" 
                    onclick="btnSubmit_Click" /></td>
            <td><asp:Button ID="btnReset" runat="server" Text="Reset" /></td>
        </tr>
         <tr>
            <td>
                <asp:HyperLink ID="hlRegistration" runat="server" NavigateUrl="RegistrationDetail.aspx">New                  Registration</asp:HyperLink>
             </td>
            <td>
                <asp:HyperLink ID="hlForgotPassword" runat="server" 
                    NavigateUrl="~/ForgotPassword.aspx">Forgot Password</asp:HyperLink>
             </td>
        </tr>
    </table>
       </form>
        </body>
    </html>




namespace CompanyPage
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
             SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings                                       ["LoginConnectionString"].ConnectionString);
              conn.Open();
                string checkPasswordQuery = "select password from login where password='" + txtPassword.Text + "'";
                SqlCommand passComm = new SqlCommand(checkPasswordQuery, conn);
                string password = passComm.ExecuteScalar().ToString();
                if (password == txtPassword.Text)
                {
                    Session["New"] = txtPassword.Text;
                    Response.Write("Password is correct");
                    Response.Redirect("Main.aspx");
                    Server.Transfer("Main.aspx");
                }
                else
                {
                    Response.Write("Password is not correct");
                }
            }
        }

       
    }



 ///Registration Page///

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RegistrationDetail.aspx.cs" Inherits="CompanyPage.Registration_Detail" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
         .center {
    margin-left:400px; 
    margin-right:auto;
    margin-top:100px;
    border-width:2px;
  }
</style>
</head>
<body style="background-color:#EEE8AA">
    <form id="form1" runat="server">
     <table class ="center">
            <tr>
                <td class="style1" colspan="2">
                    <h1 style="color:blue;"><strong>Registration Details</strong></h1></td>
            </tr>
             <tr>
                <td class="style1" colspan="2">
                    </td>
            </tr>
         
            <tr>
                <td class="style2">
                    Name :</td>
                <td class="style3">
                    <asp:TextBox ID="txtname" runat="server" Text=""></asp:TextBox>
                            </td>
            </tr>
            <tr>
                <td>
                    DOB :</td>
                <td class="style4">
                    <asp:TextBox ID="txtDOB" runat="server" Text=""></asp:TextBox>
                </td>
            </tr>
           
            <tr>
                <td>
                    Gender :</td>
                <td class="style4">
                    <asp:TextBox ID="txtGender" runat="server" Text=""></asp:TextBox>
                </td>
            </tr>
                   <tr>
                <td>
                    Email ID :</td>
                <td class="style4">
                    <asp:TextBox ID="txtEmail" runat="server" Text=""></asp:TextBox>
                   
                </td>
            </tr>
            <tr>
                <td>
                    Password :</td>
                <td>
                    <asp:TextBox ID="txtPassword" runat="server" Text=""></asp:TextBox>
                                    </td>
            </tr>
         <tr>
           <td><asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
         </td>
            </tr>
        </table>
    </form>
</body>
</html>



namespace CompanyPage
{
    public partial class Registration_Detail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings                                ["RegistrationConnectionString"].ConnectionString);
                conn.Open();
                string checkuser = "select count (*) from UserData where Email='" + txtEmail.Text + "'";
                SqlCommand com = new SqlCommand(checkuser, conn);
                int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
                if (temp == 1)
                {
                    Response.Write("User Exists");
                }
                conn.Close();
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings                 ["RegistrationConnectionString"].ConnectionString);
                conn.Open();
                string insertQuery = "insert into UserData(Name,Email,Password,Gender) values                                 (@name,@email,@password,@gender)";
                SqlCommand com = new SqlCommand(insertQuery, conn);
                com.Parameters.AddWithValue("@name", txtname.Text);
                com.Parameters.AddWithValue("@email", txtEmail.Text);
                com.Parameters.AddWithValue("@password", txtPassword.Text);
                com.Parameters.AddWithValue("@gender", txtGender.Text);

                com.ExecuteNonQuery();
                Response.Write("Registration Successful");
                Response.Redirect("Employee.aspx");
                Server.Transfer("Employee.aspx");
                conn.Close();

            }
            catch (Exception ex)
            {
                Response.Write("Error:" +ex.ToString());
            }
               
             }
         }
      }

Leave a Reply

Your email address will not be published. Required fields are marked *