Monday, June 30, 2014

C# Extension Methods Example

Hi in this post i will show how to create extension methods in c#.

Lets understand with the help of an example.

Example :


1. For creating a extension method we have to create a static class and inside that we have to define a static method which will be used as an extension method.


2. In the above example u can see the extension method reverseString() for data type string.
this extension method will reverse a string and then will return us the output.

3. Code & Output :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string Str = "software";
            Console.WriteLine("Reversed String:{0}", Str.reverseString());
            Console.ReadLine();
        }
    }

    public static class ExtensionForString
    {
        public static string reverseString(this string str)
        {
            char[] cArray = str.ToCharArray();
            string reverse = String.Empty;
            for (int i = cArray.Length - 1; i > -1; i--)
            {
                reverse += cArray[i];
            }
            return reverse;
        }
    }
}







Sunday, April 20, 2014

[RESOLVED] Handler "PageHandlerFactory-Integrated" has a bad module "ManagedPipelineHandler" in its module list

Hi here i will show how to resolve this below error which usually occurs when we access a asp.net website hosted over IIS.

Handler "PageHandlerFactory-Integrated" has a bad module "ManagedPipelineHandler" in its module list

1. go to cmd and run as administrator.

2. and type this below code and hit enter. Thus it will resolve ur issue.

%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -i


3.  check the .net version your are using for your website and change the .net version according to it in the          cmd.
     In my case i was using .net 4.0.

Sunday, April 13, 2014

Sample Code to validate from date and to date in JavaScript/jquery || checkin date and checkout date validation using JavaScript(js)

 Javascript :

Below is the code to validate fromdate and todate using javascript.

   function validateDate() {

            var fromdate, todate, date1, date2;
            var chkFrom = document.getElementById('date');
            var chkTo = document.getElementById('date2');

            fromdate = chkFrom.value;
            todate = chkTo.value;

            date1 = new Date(fromdate);
            date2 = new Date(todate);

            if (date2 <= date1) {
                alert("To date Should be greater than From date");

                return false;
            }
            return true
        }
    </script>

Web Page:

calling the above javascript function on button click

 <div>
 <button type="button" id="btnsubmit" class="btn"  onclick="return validateDate();"> Submit</button>
 </div>

Monday, March 31, 2014

Friday, March 28, 2014

encrypt decrypt webconfig connectionStrings from codebehind in asp.net | Secure web config Connection Strings with RSACryptoServiceProvider Class through coding in asp.net

hi in this post i will show how to secure connection strings inside a web.config file from code in asp.net.

1. code to encrypt :

      protected void Encrypt_Click(object sender, EventArgs e)
    {
        string provider = "RSAProtectedConfigurationProvider";
        string section = "connectionStrings";
        Configuration confg = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        ConfigurationSection configSect = confg.GetSection(section);
        if (configSect != null)
        {
            configSect.SectionInformation.ProtectSection(provider);
            confg.Save();
        }
    }

web.config


2. code to decrypt :

    protected void Button2_Click(object sender, EventArgs e)
    {
        string section = "connectionStrings";
        Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        ConfigurationSection configSect = config.GetSection(section);
        if (configSect.SectionInformation.IsProtected)
        {
            configSect.SectionInformation.UnprotectSection();
            config.Save();
        }
    }

web.config