Friday, 14 September 2012

ASP.NET Life Cycle Overview

How to Block the Page, When Data is Submitting.

Introduction:
To Block the Page when Data is Submitting, we have various options, Either we can use Ajax based UpdateProgress or some jQuery Stuff. But sometime Ajax UpdateProgress not very useful because of complexity. So, the better approch appoach is to use jQuery UI Block Plug-In.
Implementation:
Download jQuery UI Block Plugin from : http://www.malsup.com/jquery/block/
<script type="text/javascript" src="js/jquery.1.4.2.js"></script>
<script type="text/javascript" src="js/jquery.blockUI.js"></script>

  $("#<%= btnSubmit.ClientID%>").click(function() {
 $.blockUI({
                            message: "<h3>Processing, Please Wait...</h3>" ,
                            css: {
                                border: 'none',
                                padding: '15px',
                                backgroundColor: '#000',
                                '-webkit-border-radius': '10px',
                                '-moz-border-radius': '10px',
                                opacity: .5,
                                color: '#fff'
                 }  });

});
The above code is simple code without any AJAX or other complex script.
Ref:  http://www.malsup.com/jquery/block/

Friday, 7 September 2012

insert data Though XML in SQL / how to using sp_xml_preparedocument

Create New Table : 

CREATE TABLE [dbo].[tblCommentBox](
    [commentBoxID] [int] IDENTITY(1,1) NOT NULL,
    [commentBoxTitle] [varchar](max) NULL,
    [commentBoxDesc] [nvarchar](max) NULL,
    [created] [datetime] NULL,
    [modified] [datetime] NULL,
    [commentStatus] [bit] NULL,
    [BoxPositionX] [int] NULL,
    [BoxPositionY] [int] NULL,
    [boxHeight] [int] NULL,
    [boxWidth] [int] NULL
) ON [PRIMARY]


create table tblCommentNetwork (
    commentNetworkId int identity(1,1),networkId int,
    commentBoxID int )


Create Store Procedure to insert : 

create proc [dbo].[bsp_commentBox_InsertUpdate_ByXML]
@RetrieveXMLDoc xml   
AS
declare @docHandler int
exec sp_xml_preparedocument @docHandler output,@RetrieveXMLDoc
begin tran
    declare @isError nvarchar(max)='0'
    declare @isCount int=0
   
    /* tblCommentBox */
    declare @AddEdit nvarchar(1)
    declare @commentBoxID int
    declare @commentBoxTitle varchar(max)
    declare @commentBoxDesc nvarchar(max)=''
    declare @commentStatus bit
    declare @BoxPositionX int
    declare @BoxPositionY int
    declare @boxHeight int
    declare @boxWidth int

    select    @commentBoxID        =    _commentBoxID,
            @commentBoxTitle    =    _commentBoxTitle,
            @commentBoxDesc        =    _commentBoxDesc,
            @commentStatus        =    _commentStatus,
            @BoxPositionX        =    _BoxPositionX,
            @BoxPositionY        =    _BoxPositionY,
            @boxHeight            =    _boxHeight,
            @boxWidth            =    _boxWidth
           
        from openxml (@docHandler,'NewDataSet/CommentBox',2)
                with(_commentBoxID int,_commentBoxTitle varchar(max),_commentBoxDesc nvarchar(max), _commentStatus int,
                            _BoxPositionX int, _BoxPositionY int, _boxHeight int, _boxWidth int)
       
        if (@commentBoxID = 0)
        begin
            insert into tblCommentBox (commentBoxTitle,commentBoxDesc,created,modified,commentStatus,BoxPositionX,BoxPositionY,boxHeight,boxWidth)
                    values (@commentBoxTitle,@commentBoxDesc,getdate(),getdate(),@commentStatus,@BoxPositionX,@BoxPositionY,@boxHeight,@boxWidth)
            set @commentBoxID = @@identity
            set @isError = @@ERROR
        end
        else
        begin           
            update tblCommentBox set commentBoxTitle = @commentBoxTitle, commentBoxDesc = @commentBoxDesc, commentStatus = @commentStatus,
                                    BoxPositionX = @BoxPositionX, BoxPositionY = @BoxPositionY, boxHeight = @boxHeight, boxWidth = @boxWidth
                where commentBoxID = @commentBoxID
            set @isError = @@ERROR
        end   
               
               
        if @isError = '0'   
        begin /*  Comment-Network */           
                delete from tblCommentNetwork where commentBoxID = @commentBoxID
                declare @networkId int
                   
                select identity(int,1,1)SNo,_networkId into #tempCommentNetwork
                    from openxml (@docHandler,'NewDataSet/CommentBox/networkCommentBox',2) with(_networkId int)
               
                    declare @NewSNo int
                    set @NewSNo =1
                    select    @networkId    =    _networkId from #tempCommentNetwork where SNo = @NewSNo
                   
                        while @@rowcount > 0
                        begin
                            Insert into tblCommentNetwork(networkId,commentBoxID) Values(@networkId,@commentBoxID)
                            set @isError = @@error
                            if @isError <> '0'
                                begin
                                        break
                                end   
                            set @NewSNo = @NewSNo + 1
                            select    @networkId    =    _networkId from #tempCommentNetwork where SNo = @NewSNo
                        end /*  While End */
        end /* Comment-Network End */
               
       
    if @isError = '0'
    begin
        select 'DS102'
        commit tran
    end
    else

    begin
        select @isError
        rollback tran
    end


