mobile ads

Monday, 25 November 2013

Sending SMS from ASP.NET

Write a method in class file

public static void CreateRequestTOSENDSMS()
        {
            string url = "SMS gateway url, which will be provided by SMS people";//      HttpContext.Current.Request.Url.AbsoluteUri.ToString().Replace("AutoLogin", "Login");
            CookieContainer myCookieContainer = new CookieContainer();
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.CookieContainer = myCookieContainer;
            request.Method = "GET";
            request.KeepAlive = false;
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            System.IO.Stream responseStream = response.GetResponseStream();
            System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
            string srcString = reader.ReadToEnd();
             // Concat the string data which will be submit
            string formatString ="username={0}&pass={1}&senderid={2}&mtype=txt&tempid={3}&f1={4}&f2={5}&f3={6}&dest_mobileno={7}&response=Y";
            string postString = string.Format(formatString, "demo12", "demo123", "DEMOVL", "1018", "1234", "5673", "tyyty");
            byte[] postData = Encoding.ASCII.GetBytes(postString);

            // Set the request parameters
            request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            request.Referer = url;
            request.KeepAlive = false;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; CIBA)";
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = myCookieContainer;
            System.Net.Cookie ck = new System.Net.Cookie("SMS", "Value of SMS cookie");
            ck.Domain = request.RequestUri.Host;
            request.CookieContainer.Add(ck);
            request.CookieContainer.Add(response.Cookies);
            // Submit the request data
            System.IO.Stream outputStream = request.GetRequestStream();
            request.AllowAutoRedirect = true;
            outputStream.Write(postData, 0, postData.Length);
            outputStream.Close();
            // Get the return data
            response = request.GetResponse() as HttpWebResponse;
            responseStream = response.GetResponseStream();
            reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
            srcString = reader.ReadToEnd();
            }

The parameters "url", "formatstring" may vary from one SMS provider to another. Add these namespaces.
using System.Text;
using System.IO;
using System.Net;

Why we use Static Keyword?

The static keyword defines, that the class or property or method we declare as static does not require a previous instance of an object. In the other hand, a static method for example cannot use any instance method / instance property of the own object.

Also static defined things will be optimized by the compiler, for example that a static object is only written once in the memory and everything accesses to the same object. When we use static keyword it means there is no need to create an instance of object . Static class including a static method can called by class name. Static class can have only static methods.

Friday, 22 November 2013

Why .NET

Few points to answer why .NET
  1.   In .NET we have language choice to code with (C#,VB.NET, Java,Boo,Python e.t.c), producing the same type of compiled code.
  2.  .NET programs run at native speed while java is interpreted which makes java slower.Although java has Just In Time compilation but it stills run slower. With .NET you are not limited to JIT but have the option for AOT(Ahead of time) compilation if you want to eliminate startup delays.
  3. Calling native code in java is not a very clean process. You need to generate some stub files which makes the whole process cumbersome and dirty. In .NET you just declare that you are calling a native function from a specified library and just start calling it. 
  4. .NET languages are richer than Java. They have object oriented feature that are absent in java e.g properties,delegates,generics.
  5.  Java GUI programs look alien on the host operating system. Even if you use the OS's theme you still notice that the java widgets look out of place.
  6.  .NET in the form of Mono has brought a whole revolution on the linux desktop in form of great applications like beagle, tomboy, diva, iFolder, banshee e.t.c. This is something that java has failed to do despite the fact that it's been there long before .NET
  7. Many programs that would have been difficult to develop with java have been developed with .NET things like compilers (Mono's C# and VB.NET) 3D game engines (unity game engine) e.t.c
  8.  The CLI is an open standard maintained by an independent standards organization (E.C.M.A) while java is still governed by SUN microsystems.Even though java has recently been open-sourced, it's future will still be highly influenced by SUN.
  9. You can code on the .NET platform using Java but you cannot code on Java platform using any of the .NET languages.

Thursday, 14 November 2013

How to use Ajax calendar in ASP.NET


Step 1: Register ajax dll

Add this line after @Page directive.
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxtoolkit" %>

Step 2 : Include scriptmanager in your code
 <asp:ScriptManager ID="scrp" runat="server">    </asp:ScriptManager>

Step 3 : Next
<asp:TextBox ID="txtDOB" runat="server" />
                                                        <asp:Image ID="imgDOB" runat="server" Height="22px" ImageUrl="~/images/calendar.JPG"
                                                            Width="26px" />
                                                        <ajaxtoolkit:CalendarExtender ID="calDOB" runat="server" PopupButtonID="imgDOB" TargetControlID="txtDOB">
                                                        </ajaxtoolkit:CalendarExtender>

Ajax calendar control has two main properties, PopupButtonID and TargetControlID.

When we click on the image button a calendar will be shown and selected date will be displayed in textbox.

For this to work you need to add AjaxControlToolKit dll.

Wednesday, 6 November 2013

PageIndexChanging Vs PageIndexChanged


PageIndexChanging
- Occurs when one of the pager buttons is clicked, but before the GridView control handles the paging operation. This event is often used to cancel the paging operation. 

Ex:
protected void gvTeachers_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                gvTeachers.PageIndex = e.NewPageIndex;
               //bind the grid
            }
            catch (Exception ex)
            {
                lblErrorMessage.Visible = true;
                lblErrorMessage.Text = ex.Message;
                log.Error(ex.Message + " "+ex.StackTrace);
            }
        }


PageIndexChanged
- Occurs when one of the pager buttons is clicked, but after the GridView control handles the paging operation. This event is commonly used when you need to perform a task after the user navigates to a different page in the control.

PageIndexChanged is fired after the actual page index is changed, but before the GridView is databound again with the new page data.

PageIndexChanging fired prior to the change and the PageIndexChanged fired after the new data for the next page is displayed.