Monday, 19 August 2013

Sms and some basics funtions

eSMSLayer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Net;
using System.IO;
using System.Xml;
using System.Net.Mail;

namespace eSMS.App_Code
{
    public class eSMSLayer
    {
        String requestString = ConfigurationManager.AppSettings["smsAPI"].ToString();

        public eSMSLayer()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public String SendMessage(String message, String mobileNo)
        {
            String strStatus = string.Empty;

            try
            {               
                //requestString = requestString + "&message=" + message + "&recipient=" + mobileNo;
                requestString = requestString + "&password=fa2c31" + "&to=91" + mobileNo + "&message=" + message + "&sender=Arya&priority=3" ;
                Stream SourceStream = null;
                StreamReader objResReader = null;
                String strResponse = string.Empty;

                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestString);
                myRequest.Credentials = CredentialCache.DefaultCredentials;
                HttpWebResponse webResponse = (HttpWebResponse)myRequest.GetResponse();
                if (webResponse.StatusCode == HttpStatusCode.OK)
                {
                    SourceStream = webResponse.GetResponseStream();
                    objResReader = new StreamReader(SourceStream);
                    strResponse = objResReader.ReadLine();
                }

                strStatus = strResponse;

            }
            catch (Exception ex)
            {
                strStatus = "Error : " + ex.Message;
            }
            return strStatus;
        }

        public void SendMail(String msgBody, String mailTo, String mailSubject, HttpPostedFile attachmentFile)
        {
            //string mailFrom = "admineSMS@aryasolution.com";
            string mailFrom = "sureshbabu.karikalan@aryasolution.com";
            try
            {
                Attachment attach = null;
                MailMessage objMailMsg = new MailMessage(mailFrom, mailTo, mailSubject, msgBody);
                SmtpClient emailClient = new SmtpClient("smtp.eu.jllnet.com");
                if (attachmentFile.ContentLength > 0)
                {
                    String strFileName = Path.GetFileName(attachmentFile.FileName);
                    attachmentFile.SaveAs(HttpContext.Current.Server.MapPath(strFileName));
                    attach = new Attachment(HttpContext.Current.Server.MapPath(strFileName));
                    attach.ContentDisposition.FileName = strFileName;
                    objMailMsg.Attachments.Add(attach);
                }
                emailClient.UseDefaultCredentials = true;
                objMailMsg.IsBodyHtml = true;
                emailClient.Send(objMailMsg);
                if (objMailMsg.Attachments.Count > 0)
                {
                    File.Delete(HttpContext.Current.Server.MapPath(attach.ContentDisposition.FileName));

                }
            }
            catch (Exception ex)
            {

            }
        }

