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();




Sunday, August 21, 2011

Change gridview row color on mouseover and mouseout in asp.net




protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='Silver'");
            // This will be the back ground color of the GridView Control
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='White'");
        }
    }


Saturday, August 20, 2011

Retreive data from Excel Sheet in asp.net



/// This mehtod retrieves the excel sheet names from

/// an excel workbook.



private String[] GetExcelSheetNames(string excelFile)
{
  OleDbConnection objConn = null;
  System.Data.DataTable dt = null;

  try
  {
    // Connection String. Change the excel file to the file you

    // will search.

    String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
        "Data Source=" + excelFile + ";Extended Properties=Excel 8.0;";
    // Create connection object by using the preceding connection string.

    objConn = new OleDbConnection(connString);
    // Open connection with the database.

    objConn.Open();
    // Get the data table containg the schema guid.

    dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

    if(dt == null)
    {
      return null;
    }

    String[] excelSheets = new String[dt.Rows.Count];
    int i = 0;

    // Add the sheet name to the string array.

    foreach(DataRow row in dt.Rows)
    {
      excelSheets[i] = row["TABLE_NAME"].ToString();
      i++;
    }

    // Loop through all of the sheets if you want too...

    for(int j=0; j < excelSheets.Length; j++)
    {
      // Query each excel sheet.

    }

    return excelSheets;
  }
  catch(Exception ex)
  {
    return null;
  }
  finally
  {
    // Clean up.

    if(objConn != null)
    {
      objConn.Close();
      objConn.Dispose();
    }
    if(dt != null)
    {
      dt.Dispose();
    }
  }




Sql Function To Split the commas Value and insert them into table.



CREATE FUNCTION [dbo].[ListToTable] (
  /*
  FUNCTION ListToTable
  Usage: select entry from listtotable('abc,def,ghi') order by entry desc
  PURPOSE: Takes a comma-delimited list as a parameter and returns the values of that list into a table variable.
  */
  @mylist varchar(8000)
  )
  RETURNS @ListTable TABLE (
  seqid int not null,
  entry varchar(255) not null)

  AS

  BEGIN
      DECLARE
              @this varchar(255),
              @rest varchar(8000),
              @pos int,
              @seqid int

      SET @this = ' '
      SET @seqid = 1
      SET @rest = @mylist
      SET @pos = PATINDEX('%,%', @rest)
      WHILE (@pos > 0)
      BEGIN
              set @this=substring(@rest,1,@pos-1)
              set @rest=substring(@rest,@pos+1,len(@rest)-@pos)
              INSERT INTO @ListTable (seqid,entry)  VALUES (@seqid,@this)
              SET @pos= PATINDEX('%,%', @rest)
              SET @seqid=@seqid+1
      END
      set @this=@rest
      INSERT INTO @ListTable (seqid,entry) VALUES (@seqid,@this)
      RETURN
  END