using System;
using System.Collections.Generic;
using System.Collections;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Text;
using System.Net.Mail;
using System.Net.Sockets;
using System.Xml;
using System.Xml.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;

public partial class MobilePortal_MobileAppLogin : System.Web.UI.Page
{
    string agent_Id;    
    string headXML = "<?xml version=\"1.0\" encoding='UTF-8' standalone='no' ?><response-Statement>";
    string footerXML = "</response-Statement>";
    protected void Page_Load(object sender, EventArgs e)
    {
        DataRow dr;
        agent_Id = Request.QueryString["agent_Id"];
        //agent_Id = "1000000044";
        if (agent_Id != null)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlParameter[] get_user_last_five_transaction = new SqlParameter[]
                {
                    new SqlParameter("@agentid",agent_Id)
                };
                ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_showLastFiveTransactionMobilePortal", get_user_last_five_transaction);
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    ds.Tables[0].Merge(ds.Tables[i]);
                }
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataTable dt = ds.Tables["Table"].Copy();
                    DataView dv = new DataView(dt);
                    dv.Sort = "TRANSACTION_DATETIME ASC";
                    string dataXML = string.Empty;
                    int count = 0;
                    foreach (DataRow row in ds.Tables[0].Rows) // Loop over the rows.
                    {
                        dataXML += "<transaction_detail><Id_No>" + count + "</Id_No><Tran_Id>" + row["TRANSACTIONID"].ToString() +
                            "</Tran_Id><Service>" + row["SERVICE_NAME"] + "</Service><Optr>" + row["OperatorName"] + "</Optr><Mob_Num>" + row["MobileDTHNumber"] +
                            "</Mob_Num><Action>" + row["Action_on_amount"] + "</Action> <Status>" + row["Status"] + "</Status><Amt>" + row["NET_AMOUNT"] +
                            "</Amt><Net_Amt>" + row["REQUEST_AMOUNT"] + "</Net_Amt><DOT>" + row["Txn_Date"] +
                            "</DOT><TOT>" + row["Txn_Time"] + "</TOT><Open_Bal>" + row["AMOUNT_BEFORE_DUE_DATE"] + "</Open_Bal><Close_Bal>" + row["AMOUNT_AFTER_DEU_DATE"] +
                            "</Close_Bal></transaction_detail>";
                        count++;
                    }
                    headXML += dataXML + footerXML;
                    WriteXML(headXML);
                }
                else
                {
                    string dataXML = string.Empty;
                    dataXML += "<transaction_detail><Id_No>" + "0" +"</Id_No><Tran_Id>" + "0" +
                            "</Tran_Id><Service>" + "Null" + "</Service><Optr>" + "Null" + "</Optr><Mob_Num>" + "0" +
                            "</Mob_Num><Action>" + "D/C" + "</Action> <Status>" + "S" + "</Status><Amt>" + "0" +
                            "</Amt><Net_Amt>" + "0" + "</Net_Amt><DOT>" + "Null" +
                            "</DOT><TOT>0</TOT><Open_Bal>" + "0" + "</Open_Bal><Close_Bal>" + "0"+
                            "</Close_Bal></transaction_detail>";
                    headXML += dataXML + footerXML;
                    WriteXML(headXML);
                }               
            }
            catch (Exception ex)
            {
                string st = ex.Message;
            }
        }
        else
        {
            headXML += "<trans-response-code>1</trans-response-code><trans-response-message>Invalid request!</trans-response-message>" + footerXML;
            WriteXML(headXML);
        }
    }
    private void WriteXML(string str)
    {
        Response.ClearHeaders();
        Response.AddHeader("content-type", "text/xml");
        Response.Write(str);
        Response.End();
    }
    private void BindXML(string a, string b)
    {
        string str = "<?xml version=\"1.0\" encoding='UTF-8' standalone='no' ?><response>";
        //str = str + "<set label='" + a + "' value='" + b + "' />";
        str = str + "<transaction_detail><Id_No>4198426</Id_No><Tran_Id>" + a + "</Tran_Id><Service>Mobile Recharge</Service><Optr>RELIANCEGSM</Optr><Mob_Num>8899097837</Mob_Num><Action>Debit</Action> <Status>Success</Status><Amt>9.72</Amt><Net_Amt>10</Net_Amt><DOT>01/04/2012</DOT><TOT>13:12:00</TOT><Open_Bal>246.5764</Open_Bal><Close_Bal>236.8564</Close_Bal></transaction_detail>";
        str = str + "</chart>";
        DataSet ds = new DataSet();
        ds.ReadXml(new StringReader(str));
        ds.WriteXml(str);
    }
}