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

Some Important Example or Regular expression in asp.net



Remove last two zero inside gridview if
Price=56.0000:

'<%#Eval("Price").ToString().Remove(Eval("Price").ToString().Length - 2, 2) %>'


Regular Expressions for Amount:

^\$?\d+(\.(\d{2}))?$
To evaluate an amount with or without a dollar sign where the cents are optional.


^([0-9]*|\d*\.\d{1}?\d*)$
Accept only (0-9) integer and one decimal point(decimal point is also optional).After decimal point it accepts at least one numeric .This will be usefull in money related fields or decimal fields.

Regular Expression for multiline Textbox:

ErrorMessage="Address should be less than 130 characters" ValidationExpression="[\s\S]{0,130}"
ValidationGroup="submit">.


compare validate for today date:

Type="Date" runat="server" ValidationGroup="submit" ErrorMessage="Start date must be greater than today date."
SetFocusOnError="True">.



On page load

 c1.ValueToCompare = DateTime.Now.ToString("MM/dd/yyyy");