Friday, July 30, 2010

Regular Expression Examples

  • IP Adress -
    Regular expression - (\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})

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;
        }