XML :

bsp_commentBox_InsertUpdate_ByXML
'<NewDataSet>
  <CommentBox>
    <_commentBoxID>0</_commentBoxID>
    <_commentBoxTitle>Frontech Mouse</_commentBoxTitle>
    <_commentStatus>1</_commentStatus>
    <_boxHeight>100</_boxHeight>
    <_boxWidth>100</_boxWidth>
    <_BoxPositionX>0</_BoxPositionX>
    <_BoxPositionY>0</_BoxPositionY>
    <_commentBoxDesc>Test Description</_commentBoxDesc>
    <networkCommentBox>
      <_commentNetworkId>0</_commentNetworkId>
      <_networkId>11</_networkId>
      <_commentBoxID>0</_commentBoxID>
    </networkCommentBox>
    <networkCommentBox>
      <_commentNetworkId>0</_commentNetworkId>
      <_networkId>22</_networkId>
      <_commentBoxID>0</_commentBoxID>
    </networkCommentBox>
  </CommentBox>
</NewDataSet>'


Wednesday, 5 September 2012

How to create log file in dot net

using (StreamWriter sw = new StreamWriter(@"log_123.txt", true))
        {
            sw.Write("Content type here.......");
        }

How to get previous page content/full html on next/current page

  Page1.aspx :

<table border="1">
            <tr>
                <th>
                    S.No
                </th>
                <th>
                    Name
                </th>
            </tr>
            <tr>
                <td>
                    1
                </td>
                <td>
                    <span style='color: red'>Hello, Mr Bhasker </span>
                </td>
            </tr>
            <tr>
                <td>
                    2
                </td>
                <td>
                    <span style='color: red'>Hello, Mr Trilok </span>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Button ID="Button1" runat="server" PostBackUrl="~/Default3.aspx" Text="Next Page" />
                </td>
            </tr>
        </table>


Page2.aspx :

 <div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />   
    </div>



Page2.aspx.cs :

protected void Button1_Click(object sender, EventArgs e)
    {
        System.IO.StringWriter Orderwriter = new System.IO.StringWriter();
        System.Web.HttpContext.Current.Server.Execute("Page1.aspx", Orderwriter);
        string mBody = Orderwriter.ToString();
    }

Monday, 3 September 2012

checked unchecked all checkboxs in gridview through jquery

jQuery Function : 

 function toggleChecked() {
            $("table#"tableManageProjects"").find(":checkbox").each(function () {
                $(this).attr("checked", true);
            })
        }
        function toggleUnChecked() {
            $("table#"tableManageProjects"").find(":checkbox").each(function () {
                $(this).attr("checked", false);
            })
        }       

HTML & Call Funcation : 

         <a href="#" onclick="javascript:toggleChecked();return false;">All</a>
         <a href="#" onclick="javascript:toggleUnChecked();return false;">None</a>

 <asp:GridView ID="RptProjectList" runat="server" >  
    <ItemTemplate>
        <table id="tableManageProjects">
            <tr>
                <td>
                    <asp:CheckBox ID="chkFavourate" runat="server" />
                </td>
                <td>
                    <asp:Label ID="lblCompany" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Company")%>'></asp:Label>
                 </td>
             </tr>
        </table>   
    </ItemTemplate>
</asp:GridView>

How to Encryption decryption password

Method (1) :

NameSpaces : 
using System.Security.Cryptography;
using System.Text;
using System.IO;

HTML :

  <p>
        Current Guid :&nbsp;
        <asp:Label ID="lblGuid" runat="server" ></asp:Label>
    </p>
<p>
        User Password :
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </p>
<p>
        <asp:Button ID="btnEncryption" runat="server"
            Text="Encryption" onclick="btnEncryption_Click" />
        <asp:Button ID="btnDecryption" runat="server"
            Text="Decryption" onclick="btnDecryption_Click" />
    </p>
<p>
        Encrypting Password :
        <asp:Label ID="lblEncrypting" runat="server" ></asp:Label>
    </p>
<p>
        Decrypting Password :
        <asp:Label ID="lblDecrypting" runat="server" ></asp:Label>
    </p>


