Friday, April 23, 2010

MD5 test application

On my Default.aspx page i have taken following controls:

1.) Label lblMessage
2.) TextBox txtPalinStr
3.) Button btnEncrypt
4.) Label lblEnryptedString
5.) TextBox txtcheck
6.) Button btncheck onclick="btncheck_Click"
7.) Label lblcheck


This is my Default.aspx.cs page :

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using MD5Encrypt;
public partial class _Default : System.Web.UI.Page
{
    private MD5 obj_MD5;

    protected void Page_Load(object sender, EventArgs e)
    {
        obj_MD5 = new MD5();
    }
    protected void btnEncrypt_Click(object sender, EventArgs e)
    {
        lblEnryptedString.Text = obj_MD5.Encrypt(txtPalinStr.Text);
    }
    protected void btncheck_Click(object sender, EventArgs e)
    {
        lblcheck.Text = obj_MD5.verify(txtcheck.Text);

        if (lblEnryptedString.Text == lblcheck.Text)
        {
            lblMessage.Text = "You Are Authenticated";
        }
        else
        {
            lblMessage.Text = "Authentication Failed";
        }
    }
}


And Finally My MD5 Class :


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

namespace MD5Encrypt
{
    public class MD5
    {
        private Byte[] EncStringBytes;
        private UTF8Encoding Encoder = new UTF8Encoding();
        private MD5CryptoServiceProvider MD5Hasher = new MD5CryptoServiceProvider();
        private static string PasswordSalt;
        string MD5String;
      
        public MD5()
        { }

        public string Encrypt(string EncString)
        {
            Random RanGen = new Random();
            string RanString = "";           
            string RanSaltLoc;          

            while(RanString.Length<=3)
            {
                RanString = RanString + RanGen.Next(0, 9);
            }

            EncStringBytes = Encoder.GetBytes(EncString + RanString);

            EncStringBytes = MD5Hasher.ComputeHash(EncStringBytes);
            MD5String = BitConverter.ToString(EncStringBytes);
            MD5String = MD5String.Replace("-", String.Empty);


            RanSaltLoc = (RanGen.Next(4, MD5String.Length)).ToString();
            MD5String = MD5String.Insert(Convert.ToInt32(RanSaltLoc),RanString);
           
            if (Convert.ToInt32(RanSaltLoc) < 10)
            {
                RanSaltLoc = "0" + RanSaltLoc;
            }

            MD5String = MD5String.Insert(3, RanSaltLoc);
            PasswordSalt = RanSaltLoc + RanString;
            return MD5String;
        }

        public string verify(string chkString)
        {
            string SaltString = PasswordSalt.Substring(2,4);
            string SaltLoaction = PasswordSalt.Substring(0, 2);

            EncStringBytes = Encoder.GetBytes(chkString + SaltString);

            EncStringBytes = MD5Hasher.ComputeHash(EncStringBytes);
            MD5String = BitConverter.ToString(EncStringBytes);
            MD5String = MD5String.Replace("-", String.Empty);
            MD5String = MD5String.Insert(Convert.ToInt32(SaltLoaction), SaltString);
           
            MD5String = MD5String.Insert(3, SaltLoaction);

            return MD5String;
        }
    }
}

Sunday, April 18, 2010

How to generate XML document using Store Procedure in SQL Server

CREATE PROCEDURE  sp_getXML 
AS
BEGIN
    SELECT    C.CategoryID,
            C.CategoryName,
            F.FurnitureID,F.FurnitureName,
            SUM(F.FurniturePrice) FurniturePrice
    FROM tbl_Furniture F
    INNER JOIN tbl_Category  C ON F.CategoryID = C.CategoryID   
    GROUP BY C.CategoryID,C.CategoryName,F.FurnitureID,F.FurnitureName
    ORDER BY C.CategoryID
    FOR XML AUTO, ROOT('Furnitures')
END

Friday, April 16, 2010

How to change color of dates which are greater than current date in Ajax calender extender

