Asheej Kommery's .NET blog

  • How to remove {HYPERLINK} text shows in outlook message in edit mode - ALT + F9

    I have been searching for a while for the option to remove the {HYPERLINK} text which shows in my mail message when I edit any message having hyperlink in outlook 2010. I know that I have pressed a key combination but don't remember what was that.

    Luckily I got the short cut back after a thorough search in google.

    So I thought I will share the short cut with you all and also for me, by any chance if I do the same mistake again!!!

    Normally in such cases your outlook message will show like below text.

    {HYPERLINK “http://asheej.blogspot.co.uk/”}


    Nothing to worry press ALT+F9 while composing the message. Try again the same key combination it will come back.
     

  • How to check the current year is leap year or not using ASP.NET

    Here I am going to explain you how to check current year is leap year or not using the method IsLeapYear. It will be usefull when you have to do some kind of validation your code based on the year.

    This is one of the common question we see in .NET forum, people go for SQL query or devide the year by zero and all to find the leap year. But Microsoft has got an inbuilt method to find out this.

    IsLeapYear method takes one argument of integer type so you need to pass the year to check whether it is leap year or not.

    Check below lines of code,

  • How to restrict the user to copy and paste the URL in browser address bar in ASP.NET

    This is one of the common restrictions you have to apply on a web application. If user is already login to the application and user knows the page extension user can directly copy and paste the URL in web browser to open the page, because user has got already an active session.

    In such cases what we have to do is, we will make use of UrlReferrer property.

    Please check below code, you may write below code in the page load of each page you wanted to restrict copy and paste of URL to open the page.

     

    string strPreviousPage = "";
     if (Request.UrlReferrer != null)
       {
        strPreviousPage = Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];         
        }       
    if(strPreviousPage =="")
        {
          Response.Redirect("~/Login.aspx");
         }
     
    Above code will forcefully redirect to Login page if user copy and paste the URL to open the page.

    Also along with this code you might be using your regular authentication code.

    Please post your queries if you have any with this code and technique.

  • "An item with the same key has already been added" - Team Foundation (TFS) Error

    Today Morning I was uploading My project to TFS, When I clicked on "add solution to source control" I started getting the error " Server was unable to process request. An item with the same key has already been added "

    The error "An item with the same key has already been added" normally occurs if your TFS client cache is corrupted.

    To resolve this issue you may install team Foundation Server 2010 Service Pack 1.

    Not only this, Service pack 1 resolves many issues which you may face.

    Refer Microsoft KB article for more details: KB2182621

  • Suppress ASP.NET server control while exporting to Excel from Gridview

    We normally use server side controls like textbox, dropdownlist, link button etc in gridview. What will happen when you export gridview to excel? Your excel will have textbox, dropdownlist etc as a control along with the value. That looks ugly when you have to send the excel report to the user.

    Here I am goiing to explain what you have to do when you export the data from gridview to Excel if you have server controls in the Gridview.
    What we will do is convert the all server controls in the gridview to literal and then we export that to Excel.

    In below sample code you can see I am assigning the value fom checkbox and Textbox to a literal control. Extend the same to add link button, dropdownlist or for other server side controls.

        private void ExportApplication()
        {
            string Excelfilename = "ExcelSample" + DateTime.Now;
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.AppendHeader("Content-Disposition:", "attachment; filename=" + Excelfilename + ".xls");
            Response.Charset = "";
            this.EnableViewState = false;
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            this.ClearControls(Gridview1);
            GridView.RenderControl(oHtmlTextWriter);
            Response.Write(oStringWriter.ToString());
            Response.End();
        }
        private void ClearControls(Control control)
        {
            for (int i = control.Controls.Count - 1; i >= 0; i--)
            {
                ClearControls(control.Controls[i]);
            }
            if (!(control is TableCell))
            {
                if (control.GetType().GetProperty("SelectedItem") != null)
                {
                    LiteralControl literal = new LiteralControl();
                    control.Parent.Controls.Add(literal);
                    try
                    {
                        literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control, null);

                    }
                    catch
                    {
                    }
                    control.Parent.Controls.Remove(control);
                }
                else
                    if (control.GetType().GetProperty("Text") != null)
                    {
                        LiteralControl literal = new LiteralControl();
                        control.Parent.Controls.Add(literal);
                        literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control, null);
                        control.Parent.Controls.Remove(control);
                    }
            }
            return;
        }

  • The Microsoft.Jet.OLEDB.4.0 provider is not registered on the local machine ASP.NET error

     This error normally occurs when werun our application on 64bit machine. Just follow below steps to resolve the issue,

    1. Start-->Run-->Inetmgr
    2. Locate the application pool your site is using
    3. Rigth click on that application pool and select the option "Advance settings..."
    4. Find the Option "Enable 32-bit Applications" and set it as "true".