Thursday, February 21, 2013

In Class ADO Example

Default.aspx

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td>User Name</td>
            <td>
                <asp:TextBox ID="txtUser" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td>Password</td>
            <td>
                <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lblMsg" runat="server" ></asp:Label></td>
            <td>
                <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" /></td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>


Default.aspx.cs

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

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

    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        DonorLoginClass dl = new DonorLoginClass();
        int person = dl.Login(txtUser.Text, txtPassword.Text);
        if (person != 0)
        {
            Session["person"] = person;
            Response.Redirect("Default2.aspx");
        }
        else
        {
            lblMsg.Text = "invalid login";
        }
    }
}

Default2.aspx

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblName" runat="server" Text="Label"></asp:Label>
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </div>
    </form>
</body>
</html>


DonorLoginClass.cs

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

/// 
/// Summary description for DonorLoginClass
/// 
public class DonorLoginClass
{
    private SqlConnection connect;

 public DonorLoginClass()
 {
        string connectionString = @"Data Source=localhost;"
            + "initial catalog=CommunityAssist;"
            + "user=DonorsLogin;password=P@ssw0rd1";
        connect = new SqlConnection(connectionString);
 }

    public int Login(string userName, string passWord)
    {
        int pKey = 0;
        string sql = "Select PersonKey, LastName, DonorPassword From DonorLogin";
            //+ "Where LastName=@LastName and DonorPassword=@password";
        SqlCommand cmd = new SqlCommand(sql, connect);
       // cmd.Parameters.AddWithValue("@LastName", userName);

        SqlDataReader reader = null;
        connect.Open();
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            if (reader["LastName"].ToString().Equals(userName)
                && reader["DonorPassword"].ToString().Equals(passWord))
            {
                pKey = int.Parse(reader["PersonKey"].ToString());
                break;
            }
        }
        reader.Close();
        connect.Close();
        return pKey;
    }
}

GetDonor.cs

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

/// 
/// Summary description for GetDonor
/// 
public class GetDonor
{
    SqlConnection connect;
    int personKey;
 public GetDonor(int pKey)
 {
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
        builder.DataSource = "localhost";
        builder.InitialCatalog = "CommunityAssist";
        builder.UserID = "RegisteredDonorsLogin";
        builder.Password = "P@ssw0rd1";
        connect = new SqlConnection(builder.ToString());
        personKey = pKey;
 }

    public string GetDonorName()
    {
        string info = null;

        string sql = "Select LastName, FirstName From Person " +
                        "Where PersonKey=@PersonKey";
        SqlCommand cmd = new SqlCommand(sql, connect);
        cmd.Parameters.AddWithValue("@PersonKey", personKey);

        SqlDataReader reader = null;
        connect.Open();
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            info = reader["FirstName"].ToString() + "  " + reader["LastName"].ToString();
        }
        reader.Close();
        connect.Close();

        return info;
    }

    public DataSet GetDonations()
    {
        DataSet ds = new DataSet();
        string sql = "Select DonationDate, DonationAmount From Donation " +
            "Where PersonKey=@PersonKey";
        SqlCommand cmd = new SqlCommand(sql, connect);
        cmd.Parameters.AddWithValue("@PersonKey", personKey);
        SqlDataReader reader = null;

        connect.Open();
        reader = cmd.ExecuteReader();
        ds.Load(reader, LoadOption.OverwriteChanges, "Donation");
        reader.Close();
        connect.Close();
        return ds;
    }

    
}

No comments:

Post a Comment