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 MobileSMS_Default : System.Web.UI.Page
{
    WBMobile_Noverification _ObjWBMNverify = new WBMobile_Noverification();
    private string _loginID;
    string txn, TxnID, WLID;
    DataSet ds = new DataSet();
    static double ReqBalance, WLbalance;
    float balance, cutoffBalance;
    int result;
    decimal  balanceR;
    decimal requestbalance;
    string OperatorId = string.Empty;
    DataSet  dsag=new DataSet();
    DataSet dsa = new DataSet();
    string agent_Id, Agentmobile_no, SOperator, refrenceno, AgMobileNo,ServiceCharge;
    int Availablebalance;
    CommonFunction objcommonfun = new CommonFunction();
    static string Chkno_RESULT = string.Empty;
    static string ret = string.Empty;
    string ErrorMessage = string.Empty;
    string ActualMPIN,date_time,WLOpStatus;
    int LoginCount;
    string stringquery, msg, BalanceAfterTransaction, flagREFREV;
    string IPAddress = HttpContext.Current.Request.UserHostAddress.ToString(); 
    protected void Page_Load(object sender, EventArgs e)
    {
        date_time = DateTime.Now.ToString();
        stringquery = Convert.ToString(Request.QueryString["message"].ToString());
        //http://venusrecharge.com/MobileSMS/Sms.aspx?from=%from&message=%message&ts=%ts
        //stringquery = "919953025142/VUS BAL/UP WEST TATA GSM 17-04-2015 12:33:30 PM";
        string[] stringquery11 = new string[10];
        //stringquery11 = stringquery.Split('/');
        //Agentmobile_no = Convert.ToString(stringquery11[0]);
        Agentmobile_no = Convert.ToString(Request.QueryString["from"].ToString());
        AgMobileNo = Agentmobile_no.Substring(Agentmobile_no.Length - Math.Min(10, Agentmobile_no.Length));
        msg = stringquery;
        msg = msg.Replace('+', ' ');
        //msg = Regex.Replace(msg, @"\s+", " ").Trim();
        string[] sms = msg.Split(' ');
        //int k = sms.Length;
        if (sms.Length > 3)
        {
            ViewState["servicetype"] = Convert.ToString(sms.GetValue(1));
            ViewState["opertaortype"] = Convert.ToString(sms.GetValue(2));
        }
        else
        {
            ViewState["servicetype"] = Convert.ToString(sms.GetValue(1));            
        }
        if (ViewState["servicetype"].Equals("TRF") || ViewState["servicetype"].Equals("trf") || ViewState["servicetype"].Equals("Trf") || ViewState["servicetype"].Equals("REV") || ViewState["servicetype"].Equals("rev") || ViewState["servicetype"].Equals("Rev"))
        {
            refrenceno = TID_GEN();
        }
        else
        {
            if (sms.Length > 3)
            {
                GetOperator();
                GetVendor();
                if (ViewState["ServiceProvider"].Equals("IWTApi"))
                {
                    refrenceno = TID_GEN1();
                }
                else
                {
                    refrenceno = TID_GEN();
                }
            }
            else
            {
                refrenceno = TID_GEN();
            }
        }
        try
        {
            SqlParameter[] url = new SqlParameter[]
            {
                new SqlParameter("@RefNumber",refrenceno),
                new SqlParameter("@MobileNo",AgMobileNo),
                new SqlParameter("@url",stringquery),
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_geturl", url);
        }
        catch (Exception ex)
        {
            throw (ex);
        }
        if (ViewState["servicetype"].Equals("MPIN") || ViewState["servicetype"].Equals("Mpin") || ViewState["servicetype"].Equals("mpin"))
        {
            SetMpin(msg, Agentmobile_no, refrenceno);
           // Response.Redirect("SetMpin.aspx?Message=" + msg + "&MobNo=" + Agentmobile_no + "&Refno=" + refrenceno + "");
        }
        else if (ViewState["servicetype"].Equals("BAL") || ViewState["servicetype"].Equals("Bal") || ViewState["servicetype"].Equals("bal"))
        {
            Response.Redirect("CheckBalance.aspx?Message=" + msg + "&MobNo=" + Agentmobile_no + "&Refno=" + refrenceno + "&servicetype=" + ViewState["servicetype"].ToString() + "");
        }
        else if (ViewState["servicetype"].Equals("TRF") || ViewState["servicetype"].Equals("trf") || ViewState["servicetype"].Equals("Trf") || ViewState["servicetype"].Equals("REV") || ViewState["servicetype"].Equals("rev") || ViewState["servicetype"].Equals("Rev"))
        {
            AmountTransfer(msg, Agentmobile_no, refrenceno);
           // Response.Redirect("AmountTransfer.aspx?Message=" + msg + "&MobNo=" + Agentmobile_no + "&Refno=" + refrenceno + "");
        }
        else
        {
            string userstatus = objcommonfun.GetUserStatus(AgMobileNo);
            if (userstatus.Equals("1"))
            {
                try
                {                    
                    ViewState["mobilenumber"] = Convert.ToString(sms.GetValue(3));
                    ViewState["amount"] = Convert.ToString(sms.GetValue(4));
                    ViewState["mpin"] = Convert.ToString(sms.GetValue(5));
                    //if (Convert.ToInt32(ViewState["amount"].ToString()) > 49)
                    //{
                        ServiceCharge = "0.00";
                    //}
                    //else
                    //{
                    //    ServiceCharge = "0.00";
                    //}
                }
                catch (Exception ex)
                {
                    objcommonfun.SendSMS(AgMobileNo, "Wrong SMS format. Kindly use correct SMS format", refrenceno, AgMobileNo, stringquery);
                }
                if (ViewState["opertaortype"] != null && ViewState["mpin"] != null && ViewState["mobilenumber"] != null && ViewState["amount"] != null)
                {
                    Regex re = new Regex(@"\d+");
                    Match m = re.Match(ViewState["mpin"].ToString());
                    if (ViewState["opertaortype"].ToString().Length.Equals(2))
                    {
                        if (ViewState["servicetype"].ToString().Equals("D") || ViewState["servicetype"].ToString().Equals("M"))
                        {
                            if (ViewState["servicetype"].ToString().Equals("D") || ViewState["servicetype"].ToString().Equals("M") && ViewState["mobilenumber"].ToString().Length > 5 && ViewState["mobilenumber"].ToString().Length < 15)
                            {
                                if (m.Length.Equals(4))
                                {
                                    if (m.Success)
                                    {
                                        GetMpin();
                                        if (LoginCount <= 2)
                                        {
                                            if (ActualMPIN == Convert.ToString(ViewState["mpin"]))
                                            {
                                                userdetails();
                                                UserCutOffDetails();
                                                if (Availablebalance > cutoffBalance)
                                                {
                                                    if (Availablebalance > Convert.ToInt32(ViewState["amount"].ToString()))
                                                    {
                                                        if (20 <= Convert.ToInt32(ViewState["amount"].ToString()))
                                                        {
                                                            try
                                                            {
                                                                if (SOperator != null && ViewState["OperatorType"] != null)
                                                                {
                                                                    GetOperatorID(SOperator);
                                                                    try
                                                                    {
                                                                        GetWLID();
                                                                        CheckWLOPStatus();
                                                                        if (WLOpStatus.Equals("1"))
                                                                        {
                                                                            if (ViewState["ServiceProvider"].Equals("eGenApi"))
                                                                            {
                                                                                eGenAPIRecharge();
                                                                            }
                                                                            else if (ViewState["ServiceProvider"].Equals("ExcelRecharge"))
                                                                            {
                                                                                ExcelRecharge();
                                                                            }
                                                                            else if (ViewState["ServiceProvider"].Equals("IWTApi"))
                                                                            {
                                                                                IWTApi();
                                                                            }
                                                                            else if (ViewState["ServiceProvider"].Equals("ApiWorld"))
                                                                            {
                                                                                ApiWorld();
                                                                            }
                                                                        }
                                                                    }
                                                                    catch (Exception ex)
                                                                    { }
                                                                }
                                                                else
                                                                {
                                                                    objcommonfun.SendSMS(AgMobileNo, "Currently this service is not available. Please try after some time", refrenceno, AgMobileNo, stringquery);
                                                                }
                                                            }
                                                            catch (Exception ex)
                                                            {
                                                                lblerror.Text = Convert.ToString(ex);
                                                            }
                                                        }
                                                    }
                                                    else
                                                    {
                                                        //objcommonfun.SendSMS(AgMobileNo, "Dear Partner%2C You don%27t have sufficient balance to do this transaction. Pls take upload in your login", refrenceno, AgMobileNo, stringquery);
                                                        objcommonfun.SendSMS(AgMobileNo, "Dear Partner%2C You don%27t have sufficient balance to do this transaction. Pls take upload in your login", refrenceno, AgMobileNo, stringquery);
                                                    }
                                                }
                                                else
                                                {
                                                    //objcommonfun.SendSMS(AgMobileNo, "Dear Partner%2C You don%27t have sufficient balance to do this transaction. Pls take upload in your login", refrenceno, AgMobileNo, stringquery);
                                                    objcommonfun.SendSMS(AgMobileNo, "Dear Partner%2C Your balance is low from cutoff balance. Please take balance in your login.", refrenceno, AgMobileNo, stringquery);
                                                }
                                            }
                                            else
                                            {
                                                try
                                                {
                                                    SqlParameter[] paramLoginCount = new SqlParameter[]
                                                {
                                                    new SqlParameter("@AgentmobileNo",AgMobileNo)
                                                };
                                                    SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_UpdateLoginCount", paramLoginCount);
                                                }
                                                catch (Exception ex)
                                                {
                                                    throw (ex);
                                                }
                                                objcommonfun.SendSMS(AgMobileNo, "Wrong MPIN used. Account will be blocked on three wrong attempts", refrenceno, AgMobileNo, stringquery);

                                            }
                                        }
                                        else
                                        {
                                            objcommonfun.SendSMS(AgMobileNo, "Dear Partner, Your account has been blocked due to using wrong MPIN three times", refrenceno, AgMobileNo, stringquery);
                                            objcommonfun.UpdateUserStatus(AgMobileNo);
                                        }
                                    }
                                    else
                                    {
                                        objcommonfun.SendSMS(AgMobileNo, "Please set 4 digits numeric MPIN only. MPIN SMS format: MPIN 2222 and send to 9230600500 ", refrenceno, AgMobileNo, stringquery);

                                    }
                                }
                                else
                                {
                                    objcommonfun.SendSMS(AgMobileNo, "Please set 4 digits numeric MPIN only. MPIN SMS format: MPIN 2222 and send to 9230600500 ", refrenceno, AgMobileNo, stringquery);

                                }
                            }
                            else
                            {
                                objcommonfun.SendSMS(AgMobileNo, "Wrong SMS format. Kindly use correct SMS format", refrenceno, AgMobileNo, stringquery);

                            }
                        }
                        else
                        {
                            objcommonfun.SendSMS(AgMobileNo, "You have send Wrong Keyword. Please use M for Mobile Recharge %26 D for DTH Recharge", refrenceno, AgMobileNo, stringquery);

                        }
                    }
                    else
                    {
                        objcommonfun.SendSMS(AgMobileNo, "You have send Wrong Operator Code. Kindly use correct operator code", refrenceno, AgMobileNo, stringquery);

                    }
                }
                else
                { }
            }
            else
            {
                objcommonfun.SendSMS(AgMobileNo, "Your are not authorize to Use SMS services. Kindly contact administrator", refrenceno, AgMobileNo, stringquery);

            }
        }            
    }

    public void UserCutOffDetails()
    {
        try
        {
            SqlParameter[] get_user_details = new SqlParameter[]
            {
                new SqlParameter("@LOGINID",agent_Id)
            };

            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_getcutoffbalance", get_user_details);
            if (dr.Read())
            {
                cutoffBalance = Convert.ToInt32(dr["CuttOffAmount"]);
            }
        }
        catch (Exception ex)
        {
            string st = ex.Message;
        }
    }

    public void userdetails()
    {
        try
        {
            SqlParameter[] get_user_details = new SqlParameter[]
            {
                new SqlParameter("@LOGINID",agent_Id)
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_getavailablebalance", get_user_details);            
            if (ds.Tables[0].Rows.Count > 0)
            {
                Availablebalance = Convert.ToInt32(ds.Tables[0].Rows[0]["balance"]);
            }
        }
        catch (Exception ex)
        {
            string st = ex.Message;
        }
    }

    public void GetMpin()
    {
        try
        {          
            SqlParameter[] get_user_details = new SqlParameter[]
            {
                new SqlParameter("@MobileNo",AgMobileNo)
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_GetMpin", get_user_details);
            if (ds.Tables[0].Rows.Count > 0)
            {
                ActualMPIN = ds.Tables[0].Rows[0]["Mpin1"].ToString();
                agent_Id = ds.Tables[0].Rows[0]["LOGINID"].ToString();
                LoginCount=Convert.ToInt32(ds.Tables[0].Rows[0]["LOGINCOUNT"].ToString());
            }
        }
        catch (Exception ex)
        {
            lblerror.Text = Convert.ToString(ex);
        }
    }

    private void GetOperatorID(string mobileOperator)
    {
        SqlParameter[] ONLINEOFFLINEOPERATOR = new SqlParameter[]
        {
            new SqlParameter("@opname", mobileOperator),
            new SqlParameter("@OperatorType",ViewState["OperatorType"].ToString())
        };
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_GETONLINEOFFLINEOPERATOR", ONLINEOFFLINEOPERATOR);
        if (ds.Tables[0].Rows.Count != 0)
        {
            ViewState["OperatorID"] = ds.Tables[0].Rows[0]["MobileDthOperatorDetailsID"];
            ViewState["ServiceType"] = ds.Tables[0].Rows[0]["ServiceType"];
            ViewState["mobileoperatorValue"] = ds.Tables[0].Rows[0]["eGenApiOpID"];
            ViewState["ExcelRecharge"] = ds.Tables[0].Rows[0]["ExcelRecharge"];
            ViewState["IWTOpID"] = ds.Tables[0].Rows[0]["IWTOpID"];
            ViewState["ApiWorld"] = ds.Tables[0].Rows[0]["ApiWorld"];
        }
    }

    public void GetVendor()
    {
        try
        {
            string optype;
            if (ViewState["OperatorType"].Equals("MOBILE"))
            {
                optype = "MobileRecharge";
            }
            else
            {
                optype = "DTH";
            }
            SqlParameter[] GetCurrentVendor = new SqlParameter[]
            {
                new SqlParameter("@operator",SOperator),
                new SqlParameter("@operatortype",optype)
            };
            DataSet dsetGetCurrentVendor = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_GetServiceVendorOperatorwiseForSMSApplication", GetCurrentVendor);
            if (dsetGetCurrentVendor.Tables[0].Rows.Count > 0)
            {
                if (dsetGetCurrentVendor.Tables[0].Rows[0]["OutVal"].ToString().Equals("1"))
                {
                    ViewState["ServiceProvider"] = dsetGetCurrentVendor.Tables[0].Rows[0]["ServiceProviderSms"].ToString();
                }
            }
        }
        catch (Exception ex)
        {
            lblerror.Text = Convert.ToString(ex);
        }
    }

    private void GetOperator()
    {
        string OpType;
        if (ViewState["servicetype"].ToString().Equals("D"))
        {
            OpType = "DTH";
        }
        else
        {
            OpType = "Mobile";
        }
        try
        {
            SqlParameter[] get_user_details = new SqlParameter[]
            {
                new SqlParameter("@OpCode",ViewState["opertaortype"].ToString()),
                new SqlParameter("@OpType",OpType),
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_GetOperatorByOpCode", get_user_details);
            if (ds.Tables[0].Rows.Count > 0)
            {
                SOperator = Convert.ToString(ds.Tables[0].Rows[0]["OperatorName"]);
                ViewState["OperatorType"] = Convert.ToString(ds.Tables[0].Rows[0]["OperatorType"]);
            }            
        }
        catch (Exception ex)
        {
            lblerror.Text = Convert.ToString(ex);
        }
    }

    public static string TID_GEN()
    {
        string new_tid = System.Guid.NewGuid().ToString();
        new_tid = new_tid.Replace("-", string.Empty).ToLower();
        new_tid = new_tid.Substring(0, 14).Replace("a", "1");
        new_tid = new_tid.Substring(0, 14).Replace("b", "2");
        new_tid = new_tid.Substring(0, 14).Replace("c", "3");
        new_tid = new_tid.Substring(0, 14).Replace("d", "4");
        new_tid = new_tid.Substring(0, 14).Replace("e", "5");
        new_tid = new_tid.Substring(0, 14).Replace("f", "6");
        new_tid = new_tid.Substring(0, 14).Replace("g", "14");
        new_tid = new_tid.Substring(0, 14).Replace("h", "8");
        new_tid = new_tid.Substring(0, 14).Replace("i", "9");
        new_tid = new_tid.Substring(0, 14).Replace("j", "0");
        new_tid = new_tid.Substring(0, 14).Replace("k", "1");
        new_tid = new_tid.Substring(0, 14).Replace("l", "2");
        new_tid = new_tid.Substring(0, 14).Replace("m", "3");
        new_tid = new_tid.Substring(0, 14).Replace("n", "4");
        new_tid = new_tid.Substring(0, 14).Replace("o", "5");
        new_tid = new_tid.Substring(0, 14).Replace("p", "0");
        new_tid = new_tid.Substring(0, 14).Replace("q", "9");
        new_tid = new_tid.Substring(0, 14).Replace("r", "8");
        new_tid = new_tid.Substring(0, 14).Replace("s", "14");
        new_tid = new_tid.Substring(0, 14).Replace("t", "6");
        new_tid = new_tid.Substring(0, 14).Replace("u", "5");
        new_tid = new_tid.Substring(0, 14).Replace("v", "4");
        new_tid = new_tid.Substring(0, 14).Replace("w", "3");
        new_tid = new_tid.Substring(0, 14).Replace("x", "2");
        new_tid = new_tid.Substring(0, 14).Replace("y", "1");
        new_tid = new_tid.Substring(0, 14).Replace("z", "0");
        return new_tid;
    }

    public void eGenAPIRecharge()
    {
        try
        {
            WLAmount();
            ReqBalance = Convert.ToDouble(ViewState["amount"].ToString());
            if (WLbalance > ReqBalance)
            {
                userdetails();
                if (Availablebalance > Convert.ToInt32(ViewState["amount"].ToString()))
                {
                    string IPAddress = Request.UserHostAddress.ToString();
                    if (ViewState["servicetype"].Equals("D"))
                    {
                        result = CommonFunction.InsertTransactionWithPendingStatusSMS(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, "DTH",SOperator, ViewState["OperatorID"].ToString(), 
                         "All Circle", ViewState["mobilenumber"].ToString(), ViewState["amount"].ToString(),ServiceCharge, " ", "eGenApi", "Mobile Application", "DTH Recharge-SMS");

                        if (result == 1)
                        {
                            objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                            Response.Redirect("DublicateTransaction.aspx");
                        }
                        else if (result == 2)
                        {
                            txn = CommonFunction.eGenAPIRecharge("dth", ViewState["mobileoperatorValue"].ToString(), "ALL", ViewState["mobilenumber"].ToString(), ViewState["amount"].ToString(), refrenceno, AgMobileNo, agent_Id);
                            
                            SqlParameter[] TxnResponce = new SqlParameter[]
                            {
                                 new SqlParameter("@TXNRESPONCE",txn),
                                new SqlParameter("@REFRENCENO",refrenceno)
                            };
                            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);
                        }
                    }
                    else
                    {
                        result = CommonFunction.InsertTransactionWithPendingStatusSMS(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, "MOBILE",SOperator, ViewState["OperatorID"].ToString(),
                           "All Circle", ViewState["mobilenumber"].ToString(), ViewState["amount"].ToString(), ServiceCharge, " ", "eGenApi", "Mobile Application", "Mobile Recharge-SMS");

                        if (result == 1)
                        {
                            objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                            Response.Redirect("DublicateTransaction.aspx");
                        }
                        else if (result == 2)
                        {
                            txn = CommonFunction.eGenAPIRecharge("mobile", ViewState["mobileoperatorValue"].ToString(), "ALL", ViewState["mobilenumber"].ToString(), ViewState["amount"].ToString(), refrenceno, AgMobileNo, agent_Id);
                            SqlParameter[] TxnResponce = new SqlParameter[]
                            {
                                 new SqlParameter("@TXNRESPONCE",txn),
                                new SqlParameter("@REFRENCENO",refrenceno)
                            };
                            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);

                        }
                    }
                    if (result == 2)
                    {
                        string res = txn;
                        if (!(res.Contains("error") || res.Contains("Error")))
                        {
                            ds.ReadXml(new StringReader(res));
                            if (ds.Tables["Response"].Rows.Count > 0)
                            {
                                ViewState["resultCode"] = Convert.ToString(ds.Tables["Response"].Rows[0]["resultCode"]);
                                ViewState["Status"] = Convert.ToString(ds.Tables["Response"].Rows[0]["resultValue"]);
                                TxnID = Convert.ToString(ds.Tables["Response"].Rows[0]["tranId"]);
                                ViewState["operatorId"] = Convert.ToString(ds.Tables["Response"].Rows[0]["operatorId"]);
                                if (ViewState["resultCode"].Equals("125") || ViewState["resultCode"].Equals("121") || ViewState["resultCode"].Equals("122"))
                                {
                                    if (ViewState["resultCode"].Equals("125"))
                                    {
                                        _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, ViewState["amount"].ToString(), "Success", ViewState["Status"].ToString(), txn, TxnID, ViewState["operatorId"].ToString());
                                        BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);

                                        if (ViewState["servicetype"].Equals("D"))
                                        {
                                            objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", DTH Account No." + " " + ViewState["mobilenumber"] + " " + " has been successfully recharged with Rs. " + " " + ViewState["amount"].ToString() + " " + ". your login Balance is Rs." + " " + BalanceAfterTransaction + "", refrenceno, AgMobileNo, stringquery);
                                        }
                                        else
                                        {
                                            objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Mobile Number  " + " " + ViewState["mobilenumber"] + " " + " has been successfully recharged with Rs. " + " " + ViewState["amount"].ToString() + " " + ". your login Balance is Rs." + " " + BalanceAfterTransaction + "", refrenceno, AgMobileNo, stringquery);
                                        };
                                    }
                                    else if (ViewState["resultCode"].Equals("121") || ViewState["resultCode"].Equals("122"))
                                    {
                                        _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, ViewState["amount"].ToString(), "Pending", ViewState["Status"].ToString(), txn, TxnID, ViewState["operatorId"].ToString());
                                        BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                                        if (ViewState["servicetype"].Equals("D"))
                                        {
                                            objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction for DTH A/C No. " + " " + ViewState["mobilenumber"] + " " + " is going on pending. your login Balance is Rs " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                                        }
                                        else
                                        {
                                            objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction for Mobile No. " + " " + ViewState["mobilenumber"] + " " + " is going on pending your login Balance is Rs. " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                                        }
                                    }
                                }
                                else
                                {
                                    _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, ViewState["amount"].ToString(), "Fail", ViewState["Status"].ToString(), txn, refrenceno, ViewState["operatorId"].ToString());
                                    BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                                    objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                                }
                            }
                        }
                        else
                        {
                            _ObjWBMNverify.FinalUpdate(WLID,agent_Id, refrenceno, ViewState["amount"].ToString(), "Fail", ErrorMessage, txn, refrenceno, "");
                            BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                            objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                        }
                    }
                }
                else
                {
                    objcommonfun.SendSMS(AgMobileNo, "Dear Partner%2C You don%27t have sufficient balance to do this transaction. Pls take upload in your login", refrenceno, AgMobileNo, stringquery);
                }
            }
            else
            {
                objcommonfun.SendSMS(AgMobileNo, "Dear Partner Your Administrator don%27t have sufficient balance to do this transaction. Pls take upload in your login", refrenceno, AgMobileNo, stringquery);
            }
        }
        catch (Exception ex)
        {
            lblerror.Text = Convert.ToString(ex);
        }
    }


    //******************************ExcelRecharge*******************************
    public void ExcelRecharge()
    {
        try
        {
            WLAmount();
            ReqBalance = Convert.ToDouble(ViewState["amount"].ToString());
            if (WLbalance > ReqBalance)
            {
                userdetails();
                if (Availablebalance > Convert.ToInt32(ViewState["amount"].ToString()))
                {
                    string IPAddress = Request.UserHostAddress.ToString();
                    if (ViewState["servicetype"].Equals("D"))
                    {
                        result = CommonFunction.InsertTransactionWithPendingStatusSMS(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, "DTH", SOperator, ViewState["OperatorID"].ToString(),
                         "All Circle", ViewState["mobilenumber"].ToString(), ViewState["amount"].ToString(), ServiceCharge, " ", "ExcelRecharge", "Mobile Application", "DTH Recharge-SMS");

                        if (result == 1)
                        {
                            objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                            Response.Redirect("DublicateTransaction.aspx");
                        }
                        else if (result == 2)
                        {
                            txn = CommonFunction.ExcelApiRecharge(agent_Id, "19", "123456", ViewState["amount"].ToString(), ViewState["mobilenumber"].ToString(), ViewState["ExcelRecharge"].ToString(), refrenceno, "", "", "DTH Recharge-SMS");
                            SqlParameter[] TxnResponce = new SqlParameter[]
                            {
                                new SqlParameter("@TXNRESPONCE",txn),
                                new SqlParameter("@REFRENCENO",refrenceno)
                            };
                            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);
                        }
                    }
                    else
                    {
                        result = CommonFunction.InsertTransactionWithPendingStatusSMS(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, "MOBILE", SOperator, ViewState["OperatorID"].ToString(),
                           "All Circle", ViewState["mobilenumber"].ToString(), ViewState["amount"].ToString(), ServiceCharge, " ", "ExcelRecharge", "Mobile Application", "Mobile Recharge-SMS");

                        if (result == 1)
                        {
                            objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                            Response.Redirect("DublicateTransaction.aspx");
                        }
                        else if (result == 2)
                        {
                            txn = CommonFunction.ExcelApiRecharge(agent_Id, "19", "123456", ViewState["amount"].ToString(), ViewState["mobilenumber"].ToString(), ViewState["ExcelRecharge"].ToString(), refrenceno, "", "", "Mobile Recharge-SMS");
                            SqlParameter[] TxnResponce = new SqlParameter[]
                            {
                                 new SqlParameter("@TXNRESPONCE",txn),
                                new SqlParameter("@REFRENCENO",refrenceno)
                            };
                            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);

                        }
                    }
                    if (result == 2)
                    {
                        string res = txn;
                        if (res.Contains("Pending") || res.Contains("PENDING"))
                        {
                            ds.ReadXml(new StringReader(res));
                            if (ds.Tables["Data"].Rows.Count > 0)
                            {
                                ViewState["OrderId"] = Convert.ToString(ds.Tables["Data"].Rows[0]["OrderId"]);
                                ViewState["Status"] = Convert.ToString(ds.Tables["Data"].Rows[0]["Status"]);
                                TxnID = Convert.ToString(ds.Tables["Data"].Rows[0]["MerOid"]);
                                if (ViewState["Status"].Equals("Failure") || ViewState["Status"].Equals("FAILURE"))
                                {
                                    _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, ViewState["amount"].ToString(), "Fail", ViewState["Status"].ToString(), txn, TxnID, ViewState["ExcelRecharge"].ToString());
                                    BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                                    if (ViewState["servicetype"].Equals("D"))
                                    {
                                        objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                                                                            }
                                    else
                                    {
                                        objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                                    }
                                }
                                else if (ViewState["Status"].Equals("Pending") || ViewState["Status"].Equals("PENDING"))
                                {
                                    _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, ViewState["amount"].ToString(), "Success", ViewState["Status"].ToString(), txn, TxnID, ViewState["ExcelRecharge"].ToString());
                                    BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                                    if (ViewState["servicetype"].Equals("D"))
                                    {
                                        objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", DTH Account No." + " " + ViewState["mobilenumber"] + " " + " has been successfully recharged with Rs. " + " " + ViewState["amount"].ToString() + " " + ". your login Balance is Rs." + " " + BalanceAfterTransaction + "", refrenceno, AgMobileNo, stringquery);
                                    }
                                    else
                                    {
                                        objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Mobile Number  " + " " + ViewState["mobilenumber"] + " " + " has been successfully recharged with Rs. " + " " + ViewState["amount"].ToString() + " " + ". your login Balance is Rs." + " " + BalanceAfterTransaction + "", refrenceno, AgMobileNo, stringquery);
                                    }
                                }
                                else
                                {
                                    _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, ViewState["amount"].ToString(), "Success", ViewState["Status"].ToString(), txn, TxnID, ViewState["ExcelRecharge"].ToString());
                                    BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                                    if (ViewState["servicetype"].Equals("D"))
                                    {                                        
                                        objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", DTH Account No." + " " + ViewState["mobilenumber"] + " " + " has been successfully recharged with Rs. " + " " + ViewState["amount"].ToString() + " " + ". your login Balance is Rs." + " " + BalanceAfterTransaction + "", refrenceno, AgMobileNo, stringquery);
                                    }
                                    else
                                    {
                                        objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Mobile Number  " + " " + ViewState["mobilenumber"] + " " + " has been successfully recharged with Rs. " + " " + ViewState["amount"].ToString() + " " + ". your login Balance is Rs." + " " + BalanceAfterTransaction + "", refrenceno, AgMobileNo, stringquery);
                                    }
                                }
                            }
                        }
                        else
                        {
                            _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, ViewState["amount"].ToString(), "Fail", ErrorMessage, txn, refrenceno, "");
                            BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                            objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                        }
                    }
                }
                else
                {
                    objcommonfun.SendSMS(AgMobileNo, "Dear Partner%2C You don%27t have sufficient balance to do this transaction. Pls take upload in your login", refrenceno, AgMobileNo, stringquery);
                }
            }
            else
            {
                objcommonfun.SendSMS(AgMobileNo, "Dear Partner Your Administrator don%27t have sufficient balance to do this transaction. Pls take upload in your login", refrenceno, AgMobileNo, stringquery);
            }
        }
        catch (Exception ex)
        {
            lblerror.Text = Convert.ToString(ex);
        }
    }
    //**************************************************************************
    public void ApiWorld()
    {
        try
        {
            WLAmount();
            ReqBalance = Convert.ToDouble(ViewState["amount"].ToString());
            if (WLbalance > ReqBalance)
            {
                userdetails();
                if (Availablebalance > Convert.ToInt32(ViewState["amount"].ToString()))
                {
                    string IPAddress = Request.UserHostAddress.ToString();
                    if (ViewState["servicetype"].Equals("D"))
                    {
                        result = CommonFunction.InsertTransactionWithPendingStatusSMS(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, "DTH", SOperator, ViewState["OperatorID"].ToString(),
                         "All Circle", ViewState["mobilenumber"].ToString(), ViewState["amount"].ToString(), ServiceCharge, " ", "ApiWorld", "Mobile Application", "DTH Recharge-SMS");

                        if (result == 1)
                        {
                            objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                            Response.Redirect("DublicateTransaction.aspx");
                        }
                        else if (result == 2)
                        {
                            txn = CommonFunction.ApiWorldRecharge(agent_Id, "19", "DH", ViewState["amount"].ToString(), ViewState["mobilenumber"].ToString(), ViewState["ApiWorld"].ToString(), refrenceno, "", "", "DTH Recharge-SMS");
                            SqlParameter[] TxnResponce = new SqlParameter[]
                            {
                                new SqlParameter("@TXNRESPONCE",txn),
                                new SqlParameter("@REFRENCENO",refrenceno)
                            };
                            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);
                        }
                    }
                    else
                    {
                        result = CommonFunction.InsertTransactionWithPendingStatusSMS(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, "MOBILE", SOperator, ViewState["OperatorID"].ToString(),
                           "All Circle", ViewState["mobilenumber"].ToString(), ViewState["amount"].ToString(), ServiceCharge, " ", "ApiWorld", "Mobile Application", "Mobile Recharge-SMS");

                        if (result == 1)
                        {
                            objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                            Response.Redirect("DublicateTransaction.aspx");
                        }
                        else if (result == 2)
                        {
                            txn = CommonFunction.ApiWorldRecharge(agent_Id, "19", "MR", ViewState["amount"].ToString(), ViewState["mobilenumber"].ToString(), ViewState["ApiWorld"].ToString(), refrenceno, "", "", "Mobile Recharge-SMS");
                            SqlParameter[] TxnResponce = new SqlParameter[]
                            {
                                 new SqlParameter("@TXNRESPONCE",txn),
                                new SqlParameter("@REFRENCENO",refrenceno)
                            };
                            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);

                        }
                    }
                    if (result == 2)
                    {
                        string res = txn;
                        if (res.Contains("PROCESSED") || res.Contains("SUCCESS") || res.Contains("Processed") || res.Contains("Success"))
                        {
                            string[] result_array = res.Split(' ');
                            if (result_array.Length > 2)
                            {
                                ViewState["OrderId"] = Convert.ToString(result_array[0]);
                                ViewState["Status"] = Convert.ToString(result_array[1]);
                                TxnID = Convert.ToString(result_array[3]);
                                if (ViewState["Status"].Equals("Failure") || ViewState["Status"].Equals("FAILURE"))
                                {
                                    _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, ViewState["amount"].ToString(), "Fail", ViewState["Status"].ToString(), txn, ViewState["OrderId"].ToString(), "");
                                    BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                                    if (ViewState["servicetype"].Equals("D"))
                                    {
                                        objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                                    }
                                    else
                                    {
                                        objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                                    }
                                }
                                else if (ViewState["Status"].Equals("Processed") || ViewState["Status"].Equals("PROCESSED"))
                                {
                                    _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, ViewState["amount"].ToString(), "Success", ViewState["Status"].ToString(), txn, ViewState["OrderId"].ToString(), "");
                                    BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                                    if (ViewState["servicetype"].Equals("D"))
                                    {
                                        objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", DTH Account No." + " " + ViewState["mobilenumber"] + " " + " has been successfully recharged with Rs. " + " " + ViewState["amount"].ToString() + " " + ". your login Balance is Rs." + " " + BalanceAfterTransaction + "", refrenceno, AgMobileNo, stringquery);
                                    }
                                    else
                                    {
                                        objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Mobile Number  " + " " + ViewState["mobilenumber"] + " " + " has been successfully recharged with Rs. " + " " + ViewState["amount"].ToString() + " " + ". your login Balance is Rs." + " " + BalanceAfterTransaction + "", refrenceno, AgMobileNo, stringquery);
                                    }
                                }
                                else
                                {
                                    _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, ViewState["amount"].ToString(), "Success", ViewState["Status"].ToString(), txn, ViewState["OrderId"].ToString(), "");
                                    BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                                    if (ViewState["servicetype"].Equals("D"))
                                    {
                                        objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", DTH Account No." + " " + ViewState["mobilenumber"] + " " + " has been successfully recharged with Rs. " + " " + ViewState["amount"].ToString() + " " + ". your login Balance is Rs." + " " + BalanceAfterTransaction + "", refrenceno, AgMobileNo, stringquery);
                                    }
                                    else
                                    {
                                        objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Mobile Number  " + " " + ViewState["mobilenumber"] + " " + " has been successfully recharged with Rs. " + " " + ViewState["amount"].ToString() + " " + ". your login Balance is Rs." + " " + BalanceAfterTransaction + "", refrenceno, AgMobileNo, stringquery);
                                    }
                                }
                            }
                        }
                        else
                        {
                            _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, ViewState["amount"].ToString(), "Fail", ErrorMessage, txn, refrenceno, "");
                            BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                            objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                        }
                    }
                }
                else
                {
                    objcommonfun.SendSMS(AgMobileNo, "Dear Partner%2C You don%27t have sufficient balance to do this transaction. Pls take upload in your login", refrenceno, AgMobileNo, stringquery);
                }
            }
            else
            {
                objcommonfun.SendSMS(AgMobileNo, "Dear Partner Your Administrator don%27t have sufficient balance to do this transaction. Pls take upload in your login", refrenceno, AgMobileNo, stringquery);
            }
        }
        catch (Exception ex)
        {
            lblerror.Text = Convert.ToString(ex);
        }
    }
    public void GetWLID()
    {
        try
        {
            SqlParameter[] get_user_details = new SqlParameter[]
            {
                new SqlParameter("@agent_Id",agent_Id)
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_GetWLIDForVB", get_user_details);
            if (ds.Tables[0].Rows.Count > 0)
            {
                WLID = ds.Tables[0].Rows[0]["WLID"].ToString();
            }
        }
        catch (Exception ex)
        {
            string st = ex.Message;
        }
    }

    public void CheckWLOPStatus()
    {
        string OpType;
        if (ViewState["servicetype"].ToString().Equals("D"))
        {
            OpType = "DTH";
        }
        else
        {
            OpType = "Mobile";
        }
        SqlParameter[] getoperatorstatus = new SqlParameter[]
        {
            new SqlParameter("@WLID",WLID),
            new SqlParameter("@operator",SOperator),
            new SqlParameter("@ServiceType",OpType)
        };
        DataSet Opds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_Getoperatorstatus", getoperatorstatus);
        WLOpStatus = Opds.Tables[0].Rows[0]["Flag"].ToString();
    }

    public void WLAmount()
    {
        try
        {
            SqlParameter[] get_user_details = new SqlParameter[]
            {
                new SqlParameter("@LOGINID",WLID)
            };
            SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_getavailablebalance", get_user_details);
            if (dr.Read())
            {
                WLbalance = Convert.ToInt32(dr["balance"]);
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some problem please try after some time');"), true);
        }
    }
    public void IWTApi()
    {
        try
        {
            WLAmount();
            ReqBalance = Convert.ToDouble(ViewState["amount"].ToString());
            if (WLbalance > ReqBalance)
            {
                userdetails();
                if (Availablebalance > Convert.ToInt32(ViewState["amount"].ToString()))
                {
                    string IPAddress = Request.UserHostAddress.ToString();
                    if (ViewState["servicetype"].Equals("D"))
                    {
                        result = CommonFunction.InsertTransactionWithPendingStatusSMS(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, "DTH", SOperator, ViewState["OperatorID"].ToString(),
                         "All Circle", ViewState["mobilenumber"].ToString(), ViewState["amount"].ToString(), ServiceCharge, " ", "IWTApi", "Mobile Application", "DTH Recharge-SMS");

                        if (result == 1)
                        {
                            objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                            Response.Redirect("DublicateTransaction.aspx");
                        }
                        else if (result == 2)
                        {
                            txn = CommonFunction.IWTApiRecharge(agent_Id, ViewState["amount"].ToString(), ViewState["mobilenumber"].ToString(), ViewState["IWTOpID"].ToString(), refrenceno, "", "", "DTH Recharge-SMS");
                            SqlParameter[] TxnResponce = new SqlParameter[]
                            {
                                new SqlParameter("@TXNRESPONCE",txn),
                                new SqlParameter("@REFRENCENO",refrenceno)
                            };
                            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);
                        }
                    }
                    else
                    {
                        result = CommonFunction.InsertTransactionWithPendingStatusSMS(WLID, IPAddress.ToUpper(), refrenceno, agent_Id, "MOBILE", SOperator, ViewState["OperatorID"].ToString(),
                           "All Circle", ViewState["mobilenumber"].ToString(), ViewState["amount"].ToString(), ServiceCharge, " ", "IWTApi", "Mobile Application", "Mobile Recharge-SMS");

                        if (result == 1)
                        {
                            objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                            Response.Redirect("DublicateTransaction.aspx");
                        }
                        else if (result == 2)
                        {
                            txn = CommonFunction.IWTApiRecharge(agent_Id, ViewState["amount"].ToString(), ViewState["mobilenumber"].ToString(), ViewState["IWTOpID"].ToString(), refrenceno, "", "", "Mobile Recharge-SMS");
                            SqlParameter[] TxnResponce = new SqlParameter[]
                            {
                                 new SqlParameter("@TXNRESPONCE",txn),
                                new SqlParameter("@REFRENCENO",refrenceno)
                            };
                            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "USP_INSERTTXNRESPONCEFIRSTTIME", TxnResponce);

                        }
                    }
                    if (result == 2)
                    {
                        string res = txn;
                        ds.ReadXml(new StringReader(res));
                        if (ds.Tables["RechargeResponse"].Rows.Count > 0)
                        {
                            ViewState["ErrorCode"] = Convert.ToString(ds.Tables["RechargeResponse"].Rows[0]["ErrorCode"]);
                            ViewState["Desc"] = Convert.ToString(ds.Tables["RechargeResponse"].Rows[0]["Desc"]);
                            if (ViewState["ErrorCode"].Equals("00000"))
                            {
                                TxnID = Convert.ToString(ds.Tables["RechargeResponse"].Rows[0]["TxnID"]);
                                _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, ViewState["amount"].ToString(), "Success", ViewState["Desc"].ToString(), txn, TxnID, ViewState["IWTOpID"].ToString());
                                BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                                if (ViewState["servicetype"].Equals("D"))
                                {
                                    objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", DTH Account No." + " " + ViewState["mobilenumber"] + " " + " has been successfully recharged with Rs. " + " " + ViewState["amount"].ToString() + " " + ". your login Balance is Rs." + " " + BalanceAfterTransaction + "", refrenceno, AgMobileNo, stringquery);
                                }
                                else
                                {
                                    objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Mobile Number  " + " " + ViewState["mobilenumber"] + " " + " has been successfully recharged with Rs. " + " " + ViewState["amount"].ToString() + " " + ". your login Balance is Rs." + " " + BalanceAfterTransaction + "", refrenceno, AgMobileNo, stringquery);
                                }
                            }
                            else
                            {
                                _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, ViewState["amount"].ToString(), "Fail", ViewState["Desc"].ToString(), txn, refrenceno, ViewState["IWTOpID"].ToString());
                                BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                                if (ViewState["servicetype"].Equals("D"))
                                {
                                    objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                                }
                                else
                                {
                                    objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                                }
                            }
                        }
                        else
                        {
                            _ObjWBMNverify.FinalUpdate(WLID, agent_Id, refrenceno, ViewState["amount"].ToString(), "Fail", "Failed", txn, refrenceno, ViewState["IWTOpID"].ToString());
                            BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                            if (ViewState["servicetype"].Equals("D"))
                            {
                                objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                            }
                            else
                            {
                                objcommonfun.SendSMS(AgMobileNo, "TID-" + " " + refrenceno + " " + ", Your transaction has been failed from Operator End, Please try with correct details. Your login balance is " + " " + BalanceAfterTransaction + " " + "", refrenceno, AgMobileNo, stringquery);
                            }
                        }
                        
                    }
                }
                else
                {
                    objcommonfun.SendSMS(AgMobileNo, "Dear Partner%2C You don%27t have sufficient balance to do this transaction. Pls take upload in your login", refrenceno, AgMobileNo, stringquery);
                }
            }
            else
            {
                objcommonfun.SendSMS(AgMobileNo, "Dear Partner Your Administrator don%27t have sufficient balance to do this transaction. Pls take upload in your login", refrenceno, AgMobileNo, stringquery);
            }
        }
        catch (Exception ex)
        {
            lblerror.Text = Convert.ToString(ex);
        }
    }

    public static string TID_GEN1()
    {
        string new_tid = System.Guid.NewGuid().ToString();
        new_tid = new_tid.Replace("-", string.Empty).ToLower();
        new_tid = new_tid.Substring(0, 16).Replace("a", "1");
        new_tid = new_tid.Substring(0, 16).Replace("b", "2");
        new_tid = new_tid.Substring(0, 16).Replace("c", "3");
        new_tid = new_tid.Substring(0, 16).Replace("d", "4");
        new_tid = new_tid.Substring(0, 16).Replace("e", "5");
        new_tid = new_tid.Substring(0, 16).Replace("f", "6");
        new_tid = new_tid.Substring(0, 16).Replace("g", "7");
        new_tid = new_tid.Substring(0, 16).Replace("h", "8");
        new_tid = new_tid.Substring(0, 16).Replace("i", "9");
        new_tid = new_tid.Substring(0, 16).Replace("j", "0");
        new_tid = new_tid.Substring(0, 16).Replace("k", "1");
        new_tid = new_tid.Substring(0, 16).Replace("l", "2");
        new_tid = new_tid.Substring(0, 16).Replace("m", "3");
        new_tid = new_tid.Substring(0, 16).Replace("n", "4");
        new_tid = new_tid.Substring(0, 16).Replace("o", "5");
        new_tid = new_tid.Substring(0, 16).Replace("p", "0");
        new_tid = new_tid.Substring(0, 16).Replace("q", "9");
        new_tid = new_tid.Substring(0, 16).Replace("r", "8");
        new_tid = new_tid.Substring(0, 16).Replace("s", "7");
        new_tid = new_tid.Substring(0, 16).Replace("t", "6");
        new_tid = new_tid.Substring(0, 16).Replace("u", "5");
        new_tid = new_tid.Substring(0, 16).Replace("v", "4");
        new_tid = new_tid.Substring(0, 16).Replace("w", "3");
        new_tid = new_tid.Substring(0, 16).Replace("x", "2");
        new_tid = new_tid.Substring(0, 16).Replace("y", "1");
        new_tid = new_tid.Substring(0, 16).Replace("z", "0");
        return new_tid;
    }



    private void SetMpin(string msg, string Agentmobile_no, string refrenceno)
    {
        //stringquery = "VUS+MPIN+5544";
       string stringquery_mpin = msg;
       string MobileNo_mpin = Agentmobile_no;
       string RefrenceNo_mpin = refrenceno;
        //string[] stringquery11 = new string[10];
        //stringquery11 = stringquery.Split('/');

        //msg = stringquery;
        msg = Regex.Replace(msg, @"\s+", " ");
        string[] sms = msg.Split(' ');
        ViewState["Mpin"] = Convert.ToString(sms.GetValue(2));
       string AgMobileNo_mpin = MobileNo_mpin.Substring(MobileNo_mpin.Length - Math.Min(10, MobileNo_mpin.Length));
        string strNAme = string.Empty;
        strNAme = ViewState["Mpin"].ToString();
        if (Regex.IsMatch(strNAme, "[A-Z|a-z]"))
        {
            SendSMS(AgMobileNo_mpin, "Please set 4 digits numeric MPIN only. MPIN SMS format: MPIN XXXX and send to XXXX ", RefrenceNo_mpin);
        }
        else if (ViewState["Mpin"].ToString().Length.Equals(4))
        {
            try
            {
                SqlParameter[] paramUpdateMpin = new SqlParameter[]
                {
                    new SqlParameter("@MobileNo",AgMobileNo_mpin),
                    new SqlParameter("@Mpin",Convert.ToInt32(ViewState["Mpin"].ToString()))
                };
                SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_UpdateMpin", paramUpdateMpin);
                //SendSMS(AgMobileNo, "Dear Partner, Your MPIN set successfully. Pls %E2%80%99 share it to anyone. Delete MPIN containing SMS for security of your account");
                objcommonfun.SendSMS(AgMobileNo_mpin, "Dear Partner, Your MPIN set successfully. Pls don’t share it to anyone. Delete MPIN containing SMS for security of your account", RefrenceNo_mpin, MobileNo_mpin, "");
            }
            catch (Exception ex)
            {
            }
        }
    }


    private void SendSMS(string MobileNo, string sms, string RefrenceNo)
    {
        string CourentDate = DateTime.Now.ToShortDateString();
        string strUrl = "http://sms.igenservices.in/api/web2sms.php?workingkey=A0c8f3938f877b4a46175dfa15e04b064&sender=RECHRG&to=" + MobileNo + "&message=" + sms + "";
        WebRequest request = HttpWebRequest.Create(strUrl);
        request.Method = "GET";
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream s = (Stream)response.GetResponseStream();
        StreamReader readStream = new StreamReader(s);
        string dataString = readStream.ReadToEnd();
        response.Close();
        s.Close();
        readStream.Close();
        try
        {
            SqlParameter[] url = new SqlParameter[]
            {
                new SqlParameter("@RefNumber",RefrenceNo),
                new SqlParameter("@SentMessage",sms),
                new SqlParameter("@SMSResponce",dataString),
            };
            SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_Responcegeturl", url);
        }
        catch (Exception ex)
        {
            CommonFunction.writeToLogFile(Convert.ToString(ex));
        }
    }


    private void AmountTransfer(string msg, string Agentmobile_no, string refrenceno)
    {
        //VUS+BAL+AT+8002266480
        stringquery = msg;
      string  MobileNo = Agentmobile_no;
        string RefrenceNo = refrenceno;
        //string[] stringquery11 = new string[10];
        //stringquery11 = stringquery.Split('/');

        msg = stringquery;
        msg = Regex.Replace(msg, @"\s+", " ");
        string[] sms = msg.Split(' ');
        ViewState["Keyword"] = Convert.ToString(sms.GetValue(1));
        ViewState["Userid"] = Convert.ToString(sms.GetValue(2));
        ViewState["Amount"] = Convert.ToString(sms.GetValue(3));
        ViewState["mpin"] = Convert.ToString(sms.GetValue(4));
        AgMobileNo = MobileNo.Substring(MobileNo.Length - Math.Min(10, MobileNo.Length));
        string strAmt = string.Empty;
        strAmt = ViewState["Amount"].ToString();
        string userstatus = objcommonfun.GetUserStatus(AgMobileNo);
        GetMpin();
        if (userstatus.Equals("1"))
        {
            if (LoginCount <= 2)
            {
                if (ActualMPIN == Convert.ToString(ViewState["mpin"]))
                {
                    if (Regex.IsMatch(strAmt, "[A-Z|a-z]"))
                    {
                        BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                        if (Convert.ToDouble(BalanceAfterTransaction) > 0.25)
                        {
                            deductamount(RefrenceNo);
                            objcommonfun.SendSMS(AgMobileNo, "You have send Wrong Amount. Kindly use correct Amount This SMS charge@ 0.25 paisa", RefrenceNo, AgMobileNo, stringquery);
                        }
                    }
                    else
                    {
                        GetAgentdetail();
                        if (ViewState["PartnerID"] != null)
                        {
                            getuserdetail();
                            if (flagREFREV.Equals("1"))
                            {
                                refundamount(RefrenceNo);
                            }
                            else
                            {
                                BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                                if (Convert.ToDouble(BalanceAfterTransaction) > 0.25)
                                {
                                    deductamount(RefrenceNo);
                                    objcommonfun.SendSMS(AgMobileNo, "You are not authorise to use this services. This SMS charge@ 0.25 paisa", RefrenceNo, AgMobileNo, stringquery);
                                }
                            }
                        }
                        else
                        {
                            BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                            if (Convert.ToDouble(BalanceAfterTransaction) > 0.25)
                            {
                                deductamount(RefrenceNo);
                                objcommonfun.SendSMS(AgMobileNo, "You have send Wrong Partner ID. Kindly use correct Partner ID This SMS charge@ 0.25 paisa", RefrenceNo, AgMobileNo, stringquery);
                            }
                        }
                    }
                }
                else
                {
                    try
                    {
                        SqlParameter[] paramLoginCount = new SqlParameter[]
                        {
                            new SqlParameter("@AgentmobileNo",AgMobileNo)
                        };
                        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_UpdateLoginCount", paramLoginCount);
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                    if (Convert.ToDouble(BalanceAfterTransaction) > 0.25)
                    {
                        deductamount(RefrenceNo);
                        objcommonfun.SendSMS(AgMobileNo, "Wrong MPIN used. Account will be blocked on three wrong attempts This SMS charge @0.25 paisa", RefrenceNo, AgMobileNo, stringquery);
                    }
                }
            }
            else
            {
                BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                if (Convert.ToDouble(BalanceAfterTransaction) > 0.25)
                {
                    deductamount(RefrenceNo);
                    objcommonfun.SendSMS(AgMobileNo, "Dear Partner, Your account has been blocked due to using wrong MPIN three times", RefrenceNo, AgMobileNo, stringquery);
                    objcommonfun.UpdateUserStatus(AgMobileNo);
                }
            }
        }
        else
        {
            BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
            if (Convert.ToDouble(BalanceAfterTransaction) > 0.25)
            {
                deductamount(RefrenceNo);
                objcommonfun.SendSMS(AgMobileNo, "Your are not authorize to Use SMS services. Kindly contact administrator", RefrenceNo, AgMobileNo, stringquery);
            }
        } 
    }

    public void deductamount(string RefrenceNo)
    {
        try
        {
            SqlParameter[] deductamount = new SqlParameter[]
            {
                new SqlParameter("@Loginid",agent_Id),                
                new SqlParameter("@refrenceno",RefrenceNo),
                new SqlParameter("@balance",BalanceAfterTransaction),
                new SqlParameter("@ServiceName","SMS Charge") 
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_deductAmountForSMS", deductamount);
        }
        catch (Exception ex)
        {
            //CommonFunction.writeToLogFile(Convert.ToString(ex));
        }
    }

    public void GetAgentdetail()
    {
        SqlParameter[] getuserdetail = new SqlParameter[]
        {
            new SqlParameter("@Username",ViewState["Userid"].ToString()),
            new SqlParameter ("@Upperlevel",agent_Id)
        };
        DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "usp_GetAgentDetail", getuserdetail);
        if (ds.Tables[0].Rows.Count > 0)
        {
            ViewState["PartnerID"] = ds.Tables[0].Rows[0]["Loginid"].ToString();
            ViewState["MobileNo"] = ds.Tables[0].Rows[0]["MobileNo"].ToString();
        }
    }

    public void getuserdetail()
    {
        try
        {
            SqlParameter[] getserviceactivation = new SqlParameter[] 
            {
                new SqlParameter("@LOGINID",agent_Id)
            };
            DataSet ds = SqlHelper.GetDataSet(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_USERDETAILS_forsmserviceActivation", getserviceactivation);
            if (ds.Tables[0].Rows.Count > 0)
            {
                flagREFREV = ds.Tables[0].Rows[0]["SMServicesTRFREV"].ToString();
            }
        }
        catch (Exception ex)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "OhCrap", String.Format("alert('There is some problem .Please try again.');"), true);
        }
    }
    public void userdetailP()
    {
        SqlParameter[] get_user_details = new SqlParameter[]
        {
            new SqlParameter("@LOGINID",ViewState["PartnerID"].ToString())
        };
        SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_getavailablebalance", get_user_details);
        if (dr.Read())
        {
            balanceR = Convert.ToDecimal(dr["balance"]);
        }
    }
    private void refundamount(string RefrenceNo)
    {
        if (ViewState["Keyword"].Equals("REV") || ViewState["Keyword"].Equals("Rev") || ViewState["Keyword"].Equals("rev"))
        {
            try
            {
                userdetailP();
                if (Convert.ToDouble(ViewState["Amount"].ToString()) < 500 || Convert.ToDouble(ViewState["Amount"].ToString()) > 5000)
                {
                    BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                    if (Convert.ToDouble(BalanceAfterTransaction) > 0.25)
                    {
                        deductamount(RefrenceNo);
                        objcommonfun.SendSMS(AgMobileNo, "Invalid Amount for Reverse Minimum value for reverse is 500 and maximum is 5000 This SMS charge @ 0.25 paisa", RefrenceNo, AgMobileNo, stringquery);
                    }
                }
                else
                {
                    requestbalance = Convert.ToDecimal(ViewState["Amount"].ToString());
                    if (requestbalance < balanceR)
                    {
                        SqlParameter[] param = new SqlParameter[]
                        {
                            new SqlParameter("@USERMAME",ViewState["Userid"].ToString()),
                            new SqlParameter("@refundedamount",requestbalance),
                            new SqlParameter("@ref_no_refunded",RefrenceNo),
                            new SqlParameter("@remark",""),                        
                            new SqlParameter("@IPAddress",IPAddress),
                            new SqlParameter("@ErrorMessage",SqlDbType.Int)
                        };
                        param[5].Direction = ParameterDirection.Output;
                        SqlHelper.ExecuteNonQuery(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "UDP_PartnerRollback_Amount_sms", param);
                        int result = Convert.ToInt32(param[5].Value);
                        if (result == 1)
                        {
                            objcommonfun.SendSMS(AgMobileNo, "There is some technical problem .Please try again", RefrenceNo, AgMobileNo, stringquery);
                        }
                        else
                        {
                            BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                            objcommonfun.SendSMS(AgMobileNo, "TID " + "" + RefrenceNo + " Amount " + requestbalance + " has been reversed from " + ViewState["Userid"].ToString() + " successfully Your current balance is " + BalanceAfterTransaction + " This SMS charge @ 0.25 paisa", RefrenceNo, AgMobileNo, stringquery);
                            //deductamount();
                            BalanceAfterTransaction = objcommonfun.userdetails(ViewState["PartnerID"].ToString());
                            objcommonfun.SendSMSByAdmin(ViewState["MobileNo"].ToString(), "An amount of Rs. " + requestbalance + " debited from your account on " + Convert.ToDateTime(System.DateTime.Now.ToShortDateString()).ToString("dd-MMM-yyyy") + " at " + DateTime.Now.ToString("HH:mm:ss") + " against txn id " + RefrenceNo + " by " + ViewState["Username"].ToString() + " Your Current Balance is " + BalanceAfterTransaction + "", RefrenceNo, ViewState["PartnerID"].ToString());


                        }
                    }
                    else
                    {
                        BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                        if (Convert.ToDouble(BalanceAfterTransaction) > 0.25)
                        {
                            deductamount(RefrenceNo);
                            objcommonfun.SendSMS(AgMobileNo, "Insufficient Balance for Reversal Only " + balanceR + " can be reverse This SMS charge @ 0.25 paisa", RefrenceNo, AgMobileNo, stringquery);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //CommonFunction.writeToLogFile(Convert.ToString(ex));
            }
        }
        if (ViewState["Keyword"].Equals("TRF") || ViewState["Keyword"].Equals("Trf") || ViewState["Keyword"].Equals("trf"))
        {
            try
            {
                userdetails();
                if (Convert.ToDouble(ViewState["Amount"].ToString()) < 500 || Convert.ToDouble(ViewState["Amount"].ToString()) > 5000)
                {
                    BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                    if (Convert.ToDouble(BalanceAfterTransaction) > 0.25)
                    {
                        deductamount(RefrenceNo);
                        objcommonfun.SendSMS(AgMobileNo, "Invalid Amount for Transfer Minimum value for transfer is 500 and maximum is 5000 This SMS charge @ 0.25 paisa", RefrenceNo, AgMobileNo, stringquery);
                    }
                }
                else
                {
                    requestbalance = Convert.ToDecimal(ViewState["Amount"].ToString());
                    if (requestbalance < Convert.ToDecimal(balance) + Convert.ToDecimal(0.25))
                    {
                        SqlParameter[] PushBalance = new SqlParameter[]
                        {
                            new SqlParameter("@DSCURRENTLEVEL",agent_Id),
                            new SqlParameter("@AgentUserName",ViewState["Userid"].ToString()),
                            new SqlParameter("@TransactionID",RefrenceNo),
                            new SqlParameter("@AMOUNT",requestbalance),
                            new SqlParameter("@Depositedin",""),
                            new SqlParameter("@Remarks",""),
                            new SqlParameter("@IPAddress ",IPAddress),
                            new SqlParameter("@ErrorMessage",SqlDbType.Int)
                        };
                        PushBalance[7].Direction = ParameterDirection.Output;
                        SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.mainConnectionString, CommandType.StoredProcedure, "udp_PushBalance_sms", PushBalance);
                        int result = Convert.ToInt32(PushBalance[7].Value);
                        if (result == 1)
                        {
                            objcommonfun.SendSMS(AgMobileNo, "There is some technical problem .Please try again.", RefrenceNo, AgMobileNo, stringquery);
                        }
                        else
                        {
                            BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                            objcommonfun.SendSMS(AgMobileNo, "TID " + "" + RefrenceNo + " Amount " + requestbalance + " has been transferred to " + ViewState["Userid"].ToString() + " successfully Your current balance is " + BalanceAfterTransaction + " This SMS charge @ 0.25 paisa", RefrenceNo, AgMobileNo, stringquery);
                            //deductamount();
                            BalanceAfterTransaction = objcommonfun.userdetails(ViewState["PartnerID"].ToString());
                            objcommonfun.SendSMSByAdmin(ViewState["MobileNo"].ToString(), "An amount of Rs. " + requestbalance + " credited into your account on " + Convert.ToDateTime(System.DateTime.Now.ToShortDateString()).ToString("dd-MMM-yyyy") + " at " + DateTime.Now.ToString("HH:mm:ss") + " against txn id " + RefrenceNo + " by " + ViewState["Username"].ToString() + " Your Current Balance is " + BalanceAfterTransaction + "", RefrenceNo, ViewState["PartnerID"].ToString());

                        }
                    }
                    else
                    {
                        BalanceAfterTransaction = objcommonfun.userdetails(agent_Id);
                        if (Convert.ToDouble(BalanceAfterTransaction) > 0.25)
                        {
                            deductamount(RefrenceNo);
                            objcommonfun.SendSMS(AgMobileNo, "Dear Partner, You don’t have sufficient balance to do this transaction. Pls take upload in your login This SMS charge @ 0.25 paisa", RefrenceNo, AgMobileNo, stringquery);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //CommonFunction.writeToLogFile(Convert.ToString(ex));
            }
        }
    }
}
