using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

using Utilities;

using System.Data;                                          //needed for ADO.NET classes

using System.Data.SqlClient;                                //needed for the Sql data provider

using System.Runtime.Serialization.Formatters.Binary;       //needed for BinaryFormatter

using System.IO;                                            //needed for the MemoryStream

 

namespace Serialization

{

    public partial class SerializationExample1 : System.Web.UI.Page

    {

        DBConnect objDB = new DBConnect();

        SqlCommand objCommand = new SqlCommand();

 

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void btnFindByID_Click(object sender, EventArgs e)

        {

            int id;

 

            if (int.TryParse(txtAccountInfo.Text, out id))

            {

                // Set the SQLCommand object's properties for executing a Stored Procedure.

                objCommand.CommandType = CommandType.StoredProcedure;

                objCommand.CommandText = "GetAccountByID";      // identify the Stire Procedure to Execute

 

                // Add an input parameter to pass to the Stored Procedure that will be used

                // for the @theID built-in parameter.

                objCommand.Parameters.AddWithValue("@theID", id);

 

                // Execute the Stored Procedure using the DBConnect object and the SQLCommand object

                gvAccounts.DataSource = objDB.GetDataSetUsingCmdObj(objCommand);

                gvAccounts.DataBind();

                lblDisplay.Text = "";

 

            }

            else

                lblDisplay.Text = "You entered an invalid Account ID!";

 

            lblCardInfo.Text = "";

            ClearCreditCardInfo();

        }

 

        protected void btnFindByName_Click(object sender, EventArgs e)

        {

            // Set the SQLCommand object's properties for executing a Stored Procedure

            objCommand.CommandType = CommandType.StoredProcedure;

            objCommand.CommandText = "GetAccountByName";  // identify the Stored Procedure to Execute

 

            // Add an input parameter to pass to the Stored Procedure that will be used

            // for the @theName built-in parameter.           

            objCommand.Parameters.AddWithValue("@theName", txtAccountInfo.Text);

 

            // Execute stored procedure using DBConnect object and the SQLCommand object

            gvAccounts.DataSource = objDB.GetDataSetUsingCmdObj(objCommand);

            gvAccounts.DataBind();

 

            lblDisplay.Text = "";

            lblCardInfo.Text = "";

 

            ClearCreditCardInfo();

        }

 

        protected void btnStoreCard_Click(object sender, EventArgs e)

        {

            int id;

 

            if (IsNumeric(txtCreditCardNumber.Text) && int.TryParse(txtAccountInfo.Text, out id))

            {

                // Create a CreditCard object to store in database

                CreditCard objCreditCard = new CreditCard();

                objCreditCard.CardNumber = txtCreditCardNumber.Text;

                objCreditCard.CardType = ddlCardType.SelectedItem.Value;

                objCreditCard.ExpirationMonth = int.Parse(ddlMonth.SelectedItem.Value);

                objCreditCard.ExpirationYear = int.Parse(ddlYear.SelectedItem.Value);

 

                // Serialize the CreditCard object

                BinaryFormatter serializer = new BinaryFormatter();

                MemoryStream memStream = new MemoryStream();

                Byte[] byteArray;

                serializer.Serialize(memStream, objCreditCard);

                byteArray = memStream.ToArray();

 

                // Update the account to store the serialized object (binary data) in the database

                objCommand.CommandType = CommandType.StoredProcedure;

                objCommand.CommandText = "StoreCreditCard";

 

                objCommand.Parameters.AddWithValue("@theID", id);

                objCommand.Parameters.AddWithValue("@theCreditCard", byteArray);

 

                int retVal = objDB.DoUpdateUsingCmdObj(objCommand);

 

                // Check to see whether the update was successful

                if (retVal > 0)

                    lblDisplay.Text = "The credit card was successfully stored for this account.";

                else

                    lblDisplay.Text = "A problem occured in storing the credit card.";

           

            }

            else

            {

                lblDisplay.Text = "You must enter a valid AccountID and Credit Card Number!";

            }

        }

 

        protected void btnRetrieveCardInfo_Click(object sender, EventArgs e)

        {

            int id;

           

            if (int.TryParse(txtAccountInfo.Text, out id))

            {        

                String strSQL = "SELECT CreditCard FROM Account WHERE AccountID ='" + txtAccountInfo.Text + "'";

                objDB.GetDataSet(strSQL);

 

                if (objDB.GetField("CreditCard", 0) != System.DBNull.Value)

                {

                    // De-serialize the binary data to reconstruct the CreditCard object retrieved

                    // from the database

                    Byte[] byteArray = (Byte[])objDB.GetField("CreditCard", 0);

 

                    BinaryFormatter deSerializer = new BinaryFormatter();

                    MemoryStream memStream = new MemoryStream(byteArray);

 

                    CreditCard objCreditCard = (CreditCard)deSerializer.Deserialize(memStream);

 

                    lblCardInfo.Text = "The following credit card information was found: </br>" +

                                        "----------------------------------------------- </br>" +

                                        "Card Type: " + objCreditCard.CardType + " </br>" +

                                        "Card #: " + objCreditCard.CardNumber + " </br>" +

                                        "Exp Date: " + objCreditCard.ExpirationMonth + "/" + objCreditCard.ExpirationYear + " </br>" +

                                        "----------------------------------------------- </br>";

 

                    lblDisplay.Text = "";

                }

                else

                {

                    lblCardInfo.Text = "A credit card was never saved for this account.";

                }

            }

            else

            {

                lblDisplay.Text = "You entered an invalid AccountID!";

            }

        }

 

        private void ClearCreditCardInfo()

        {

            ddlCardType.ClearSelection();

            ddlMonth.ClearSelection();

            ddlYear.ClearSelection();

            txtCreditCardNumber.Text = "";

        }

 

        private Boolean IsNumeric(String inputString)

        {

            Boolean isNumber = true;

 

            foreach (Char ch in inputString.ToCharArray())

            {

                isNumber = isNumber && Char.IsDigit(ch);

            }

 

            return isNumber;

        }

    }

}