Tuesday, March 6, 2012

Manual Login using Login Control and ADO

Here is the CS code for Default1.aspx


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

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

    }
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        //connect
        SqlConnection connect = new SqlConnection
            ("Data Source=localhost;initial catalog=Automart;integrated security=true");

        SqlCommand cmd = new SqlCommand
            ("Select email, Customerpassword, PersonKey "
+ "From Customer.RegisteredCustomer Where email=@email "
+ " and CustomerPassword=@password", connect);
        cmd.Parameters.AddWithValue("@email", Login1.UserName.ToString());
        cmd.Parameters.AddWithValue("@password", Login1.Password.ToString());

        DataSet ds = new DataSet();
        SqlDataReader reader;

        connect.Open();
        reader = cmd.ExecuteReader();
        ds.Load(reader,LoadOption.OverwriteChanges,"LoginTable");
        reader.Dispose();
        connect.Close();

        foreach (DataRow row in ds.Tables[0].Rows)
        {
            if (row["email"].ToString().Equals(Login1.UserName.ToString()) 
&& row["CustomerPassword"].ToString().Equals(Login1.Password.ToString()))
            {

                e.Authenticated = true;
                Session["ID"] = row["PersonKey"];
                Response.Redirect("Default2.aspx");

            }
            else
            {
                e.Authenticated = false;
            }
        }




    }
}


Here is code for Default2.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection connect = new SqlConnection
            ("Data Source=Localhost;initial catalog=AutoMart;integrated security=true");

        StringBuilder sb = new StringBuilder();
        sb.Append("Select LicenseNumber, VehicleMake, VehicleYear, LocationName, ");
        sb.Append("ServiceDate, ServiceTime, ServiceName, ServicePrice, DiscountPercent ");
        sb.Append("From  Customer.Vehicle v ");
        sb.Append("Inner Join Employee.VehicleService vs ");
        sb.Append("on v.VehicleID=vs.VehicleID ");
        sb.Append("Inner Join Employee.VehicleServiceDetail vsd ");
        sb.Append("on vs.VehicleServiceID=vsd.VehicleServiceID ");
        sb.Append("Inner Join Customer.Location l ");
        sb.Append("on l.LocationID=vs.LocationID ");
        sb.Append("Inner Join Customer.AutoService a ");
        sb.Append("on a.AutoServiceID=vsd.AutoserviceID ");
        sb.Append("Where v.PersonKey=@PersonKey");

        SqlCommand cmd = new SqlCommand(sb.ToString(), connect);
        cmd.Parameters.AddWithValue("@PersonKey", (int)Session["ID"]);

        SqlDataReader reader=null;
        DataSet ds = new DataSet();

        connect.Open();
        reader = cmd.ExecuteReader();
        ds.Load(reader, LoadOption.OverwriteChanges, "Services");
        reader.Dispose();
        connect.Close();

        GridView1.DataSource = ds.Tables["Services"];
        GridView1.DataBind();

    }
}

No comments:

Post a Comment