Wednesday, April 9, 2014

Registration Form Asp.Net using C# in 3tier

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RegistratationRedirect.aspx.cs" Inherits="RegistratationRedirect" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>
    First Name:

    </td>
    <td>
    <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    Last Name:

    </td>
    <td>
    <asp:TextBox ID="txt2" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    UserName:

    </td>
    <td>
    <asp:TextBox ID="txt3" runat="server"></asp:TextBox>
    </td>
    </tr>
    
    <tr>
    <td>
    Password:

    </td>
    <td>
    <asp:TextBox ID="txt4" runat="server"></asp:TextBox>
    </td>
    </tr>
    
    <tr>
    <td>
    Role:

    </td>
    <td>
    <asp:DropDownList ID="drp" runat="server"></asp:DropDownList>
    </td>
    </tr>
    
    <tr>
    <td>
    Email Id :
    </td>
    <td>
    <asp:TextBox ID="email" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    <asp:Label ID="lblMessage" runat="server"></asp:Label>
    </td>
    <td>
    <asp:TextBox ID="forget" runat="server" ></asp:TextBox>
    </td>
    </tr>

    <tr>
    <td>
    <asp:Button ID="btnForgetPassword" runat="server" Text="Forget Password" 
            onclick="btnForgetPassword_Click" />
    </td>
    <td>
    <asp:Button ID="btnRegistration" runat="server" Text="Registration" 
            onclick="btnRegistration_Click" />
    </td>
    </tr>


    
    </table>
    
    </div>
    </form>
</body>
</html>

Ado Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for Class1
/// </summary>
public class Class1
{
    string aaa = ConfigurationManager.ConnectionStrings["asdf"].ToString();
public Class1()
{
//
// TODO: Add constructor logic here
//
}
    public DataSet gridbind()
    {
        SqlConnection con = new SqlConnection(aaa);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "sp_state";
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        return ds;




    }
    public DataSet getgrid()
    {
        SqlConnection con = new SqlConnection(aaa);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "sp_grid";
        cmd.CommandType = CommandType.StoredProcedure;
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        return ds;
    }
    public DataSet role()
    {
        SqlConnection con = new SqlConnection(aaa);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "sp_role";
        cmd.CommandType = CommandType.StoredProcedure;
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        return ds;
    }
    public void registration(string firstname,string lastname,string username,string password,string role,string emailid)
    {
        SqlConnection con = new SqlConnection(aaa);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "sp_reg";
        
        cmd.Parameters.AddWithValue("@firstname", firstname);
        cmd.Parameters.AddWithValue("@lastname", lastname);
        cmd.Parameters.AddWithValue("@username", username);
        cmd.Parameters.AddWithValue("@password", password);
        cmd.Parameters.AddWithValue("@role", role);
        cmd.Parameters.AddWithValue("@emailid", emailid);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

    }
    public int login(string username,string password)
    {
        SqlConnection con = new SqlConnection(aaa);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "sp_login";
        cmd.CommandType = CommandType.StoredProcedure;
        con.Open();
        cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = username;
        cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = password;
        int i=Convert.ToInt32(cmd.ExecuteScalar());
        con.Close();
        return i;

    }



}

Aspx.cs code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Net.Mail;
using System.Net;

public partial class RegistratationRedirect : System.Web.UI.Page
{
    Class1 obj = new Class1();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //obj.role();
            drp.DataSource = obj.role();
            drp.DataValueField = "id";
            drp.DataTextField = "role";
            drp.DataBind();
        }
    }
    protected void btnRegistration_Click(object sender, EventArgs e)
    {
        obj.registration(txt1.Text, txt2.Text, txt3.Text, txt4.Text, drp.SelectedValue, email.Text);
        Response.Redirect("Login.aspx?User=" + txt3.Text);

    }
    protected void btnForgetPassword_Click(object sender, EventArgs e)
    {

        string con = ConfigurationManager.ConnectionStrings["asdf"].ConnectionString;

        string strSelect = ("SELECT username,password,emailid FROM registration WHERE emailid='" + forget.Text + "'");
        SqlConnection connection = new SqlConnection(con);
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandType = CommandType.Text;
        command.CommandText = strSelect;
        SqlParameter email = new SqlParameter("@emailid", SqlDbType.VarChar, 50);
        email.Value = forget.Text.Trim().ToString();
        command.Parameters.Add(email);
        //Create Dataset to store results and DataAdapter to fill Dataset 
        DataSet dsPwd = new DataSet();
        SqlDataAdapter dAdapter = new SqlDataAdapter(command);
        connection.Open();
        dAdapter.Fill(dsPwd);
        connection.Close();
        if (dsPwd.Tables[0].Rows.Count > 0)
        {
            MailMessage loginInfo = new MailMessage();
            loginInfo.To.Add(forget.Text.ToString());
            loginInfo.From = new MailAddress("choulla.naresh@gmail.com");
            loginInfo.Subject = "Forgot Password Information";
            loginInfo.Body = "Username: " + dsPwd.Tables[0].Rows[0]["username"] + "<br><br>Password: " + dsPwd.Tables[0].Rows[0]["password"] + "<br><br>";
            loginInfo.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential("choulla.naresh@gmail.com", "9177499466");
            smtp.EnableSsl = true;
            //smtp.Credentials = new System.Net.NetworkCredential("srinuvvd@gmail.com", "9032479683");
            smtp.Send(loginInfo);
            lblMessage.Text = "Password is sent to you email id";//,you can now "<a href="Login.aspx">Login</a>";
        }
        else
        {
            lblMessage.Text = "Email Address Not Registered";
        }
    }
    
}