Code-Behind :

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Guid passwordSalt = System.Guid.NewGuid();
            lblGuid.Text = passwordSalt.ToString();
        }
    }

    protected void btnEncryption_Click(object sender, EventArgs e)
    {
        lblEncrypting.Text = Encryption_decryption.Encrypt(TextBox1.Text, Convert.ToString(lblGuid.Text));
    }

    protected void btnDecryption_Click(object sender, EventArgs e)
    {
        lblDecrypting.Text = Encryption_decryption.Decrypt(TextBox1.Text, Convert.ToString(lblGuid.Text));
    }


Encryption-Decryption Class :

  public class Encryption_decryption
    {
        public static string Decrypt(string TextToBeDecrypted, string SaltContaint)
        {
            RijndaelManaged RijndaelCipher = new RijndaelManaged();

            //string SaltContaint = "CSC";
            string DecryptedData;

            try
            {
                byte[] EncryptedData = Convert.FromBase64String(TextToBeDecrypted);

                byte[] Salt = Encoding.ASCII.GetBytes(SaltContaint.Length.ToString());
                //Making of the key for decryption
                PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(SaltContaint, Salt);
                //Creates a symmetric Rijndael decryptor object.
                ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));

                MemoryStream memoryStream = new MemoryStream(EncryptedData);
                //Defines the cryptographics stream for decryption.THe stream contains decrpted data
                CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);

                byte[] PlainText = new byte[EncryptedData.Length];
                int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);
                memoryStream.Close();
                cryptoStream.Close();

                //Converting to string
                DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount);
            }
            catch
            {
                DecryptedData = TextToBeDecrypted;
            }
            return DecryptedData;
        }
       
        public static string Encrypt(string TextToBeEncrypted, string SaltContaint)
        {
            RijndaelManaged RijndaelCipher = new RijndaelManaged();
            //string SaltContaint = "CSC";
            byte[] PlainText = System.Text.Encoding.Unicode.GetBytes(TextToBeEncrypted);
            byte[] Salt = Encoding.ASCII.GetBytes(SaltContaint.Length.ToString());
            PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(SaltContaint, Salt);
            //Creates a symmetric encryptor object.
            ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
            MemoryStream memoryStream = new MemoryStream();
            //Defines a stream that links data streams to cryptographic transformations
            CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
            cryptoStream.Write(PlainText, 0, PlainText.Length);
            //Writes the final state and clears the buffer
            cryptoStream.FlushFinalBlock();
            byte[] CipherBytes = memoryStream.ToArray();
            memoryStream.Close();
            cryptoStream.Close();
            string EncryptedData = Convert.ToBase64String(CipherBytes);

            return EncryptedData;
        }
    }

 
Method (2) :

HTML :

<table border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td>
                <asp:TextBox ID="txtData" runat="server"></asp:TextBox>
            </td>
            <td>
                <asp:TextBox runat="server" ID="txtEncrypt"></asp:TextBox>
            </td>
            <td>
                <asp:TextBox runat="server" ID="txtDecrypt"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnEncrypt" runat="server" OnClick="btnEncrypt_Click" Text="Encrypt" />
                <asp:Button ID="btnDecrypt" runat="server" Text="Decrypt" OnClick="btnDecrypt_Click" />
            </td>
        </tr>
    </table>

Code-Behind :

 EncryptionManager manager = new EncryptionManager();
 protected void btnEncrypt_Click(object sender, EventArgs e)
        {
            txtEncrypt.Text = manager.Encrypt(txtData.Text);
        }
        protected void btnDecrypt_Click(object sender, EventArgs e)
        {
            txtDecrypt.Text = manager.Decrypt(txtEncrypt.Text);
        }


Encryption-Decryption Class : 

 public class EncryptionManager
        {
            private byte[] Key = { 89, 83, 45, 236, 140, 228, 180, 79, 209, 164, 231, 131, 28, 7, 110, 73, 140, 235, 118, 52, 225, 46, 202, 118 };
            private byte[] IV = { 161, 200, 187, 207, 22, 92, 119, 227 };
            public string Encrypt(string inputString)
            {
                //Step #4
                byte[] buffer = Encoding.ASCII.GetBytes(inputString);

                TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider()
                {
                    Key = Key,
                    IV = IV
                };
                //Step #4,5,6
                ICryptoTransform ITransform = tripleDes.CreateEncryptor();
                return Convert.ToBase64String(ITransform.TransformFinalBlock(buffer, 0, buffer.Length));
            }
            public string Decrypt(string inputString)
            {
                byte[] buffer = Convert.FromBase64String(inputString);
                TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider()
                {
                    Key = Key,
                    IV = IV
                };
                ICryptoTransform ITransform = tripleDes.CreateDecryptor();
                return Encoding.ASCII.GetString(ITransform.TransformFinalBlock(buffer, 0, buffer.Length));
            }
        }