        public Boolean IsEligibleToSend()
        {
            KidsLayer objDL = new KidsLayer();
            if (HttpContext.Current.Session["CustomerTypeId"].ToString() == string.Empty)
            {
                DataTable objDT = objDL.Datatable("sp_GetSMSTransactionDetails", HttpContext.Current.Session["CustomerId"].ToString());
                if (objDT.Rows.Count > 0)
                {
                    Int64 smsRemainingCount = Convert.ToInt64(objDT.Rows[0][2].ToString());
                    if (smsRemainingCount > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                return false;
            }
            else
            {
                DataTable objDT = objDL.Datatable("sp_GetSMSTransactionDetails", HttpContext.Current.Session["CustomerTypeId"].ToString());
                if (objDT.Rows.Count > 0)
                {
                    Int64 smsRemainingCount = Convert.ToInt64(objDT.Rows[0][2].ToString());
                    if (smsRemainingCount > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                return false;
            }
        }

        public void UpdateSMSTransactionCount(int smsCount)
        {
            //KidsLayer objDL = new KidsLayer();
            KidsLayer objDL = new KidsLayer();

            if (HttpContext.Current.Session["CustomerTypeId"].ToString() == string.Empty)
            {
                DataTable objDT = objDL.Datatable("sp_GetSMSTransactionDetails", HttpContext.Current.Session["CustomerId"].ToString());
                if (objDT.Rows.Count > 0)
                {
                    Int64 totalSMSCount = Convert.ToInt64(objDT.Rows[0][0].ToString());
                    Int64 sentSMSCount = Convert.ToInt64(objDT.Rows[0][1].ToString());
                    Int64 smsRemainingCount = Convert.ToInt64(objDT.Rows[0][2].ToString());
                    sentSMSCount = sentSMSCount + smsCount;
                    smsRemainingCount = totalSMSCount - sentSMSCount;
                    int status = objDL.ExecuteNonQuery("sp_UpdateTransactionCount", HttpContext.Current.Session["CustomerId"].ToString(), sentSMSCount, smsRemainingCount, HttpContext.Current.Session["CustomerName"].ToString());
                    // write a log the message transaction status
                }
            }
            else
            {
                DataTable objDT = objDL.Datatable("sp_GetSMSTransactionDetails", HttpContext.Current.Session["CustomerTypeId"].ToString());
                if (objDT.Rows.Count > 0)
                {
                    Int64 totalSMSCount = Convert.ToInt64(objDT.Rows[0][0].ToString());
                    Int64 sentSMSCount = Convert.ToInt64(objDT.Rows[0][1].ToString());
                    Int64 smsRemainingCount = Convert.ToInt64(objDT.Rows[0][2].ToString());
                    sentSMSCount = sentSMSCount + smsCount;
                    smsRemainingCount = totalSMSCount - sentSMSCount;
                    int status = objDL.ExecuteNonQuery("sp_UpdateTransactionCount", HttpContext.Current.Session["CustomerTypeId"].ToString(), sentSMSCount, smsRemainingCount, HttpContext.Current.Session["CustomerName"].ToString());
                    // write a log the message transaction status
                }
            }
        }

      
    }
}

Basic functions

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

namespace eSMS.App_Code
{   
    public class KidsLayer
    {
        SqlConnection strConn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConn"].ToString());
        DataTable objDT = new DataTable("tbl");

        public void Conn()
        {
            SqlConnection strConn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConn"].ToString());
        }
  
        public DataTable Datatable(string SpName, params object[] CommandParameters)
        {
            strConn.Open();
            using (SqlCommand comm = new SqlCommand(SpName, strConn))
            {
                comm.CommandText = SpName;
                comm.CommandType = CommandType.StoredProcedure;
                SqlCommandBuilder.DeriveParameters(comm);
                comm.Parameters.RemoveAt(0);
                for (int i = 0, j = comm.Parameters.Count; i < j; i++)
                {
                    comm.Parameters[i].Value = CommandParameters[i];
                }
                SqlDataAdapter objDA = new SqlDataAdapter(comm);
                objDA.Fill(objDT);
                comm.Parameters.Clear();
                strConn.Close();
                return objDT;

            }


        }
        public int ExecuteNonQuery(string SpName, params object[] CommandParameters)
        {

            strConn.Open();
            using (SqlCommand comm = new SqlCommand(SpName, strConn))
            {
                comm.CommandText = SpName;
                comm.CommandType = CommandType.StoredProcedure;
                SqlCommandBuilder.DeriveParameters(comm);
                comm.Parameters.RemoveAt(0);
                for (int i = 0, j = comm.Parameters.Count; i < j; i++)
                {
                    comm.Parameters[i].Value = CommandParameters[i];
                }

                int result = comm.ExecuteNonQuery();
                comm.Parameters.Clear();
                strConn.Close();
                return result;

            }


        }
        public DataSet Dataset(string SpName, params object[] CommandParameters)
        {
            strConn.Open();
            using (SqlCommand cmd = new SqlCommand(SpName, strConn))
            {
                //con.Open();
                cmd.CommandText = SpName;
                cmd.CommandType = CommandType.StoredProcedure;
                SqlCommandBuilder.DeriveParameters(cmd);
                cmd.Parameters.RemoveAt(0);
                for (int i = 0, j = cmd.Parameters.Count; i < j; i++)
                {
                    cmd.Parameters[i].Value = CommandParameters[i];
                }
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                strConn.Close();
                cmd.Parameters.Clear();
                return ds;
            }

        }

    }

}

No comments:

Post a Comment