The following code will help you ,just put in inside in your page :

Include folowing stylesheet in your page :


      .ajax_Ena div
      {
        background:gray;
        cursor:text;   
      }
    

Include following code inside script :

     AjaxControlToolkit.CalendarBehavior.prototype._performLayout = function()
            {                      
                             
                 var elt = this.get_element();                                                                                  
                 if (!elt) return;
                 else
                 {
                    if (!this.get_isInitialized()) return;  
                    if (!this._isOpen) return;              
                    var dtf = Sys.CultureInfo.CurrentCulture.dateTimeFormat;
                                       
                    var selectedDate = this.get_selectedDate();  
                    var visibleDate = this._getEffectiveVisibleDate();  
                    var todaysDate = this.get_todaysDate();                             
                         switch (this._mode)
                         {  
                             case "days":  
                                   
                                 var firstDayOfWeek = this._getFirstDayOfWeek();  
                                 var daysToBacktrack = visibleDate.getDay() - firstDayOfWeek;  
                                 if (daysToBacktrack <= 0)  
                                     daysToBacktrack += 7;  
                                       
                                 var startDate = new Date(visibleDate.getFullYear(), visibleDate.getMonth(), visibleDate.getDate() - daysToBacktrack, this._hourOffsetForDst);                   
                                 var currentDate = startDate;                            
                                  
                                 for (var i = 0; i < 7; i++)
                                 {  
                                     var dayCell = this._daysTableHeaderRow.cells[i].firstChild;  
                                     if (dayCell.firstChild)
                                     {  
                                         dayCell.removeChild(dayCell.firstChild);  
                                     }  
                                     dayCell.appendChild(document.createTextNode(dtf.ShortestDayNames[(i + firstDayOfWeek) % 7]));  
                                 }  
                                
                                 for (var week = 0; week < 6; week ++)
                                 {  
                                     var weekRow = this._daysBody.rows[week];  
                                     for (var dayOfWeek = 0; dayOfWeek < 7; dayOfWeek++)
                                     {  
                                         var dayCell = weekRow.cells[dayOfWeek].firstChild;  
                                         if (dayCell.firstChild)
                                         {  
                                             dayCell.removeChild(dayCell.firstChild);  
                                         }  
                                         dayCell.appendChild(document.createTextNode(currentDate.getDate()));  
                                         dayCell.title = currentDate.localeFormat("D");  
                                         dayCell.date = currentDate;                                                                                                     
                                         $common.removeCssClasses(dayCell.parentNode, [ "ajax__calendar_hover" ,"ajax__calendar_other", "ajax__calendar_active"]);                       
                                        
                                     
                                         if(elt.id.toLowerCase().indexOf('sdate') > -1)
                                         {
                                            var today_date = ('<%=GetTodayDate(1) %>');                                                                                                
                                            if(currentDate > new Date())
                                            {
                                                Sys.UI.DomElement.addCssClass(dayCell.parentNode,"ajax_Ena");                                                                                                                      
                                            }
                                            else
                                            {
                                            $common.removeCssClasses(dayCell.parentNode,["ajax_Ena"]);
                                            }
                                          }                                          
                                         currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate() + 1, this._hourOffsetForDst);  
                                     }  
                                 }  
                                   
                                 this._prevArrow.date = new Date(visibleDate.getFullYear(), visibleDate.getMonth() - 1, 1, this._hourOffsetForDst);  
                                 this._nextArrow.date = new Date(visibleDate.getFullYear(), visibleDate.getMonth() + 1, 1, this._hourOffsetForDst);  
                                 if (this._title.firstChild)
                                 {  
                                     this._title.removeChild(this._title.firstChild);  
                                 }  
                                 this._title.appendChild(document.createTextNode(visibleDate.localeFormat("MMMM, yyyy")));  
                                 this._title.date = visibleDate;  
                   
                                 break;  
                             case "months":                   
                                 for (var i = 0; i < this._monthsBody.rows.length; i++)
                                 {  
                                     var row = this._monthsBody.rows[i];  
                                     for (var j = 0; j < row.cells.length; j++)
                                     {  
                                         var cell = row.cells[j].firstChild;  
                                         cell.date = new Date(visibleDate.getFullYear(), cell.month, 1, this._hourOffsetForDst);  
                                         cell.title = cell.date.localeFormat("Y");                                                              
                                         $common.removeCssClasses(cell.parentNode, [ "ajax__calendar_other", "ajax__calendar_active" ]);  
                                         Sys.UI.DomElement.addCssClass(cell.parentNode, this._getCssClass(cell.date, 'M'));  
                                     }  
                                 }  
                                   
                                 if (this._title.firstChild)
                                 {  
                                     this._title.removeChild(this._title.firstChild);  
                                 }  
                                 this._title.appendChild(document.createTextNode(visibleDate.localeFormat("yyyy")));  
                                 this._title.date = visibleDate;  
                                 this._prevArrow.date = new Date(visibleDate.getFullYear() - 1, 0, 1, this._hourOffsetForDst);  
                                 this._nextArrow.date = new Date(visibleDate.getFullYear() + 1, 0, 1, this._hourOffsetForDst);  
                   
                                 break;  
                             case "years":  
                   
                                 var minYear = (Math.floor(visibleDate.getFullYear() / 10) * 10);  
                                 for (var i = 0; i < this._yearsBody.rows.length; i++)
                                 {  
                                     var row = this._yearsBody.rows[i];  
                                     for (var j = 0; j < row.cells.length; j++)
                                     {  
                                         var cell = row.cells[j].firstChild;  
                                         cell.date = new Date(minYear + cell.year, 0, 1, this._hourOffsetForDst);  
                                         if (cell.firstChild)
                                         {  
                                             cell.removeChild(cell.lastChild);  
                                         }
                                         else
                                         {  
                                             cell.appendChild(document.createElement("br"));  
                                         }  
                                         cell.appendChild(document.createTextNode(minYear + cell.year));  
                                         $common.removeCssClasses(cell.parentNode, [ ".ajax__calendar_other", ".ajax__calendar_active" ]);  
                                         Sys.UI.DomElement.addCssClass(cell.parentNode, this._getCssClass(cell.date, 'y'));  
                                     }  
                                }  
                   
                                if (this._title.firstChild)
                                {  
                                    this._title.removeChild(this._title.firstChild);  
                                }  
                                this._title.appendChild(document.createTextNode(minYear.toString() + "-" + (minYear + 9).toString()));  
                                this._title.date = visibleDate;  
                                this._prevArrow.date = new Date(minYear - 10, 0, 1, this._hourOffsetForDst);  
                                this._nextArrow.date = new Date(minYear + 10, 0, 1, this._hourOffsetForDst);  
                   
                            break;  
                         }  
                         if (this._today.firstChild)
                         {  
                             this._today.removeChild(this._today.firstChild);  
                         }  
                         this._today.appendChild(document.createTextNode(String.format(AjaxControlToolkit.Resources.Calendar_Today, todaysDate.localeFormat("MMMM d, yyyy"))));  
                         this._today.date = todaysDate;                                                           
                 }             
            }


 


If you have any query than just mail me or post comment....

How to disable future dates in Ajax Calender Extender Control


The following Code will help you to solve problem :

AjaxControlToolkit.CalendarBehavior.prototype._cell_onclick_original = AjaxControlToolkit.CalendarBehavior.prototype._cell_onclick;          
            AjaxControlToolkit.CalendarBehavior.prototype._cell_onclick = function(e)
            {                                   
                var target = e.target;                                              
                if(target.id.toLowerCase().indexOf('sdate') > -1)
                {   
                    var today_date = new Date();                                   
                    if (target.date > today_date)                                         
                    {
                        this._selectedDateChanging=false;
                        return;                                           
                    }
                }
                this._cell_onclick_original(e);
            } 

 
In next post I will show how to change color of future  dates in calender extender control...