Friday, July 16, 2010

Add Serial No in DataControls

1. To add serial No in Repeater:

             <%#  DataBinder.Eval(Container,"ItemIndex") %>

Thursday, April 8, 2010

Populate Date DropDown using Javascript

Set the values of Dropdown list of a date:-

There are three dropdown lists for each Day, Month And Year.  we fill through javascript with following way:

        var date ="11/23/2010"; //Format- MM/DD/YYYY
        var mm = date.substring(0,2);
        var dd = date.substring(3,5);
        var yy = date.substring(6,10);
       
        document.getElementById("ddlDay").value=dd;
        document.getElementById("ddlMonth").value=mm;
        document.getElementById("ddlYear").value=yy;

Saturday, March 27, 2010

Select records from a DataTable

Select recodrs from a datatable:-


DataTable dtLog = Funtion();

DataRow[] drs = dtLog.Select(" UpdatedDate  = '" + dtDate.Rows[i]["Date"].ToString() + "'");
 DataTable dts = dtLog.Clone(); 
dts = drs.CopyToDataTable();

Tuesday, March 23, 2010

Default Button, asp.net

Default Button for a page:
                  <form id="form1" runat="server" defaultbutton="buttonName"></form>
Default Button for a panel:
                  <asp:Panel runat="server" DefaultButton="lbHello"></asp:Panel>
Default in a div:-
                  <div onkeypress="javascript:return WebForm_FireDefaultButton(event, 'buttonID')"></div>


For Linkbutton to make a default page, add below javascript at the end of the page

<script type="text/javascript">
    var __defaultFired = false;

function WebForm_FireDefaultButton(event, target) {
    var element = event.target || event.srcElement;

    if (!__defaultFired && event.keyCode == 13 && !(element && (element.tagName.toLowerCase() == "textarea"))) {
        var defaultButton;

        if (__nonMSDOMBrowser)
            defaultButton = document.getElementById(target);
        else
            defaultButton = document.all[target];

        if (defaultButton) {
            if(typeof(defaultButton.click()) != "undefined")
                defaultButton.click();
            else
                eval(unescape(defaultButton.href.replace("javascript:", "")));

            event.cancelBubble = true;

            if (event.stopPropagation) event.stopPropagation();
            return false;
        }
    }
    return true;
}
</script>

Saturday, February 20, 2010

How to use javascript inside updatePanel, asp.net

Use Javascirpt in code behind like this 

string Scriptup = "javascript: alert('Munesh Sunda');";
ScriptManager.RegisterStartupScript(this, this.GetType(), "Scriptup", Scriptup, true);

This works fine.

How to Remove duplicate records - Sql-server 2005

Query to remove duplicate records, in this query please add all the columns in GROUP BY clause that are duplicate, it is based on primary key


DELETE from TableName
WHERE CoumnName1 + CoumnName2 + CoumnName3
IN (select CoumnName1 + CoumnName2 + CoumnName3
        FROM TableName
        group by CoumnName1 , CoumnName2 ,CoumnName3
        having Count(ID)>1)
AND ID NOT IN (select MAX(ID) from TableName
        WHERE CoumnName1 + CoumnName2 + CoumnName3
        IN (select CoumnName1 + CoumnName2 + CoumnName3
                FROM TableName
                group by CoumnName1 , CoumnName2 ,CoumnName3
                having Count(ID)>1)
        GROUP BY CoumnName1 ,CoumnName2 ,CoumnName3)

How to DownLoad a File in ASP.NET ?

To DownLoad a file:-

1. Add a link to page, Like this:-
               <a href="Download.ashx?FileName=file.pdf&FilePath=DownLoad/">Download Standing Order Mandate</a >

          Where file.pdf = file to download
                     DownLoad = Folder name where the file placed at server.

2. Then  Add a Generic Handler to the website/project with the name Download.ashx and add the following code:
   public void ProcessRequest(HttpContext context)
    {
        if (context.Request.QueryString["FileName"] != null && context.Request.QueryString["FilePath"] != null)
        {
            string strFileName = context.Request.QueryString["FileName"];
            string strOnPath = @"~/" + context.Request.QueryString["FilePath"].ToString() + strFileName;
            string strpath = context.Server.MapPath(strOnPath);
            FileInfo myFile = new FileInfo(strpath);
            context.Response.Clear();
            context.Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName);
            context.Response.AddHeader("Content-Length", myFile.Length.ToString());
            context.Response.ContentType = "application/octet-stream";
            context.Response.WriteFile(myFile.FullName);
            context.Response.End();
        }

  

Monday, February 15, 2010

Date Format, C#

Date FromatOutput
dd-MMM-yyyy15-Feb-2010
dd-MM-yyyy15-02-2010
dd-MMM-yyyy hh:mm:ss15-Feb-2010 11:25:56
dd-MMM-yyyy hh:mm tt15-Feb-2010 11:25 AM
dd-MMM-yy15-Feb-10
MMM dd, yyyyFeb 15, 2010
MMMM dd, yyyyApril 15, 2010


Example:
DateTime objDt = DateTime.Now;
objDt.ToString("dd-MMM-YYYY") = 15-Feb-2010

Trim a string to a specified length, C#

Trim a string to a length, Here

        public  string TrimString(string strValue,int Count)
        {           
            if (strValue.Length > Count)
            {
                strValue = strValue.Substring(0, Count - 1) + "..";
            }
            return strValue;
        }

