Thursday, 7 August 2014

export gridview to excel

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" EnableEventValidation="false"%>
protected void ExportToExcel(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        using (StringWriter sw = new StringWriter())
        {
            HtmlTextWriter hw = new HtmlTextWriter(sw);

            //To Export all pages
            GridView1.AllowPaging = false;
            this.BindGrid();

            GridView1.HeaderRow.BackColor = Color.White;
            foreach (TableCell cell in GridView1.HeaderRow.Cells)
            {
                cell.BackColor = GridView1.HeaderStyle.BackColor;
            }
            foreach (GridViewRow row in GridView1.Rows)
            {
                row.BackColor = Color.White;
                foreach (TableCell cell in row.Cells)
                {
                    if (row.RowIndex % 2 == 0)
                    {
                        cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
                    }
                    else
                    {
                        cell.BackColor = GridView1.RowStyle.BackColor;
                    }
                    cell.CssClass = "textmode";
                }
            }

            GridView1.RenderControl(hw);
           
            //style to format numbers to string
            string style = @"<style> .textmode { mso-number-format:\@; } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }

Friday, 25 July 2014

Password validation using asp.net control

http://aspsnippets.com/Articles/Implement-Password-Policy-using-Regular-Expressions-and-ASPNet-RegularExpression-Validator.aspx

Monday, 14 July 2014

delete duplicate records in sql

select * from  employee
go
alter table employee add Id int Identity(1,1)
go
delete from  employee where ID not in
(
select min(id) as ID from employee group by EmployeeID,Salary

)
go
alter table employee drop column Id
go


select * from employee

Tuesday, 8 July 2014

Validate and replace char using jqery

if (str.indexOf('\'') >= 0 && str.indexOf('"') >= 0) {
   //do something
}

    $('#txt_pname').keyup(function () {
                var str = $('#txt_pname').val();
                if (str.indexOf('"') >= 0) {
                    //do something
                    $('#txt_pname').val(str.replace('"', ''));
                    alert('Double quote (") not allow.');
                }
            });

Tuesday, 1 July 2014

Get comma secreted values

create table your_table(City_Code int,Post_Code nvarchar(10),    Post_Code_Description  nvarchar(10))
insert into your_table values(100,'A1','ABC')
insert into your_table values(100,'C8','XYZ')
insert into your_table values(100,'Z3','MNO')
insert into your_table values(200,'D4','LMN')
insert into your_table values(300,'E3','IJK')
insert into your_table values(300,'B9','RST')



SELECT City_Code,
      Post_Code =
        STUFF((SELECT ', ' + Post_Code
           FROM your_table b
           WHERE b.City_Code = a.City_Code
          FOR XML PATH('')), 1, 2, '')
--,Post_Code_Description=
        STUFF((SELECT ', ' + Post_Code_Description
           FROM your_table b
          WHERE b.City_Code = a.City_Code
          FOR XML PATH('')), 1, 2, '')
FROM your_table a
GROUP BY City_Code