Friday, 9 January 2015

javascript setTimeout function


 JavaScript has setTimeout() method which calls a function or evaluates an expression after a specified number of milliseconds. See below code

setTimeout(function () { alert("Hello") }, 3000);

Above code will display an alert after 3 seconds. Let's take another example,

setTimeout(FuncDemo(), 3000);

You must be thinking that "FuncDemo()" will be executed after 3 seconds. No, You are wrong. The "FuncDemo()" will be executed immediately, without a delay of 3 seconds. Then what setTimeout is doing over here?

Well, We need to remove the parenthesis after  "FuncDemo" . Keeping the parentheses invokes the function immediately. The reason is that the first argument to setTimeout should be a function reference, not the return value of the function. So the correct code is,

setTimeout(FuncDemo, 3000);

But then you must be thinking how would you pass arguments to function (if function is expecting arguments)? Well, If you want to pass parameters to the function, you will have to call an anonymous function which in turn will call your function.

setTimeout(function () {
        FuncDemo('hello');
    }, 3000);


Monday, 5 January 2015

How to send free SMS from ASP.Net application to Mobile

In this article I am explain how to send Free SMS and Free Bulk SMS to mobile (cell) numbers in ASP.Net website. There are many websites that allow sending free SMS to mobile (cell) numbers like Way2sms, FullOnSMS, 160by2, Site2SMS, IndyaRocks.com SMSAbc.com, Ultoo.com, SmsSpark.com SMSFI.com Freesms8, Sms440, BhokaliSMS, etc.

The above sites allow users to send Free SMS to any Mobile or Cell Number.
Thus to make it easier for the .Net Community I have introduced an API for sending SMS using Site2SMS which uses the online API from Mashape.com.
In order to use this API you need to

1. Register at Site2SMS and get Username and Password.

2. Register at Mashape.com and generate key.
3. Download DLL Here.

Designer Source Code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <table border="0">
        <tr>
            <td>
                Your Number:
            </td>
            <td>
                <asp:TextBox ID="txtNumber" runat="server"></asp:TextBox>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required"
                    ControlToValidate="txtNumber" ForeColor="Red"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td>
                Password:
            </td>
            <td>
                <asp:TextBox ID="txtPassword" TextMode="Password" runat="server"></asp:TextBox>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Required"
                    ControlToValidate="txtPassword" ForeColor="Red"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td>
                Recipient Number:
            </td>
            <td>
                <asp:TextBox ID="txtRecipientNumber" runat="server" Width="300"></asp:TextBox>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Required"
                    ControlToValidate="txtRecipientNumber" ForeColor="Red"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td>
                Message:
            </td>
            <td>
                <asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine"></asp:TextBox>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Required"
                    ControlToValidate="txtMessage" ForeColor="Red"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                <asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" />
            </td>
            <td>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net;
using System.IO;
using ASPSnippets.SmsAPI;

public partial class CS : System.Web.UI.Page
{
    protected void btnSend_Click(object sender, EventArgs e)
    {
        SMS.APIType = SMSGateway.Site2SMS;
        SMS.MashapeKey = "<Mashape API Key>";https://www.mashape.com
        SMS.Username = txtNumber.Text.Trim();
        SMS.Password = txtPassword.Text.Trim();
        if (txtRecipientNumber.Text.Trim().IndexOf(",") == -1)
        {
            //Single SMS
            SMS.SendSms(txtRecipientNumber.Text.Trim(), txtMessage.Text.Trim());
        }
        else
        {
            //Multiple SMS
            List<string> numbers = txtRecipientNumber.Text.Trim().Split(',').ToList();
            SMS.SendSms(numbers, txtMessage.Text.Trim());
        }
    }
}


Please Like and Share this article ...