Generate an alphanumeric Random Code, C#

        To generate an alphanumeric Random code

         ///

        /// Public static function Generate Random Code
        /// generate the random code
        /// Created By:   Munesh
        /// Created Date: Feb 10, 2010
        ///

        /// string
        public static string GenerateRandomCode()
        {
            int _minLength = 6, _maxLength = 6;

            string _charsLCase = "abcdefgijkmnopqrstwxyz";
            string _charsUCase = "ABCDEFGHJKLMNPQRSTWXYZ";
            string _charsNumeric = "23456789";

            // Create a local array containing supported Verification Code characters
            char[][] _charGroups = new char[][]
            {
                _charsLCase.ToCharArray(),
                _charsUCase.ToCharArray(),
                _charsNumeric.ToCharArray(),
                //PASSWORD_CHARS_SPECIAL.ToCharArray()
            };

            // Use this array to track the number of unused characters in each
            // character group.
            int[] _charsLeftInGroup = new int[_charGroups.Length];

            // Initially, all characters in each group are not used.
            for (int i = 0; i < _charsLeftInGroup.Length; i++)
                _charsLeftInGroup[i] = _charGroups[i].Length;

            // Use this array to track (iterate through) unused character groups.
            int[] _leftGroupsOrder = new int[_charGroups.Length];

            // Initially, all character groups are not used.
            for (int i = 0; i < _leftGroupsOrder.Length; i++)
                _leftGroupsOrder[i] = i;

            // Because we cannot use the default randomizer, which is based on the
            // current time (it will produce the same "random" number within a
            // second), we will use a random number generator to seed the
            // randomizer.

            // Use a 4-byte array to fill it with random bytes and convert it then
            // to an integer value.
            byte[] _randomBytes = new byte[4];

            // Generate 4 random bytes.
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            rng.GetBytes(_randomBytes);

            // Convert 4 bytes into a 32-bit integer value.
            int _seed = (_randomBytes[0] & 0x7f) << 24 |
                        _randomBytes[1] << 16 |
                        _randomBytes[2] << 8 |
                        _randomBytes[3];

            // Now, this is real randomization.
            Random random = new Random(_seed);

            // This array will hold password characters.
            char[] _verificationCode = null;

            // Allocate appropriate memory for the password.
            if (_minLength < _maxLength)
                _verificationCode = new char[random.Next(_minLength, _maxLength + 1)];
            else
                _verificationCode = new char[_minLength];

            // Index of the next character to be added to password.
            int _nextCharIdx;

            // Index of the next character group to be processed.
            int _nextGroupIdx;

            // Index which will be used to track not processed character groups.
            int _nextLeftGroupsOrderIdx;

            // Index of the last non-processed character in a group.
            int _lastCharIdx;

            // Index of the last non-processed group.
            int _lastLeftGroupsOrderIdx = _leftGroupsOrder.Length - 1;

            // Generate password characters one at a time.
            for (int i = 0; i < _verificationCode.Length; i++)
            {
                // If only one character group remained unprocessed, process it;
                // otherwise, pick a random character group from the unprocessed
                // group list. To allow a special character to appear in the
                // first position, increment the second parameter of the Next
                // function call by one, i.e. lastLeftGroupsOrderIdx + 1.
                if (_lastLeftGroupsOrderIdx == 0)
                    _nextLeftGroupsOrderIdx = 0;
                else
                    _nextLeftGroupsOrderIdx = random.Next(0,
                                                         _lastLeftGroupsOrderIdx);

                // Get the actual index of the character group, from which we will
                // pick the next character.
                _nextGroupIdx = _leftGroupsOrder[_nextLeftGroupsOrderIdx];

                // Get the index of the last unprocessed characters in this group.
                _lastCharIdx = _charsLeftInGroup[_nextGroupIdx] - 1;

                // If only one unprocessed character is left, pick it; otherwise,
                // get a random character from the unused character list.
                if (_lastCharIdx == 0)
                    _nextCharIdx = 0;
                else
                    _nextCharIdx = random.Next(0, _lastCharIdx + 1);

                // Add this character to the password.
                _verificationCode[i] = _charGroups[_nextGroupIdx][_nextCharIdx];

                // If we processed the last character in this group, start over.
                if (_lastCharIdx == 0)
                    _charsLeftInGroup[_nextGroupIdx] =
                                              _charGroups[_nextGroupIdx].Length;
                // There are more unprocessed characters left.
                else
                {
                    // Swap processed character with the last unprocessed character
                    // so that we don't pick it until we process all characters in
                    // this group.
                    if (_lastCharIdx != _nextCharIdx)
                    {
                        char _temp = _charGroups[_nextGroupIdx][_lastCharIdx];
                        _charGroups[_nextGroupIdx][_lastCharIdx] =
                                    _charGroups[_nextGroupIdx][_nextCharIdx];
                        _charGroups[_nextGroupIdx][_nextCharIdx] = _temp;
                    }
                    // Decrement the number of unprocessed characters in
                    // this group.
                    _charsLeftInGroup[_nextGroupIdx]--;
                }

                // If we processed the last group, start all over.
                if (_lastLeftGroupsOrderIdx == 0)
                    _lastLeftGroupsOrderIdx = _leftGroupsOrder.Length - 1;
                // There are more unprocessed groups left.
                else
                {
                    // Swap processed group with the last unprocessed group
                    // so that we don't pick it until we process all groups.
                    if (_lastLeftGroupsOrderIdx != _nextLeftGroupsOrderIdx)
                    {
                        int _temp = _leftGroupsOrder[_lastLeftGroupsOrderIdx];
                        _leftGroupsOrder[_lastLeftGroupsOrderIdx] =
                                    _leftGroupsOrder[_nextLeftGroupsOrderIdx];
                        _leftGroupsOrder[_nextLeftGroupsOrderIdx] = _temp;
                    }
                    // Decrement the number of unprocessed groups.
                    _lastLeftGroupsOrderIdx--;
                }
            }

            // Convert password characters into a string and return the result.
            return new string(_verificationCode);
        }