Welcome

Hello, Welcome to my blog. If you like feel free to refer others

Tuesday 3 September 2013

Remove cache in asp.net

The following code will remove all keys from the cache:
public void ClearApplicationCache(){
    List<string> keys = new List<string>();
    // retrieve application Cache enumerator
    IDictionaryEnumerator enumerator = Cache.GetEnumerator();
    // copy all keys that currently exist in Cache
    while (enumerator.MoveNext()){
        keys.Add(enumerator.Key.ToString());
    }
    // delete every key from cache
    for (int i = 0; i < keys.Count; i++) {
        Cache.Remove(keys[i]);
    }
}


Modifying the second loop to check the value of the key before removing it should be trivial.
Hope this helps.

Wednesday 31 July 2013

Asp.net Show Message box after saving the data into db server(sql)

I have a common class and one of my method is this one
using System.Web;
using System.Text;
using System.Web.UI;

/// <summary> 
/// A JavaScript alert 
/// </summary> 
public static class ShowAlertMessage
{

    /// <summary> 
    /// Shows a client-side JavaScript alert in the browser. 
    /// </summary> 
    /// <param name="message">The message to appear in the alert.</param> 
    public static void Show(string message,string pageurl)
    {
        // Cleans the message to allow single quotation marks 
        string cleanMessage = message.Replace("'", "\\'");
        string script = "<script type=\"text/javascript\">alert('" + cleanMessage + "');window.location='" + pageurl + "'</script>";

        // Gets the executing web page 
        Page page = HttpContext.Current.CurrentHandler as Page;

        // Checks if the handler is a Page and that the script isn't allready on the Page 
        if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
        {
            page.ClientScript.RegisterClientScriptBlock(typeof(ShowAlertMessage), "alert", script);
        }
    }

    public static void Show(string message)
    {
        // Cleans the message to allow single quotation marks 
        string cleanMessage = message.Replace("'", "\\'");
        string script = "<script type=\"text/javascript\">alert('" + cleanMessage + "');</script>";

        // Gets the executing web page 
        Page page = HttpContext.Current.CurrentHandler as Page;

        // Checks if the handler is a Page and that the script isn't allready on the Page 
        if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
        {
            page.ClientScript.RegisterClientScriptBlock(typeof(ShowAlertMessage), "alert", script);
        }
    }
}
then any time i want to display a message i just call that method from page like this.
ShowAlertMessage.Show("Data has been saved!");
hope that help.


Happy learning :)

Monday 17 June 2013

Printing pyramid like stars using C#

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Enumerable.Range(1, 7).Aggregate(new StringBuilder(), (sb, i) => sb.AppendLine(new string('*', 7 - 2 * Math.Abs(i - 4)).PadLeft(7 - Math.Abs(i - 4)))));
            Console.ReadLine();
        }
    }   
}

Output:






Wednesday 24 April 2013

Use getElementById in Javascript to iterate through table

var SelectedRowsCount = new Array();
var AllRowsCount = new Array();
var HeaderCheckBox = new Array();

function CountCheckedRowsByCol(detailTableName, colNum)
{
    AllRowsCount[colNum] = 0;
    SelectedRowsCount[colNum] = 0;
   
    var detailTable = document.getElementById(detailTableName);
   
    if (detailTable == null)
        return;

    var Rows = detailTable.getElementsByTagName("tr");

    if (colNum == null)
        colNum = 0;

    for (i = 0; i < Rows.length; i++)
    {
        var cols = Rows[i].getElementsByTagName("td");
       
        if (cols.length <= colNum)
            continue;
       
        var chks = cols[colNum].getElementsByTagName("input");
       
        if (chks.length == 0)
            continue;
        if (chks[0].type != "checkbox")
            continue;
       
        if (AllRowsCount[colNum] == 0)
        {
            if (HeaderCheckBox[colNum] == null)
                HeaderCheckBox[colNum] = chks[0];
        }
        else
        {
            if (chks[0].checked)
                SelectedRowsCount[colNum]++;
        }
       
        AllRowsCount[colNum]++;
    }
   
    AllRowsCount[colNum]--;
   
//    alert(AllRowsCount[colNum]);
   
    //CheckHeaderIfAllColChkBoxSelected(colNum);
}

Thursday 28 February 2013

Getting Access Denied error when using XMLHttpRequest in javascript on IE


This error could happen due to cross domain issue, use relative path in Server Url if using absolute, also you may try changing Internet Explorer Trusted Site Zones settings (Internet Explorer Options->Security-> Trusted Sites (if your Url added into Trusted Sites, if added into Local intranet then choose Local Intranet) -> Custom Level) , scroll down and change Access data sources across domains value to Enable

If the above does not solve problem then you may deploy clientaccesspolicy and crossdomainpolict xml to resolve the issue:http://msdn.microsoft.com/en-us/library/cc197955(v=vs.95).aspx




 Happy Learning.....