Friday, September 16, 2011

Enable SSL in asp.net



Please Create this function to enable SSL on your site with some page but make sure the SSL certificate is already installed
on your site or server and then call this function on page load event. 



Private void EnableSSL()
    {
        String CurrentUrl = Request.Url.ToString();
        //Get absolute path
        String sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
        System.IO.FileInfo Finfo = new System.IO.FileInfo(sPath);
        string page=
Finfo.Name;
        if (
page== "abc.aspx" || page== "cde.aspx" )
        {
            String NewUrl = "";
            if (!Request.IsSecureConnection)
            {
                NewUrl = "https" + CurrentUrl.Substring(4);
                Response.Redirect(NewUrl);
            }
        }
        else
        {
            if (CurrentUrl.IndexOf("https") >= 0)
            {
                Response.Redirect(CurrentUrl.Replace("https", "http"));
            }
        }
    }









Monday, September 5, 2011

LINQ To Entity and Convert class Methods Problems

While i was working with linq to sql or entity framework i have getting error at runtime like:

1. LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression
 
 2. LINQ to Entities does not recognize the method 'System.String get_Item(System.String)' method, and this method cannot be translated into a store expression.

and my code is:

                var lst = (from p in context.Products
                           where p.CategoryID==Convert.ToInt32(this.Request.QueryString["Category"])
                           select p).ToList();

Than i have found that:

Reason: First value of type "object" is returned and then it is typecasted to int. This is, again, not permitted as a temporary anonymous object needs to be created for resolving translation of Data Type.

Solution of this:

int id = Convert.ToInt32(this.Request.QueryString["Category"]);
                var lst = (from p in context.Products
                           where p.CategoryID==id
                           select p).ToList();