Tuesday, July 12, 2011

Code to send SMS using webservice like way2sms

Download the new dll for way2sms :


and this is the code :

CookieContainer cookie = SMSClientLib.Login.Connect("mobileno.", "Password", out status);
string[] siteParameters = SMSClientLib.Login.GetSiteParameters(cookie);
string messgeSentResult = SendSMS.Send_Processing("RecMobile", "Message!", cookie, siteParameters);
 
Its Working...!!

Practical example of Abstract class

using System;
 
namespace AbstractClasses
{
    public abstract class Messager
    {
        protected string _recipient;
        protected string _message;
        public Messager(string recipient, string message)
        {
            _recipient = recipient;
            _message = message;
        }
        public void ProcessMessage()
        {
            LogMessageLength();
            // the code from here
            var isValid = false;
            if (ValidateRecipient())
            {
                isValid = true;
                SendMessage();
            }
            // to here will be implemented by the concrete classes
            if (isValid)
                NotifyTheServiceOwner("The message was correctly sent");
            else
                NotifyTheServiceOwner("The message was NOT sent");
        }
        protected abstract void SendMessage();
        protected abstract bool ValidateRecipient();
        private void LogMessageLength()
        {
            // log the message
            Console.WriteLine("Sending a message of size: {0}", _message.Length);
        }
        private void NotifyTheServiceOwner(string note)
        {
            // notify the service owner via email
        }
    }
    public class SmsMessager : Messager
    {
        public SmsMessager(string _recipient, string _message)
            : base(_recipient, _message)
        { }
        protected override bool ValidateRecipient()
        {
            // validate the phone number
            return true;
        }
        protected override void SendMessage()
        {
            // use some SMS service to send the message
        }
    }
    public class EmailMessage : Messager
    {
        public EmailMessage(string _recipient, string _message)
            : base(_recipient, _message)
        { }
        protected override bool ValidateRecipient()
        {
            // validate the email address
            return true;
        }
        protected override void SendMessage()
        {
            // use the .Net classe to send an email
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            var messagers = new Messager[]
            {
                new EmailMessage("adrian@domain.com","Hello"),
                new SmsMessager("+4074312122", "Hi"),
                new EmailMessage("rahul@domain2.com", "Hello rahul")
            };
            foreach (var messager in messagers)
            {
                messager.ProcessMessage();
            }
        }
    }
}

Friday, July 8, 2011

Exploring Session in ASP.Net

Nice Article at the following link in CodeProject:

http://www.codeproject.com/KB/aspnet/ExploringSession.aspx#21

Many many thanks to Mr. Abhijit Jana for such an informative and fantastic article.

Both Session.Clear() and Session.RemoveAll() does the same job.
"Removes All Keys and Values from the Session state collection."

Then what is the difference between these two ?
Session.RemoveAll() Calls Clear() method internally. Yes it is.

Now, what does this two methods do ?
  • They  removes all keys and values from the session-state collection . which means just remove the session items nothing more than that and session ID Will remain Same.

  • Session.Clear() and Session.RemoveAll() never raised Session_End() . Session_End raised by Session.abandon() and if we are not explicitly calling the Abandon(), then Session_End will fire on SessionTimeOut.
  • Abandon method destroys all the objects stored in a Session object and releases their resources. Once you call this method, Session_End will raised automatically. 
  • When you abandon a session, the session ID cookie is not removed from the browser of the user. Therefore, as soon as the session has been abandoned, any new requests to the same application will use the same session ID but will have a new session state instance.
"If we don’t call Abandon method explicitly, the server destroys these objects when the session times out.  Which means, for Session.Clear() and Session.RemoveAll(), just session data get removed, but session still alive, but Session.Abandon() Destroy the all object."

The main difference with Session.Clear() / Session.RemoveAll()  and Session.Abandon()
  • Clear removes items immediately from session collection, Abandon destroy the objects.
  • Abandon raised Session.End()
  • Clear keeps SessionState and resources associated with it where as abandon releases them and GC can collect them any time.

 

Thursday, July 7, 2011

Maintain GridView Scroll Position and Header Inside Update Panel

http://www.codeproject.com/KB/aspnet/AjaxScroll.aspx

How to Freeze GridView Header While Scrolling ?

This is very common problem in Web development that we need to freeze the GridView header at the time of scrolling, Here is one simple tips

Step 1 : Create a CSS class as following

.HeaderFreez
{
position:relative ;
top:expression(this.offsetParent.scrollTop);
z-index: 10;
}
Step 2 Set Gridview’s HeaderStyle CssClass as follows

CssClass="HeaderFreez"

 

How to find HashTable Key from HashTable value

Here is the code:

public class CustomHashtable:Hashtable
{
public CustomHashtable()
{
//
// TODO: Add constructor logic here
//
}

public bool ContainsValue(object Value, out int key)
{
if (this.ContainsValue(Value))
{
//iterate and get the key
foreach (int aKey in Keys)
{
if (this[aKey].Equals(Value)==true)
{
key=aKey;
return true;
}
}
}
else
{
key = -1;
return false;
}
key = -1;
return false;

}
}

Saturday, November 22, 2008

I would like to share some stuff related to ASP.Net technology. As these links have helped me a lot during my learning phase , it would definately help novice programmers.