Tuesday, 26 December 2017

Alter table using sql queries in sql server


Add column in table

>> ALTER TABLE table_name ADD column_name column-definition;
     ALTER TABLE employees ADD last_name VARCHAR(50);

Change column type in table
>> ALTER TABLE table_name  ALTER COLUMN ColumnName ColumnType
   ALTER TABLE mstVillage ALTER COLUMN Lat decimal(18,9)

Add multiple columns in table
>> ALTER TABLE table_name ADD column_1 column-definition,column_2 column-definition,... column_n column_definition;
  ALTER TABLE employees ADD last_name VARCHAR(50), first_name VARCHAR(40);


Modify column in table
 >> ALTER TABLE table_name ALTER COLUMN column_name column_type;
     ALTER TABLE Emp ALTER COLUMN Col_name VARCHAR(75) NOT NULL;


Drop column in table
>> ALTER TABLE tbl_Name DROP COLUMN column_name;
   ALTER TABLE Emp DROP COLUMN Col_name;


Rename column in table
>> sp_rename 'table_name.old_col_name', 'new_col_name', 'COLUMN';
   sp_rename 'employees.last_name', 'lname', 'COLUMN';


Rename table
>> sp_rename 'old_tbl_name', 'new_tbl_name';

   sp_rename 'employee', 'emp';

Monday, 25 December 2017

Running Counter formats using jquery

////////////////////////// HTML


<span id="counterLbl" class="count">306789948.26</span>



/////////// INR Format JS

function Count_Running() {
                        $('.count').each(function () {
                            $(this).prop('Counter', 0).animate({
                                Counter: $(this).text()
                            }, {
                                duration: 4000,
                                easing: 'swing',
                                step: function (rnCnt) {
                                    var va = INR_RunningCnt_Format(rnCnt);
                                    $(this).text(va);
                                }
                            });
                        });
                    }
                    function INR_RunningCnt_Format(nStr) {
                        nStr += '';
                        x = nStr.split('.');
                        x1 = x[0];
                        x2 = x.length > 1 ? '.' + x[1] : '';
                        var rgx = /(\d+)(\d{3})/;
                        var z = 0;
                        var len = String(x1).length;
                        var num = parseInt((len / 2) - 1);

                        while (rgx.test(x1)) {
                            if (z > 0) {
                                x1 = x1.replace(rgx, '$1' + ',' + '$2');
                            }
                            else {
                                x1 = x1.replace(rgx, '$1' + ',' + '$2');
                                rgx = /(\d+)(\d{2})/;
                            }
                            z++;
                            num--;
                            if (num == 0) {
                                break;
                            }
                        }
                        return x1 + x2;

                    }

////// Result -          30,67,89,948.26

//////////////// Other formate JS
function Count_Running() {
                        $('.count').each(function () {
                            $(this).prop('Counter', 0).animate({
                                Counter: $(this).text()
                            }, {
                                duration: 4000,
                                easing: 'swing',
                                step: function (now) {
                                    var va = Other_Format(now);
                                    $(this).text(va);
                                }
                            });
                        });
                    }

function Other_Format(nStr) {
            nStr += '';
            x = nStr.split('.');
            x1 = x[0];
            x2 = x.length > 1 ? '.' + x[1] : '';
            var rgx = /(\d+)(\d{3})/;
            while (rgx.test(x1)) {
                x1 = x1.replace(rgx, '$1' + ',' + '$2');
            }
            return x1 + x2;
        }

/////////Result-  306,789,948.26

Thursday, 21 December 2017

Gridview export to excel in formatted way using asp.net c#

///////////// Export to excel

private void ExportToExcel_Farmatted(DataTable table)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Reports.xls");
        ///Supporting HTML
        HttpContext.Current.Response.Charset = "utf-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
        //sets font and family
        HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
        HttpContext.Current.Response.Write("<BR><BR><BR>");
        //sets the table border, cell spacing, border color, font of the text, background, foreground, font height
        HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' borderColor='#000000' cellSpacing='0' cellPadding='0' " +
          "style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");
        //am getting my grid's column headers

        for (int j = 0; j < table.Columns.Count; j++)
        {      //write in new column
            HttpContext.Current.Response.Write("<Td>");
            //Get column headers  and make it as bold in excel columns
            HttpContext.Current.Response.Write("<B>");
            HttpContext.Current.Response.Write(table.Columns[j].ColumnName);
            HttpContext.Current.Response.Write("</B>");
            HttpContext.Current.Response.Write("</Td>");
        }
        HttpContext.Current.Response.Write("</TR>");
        foreach (DataRow row in table.Rows)
        {//write in new row
            HttpContext.Current.Response.Write("<TR>");
            for (int i = 0; i < table.Columns.Count; i++)
            {
                HttpContext.Current.Response.Write("<Td>");
                HttpContext.Current.Response.Write(row[i].ToString());
                HttpContext.Current.Response.Write("</Td>");
            }

            HttpContext.Current.Response.Write("</TR>");
        }
        HttpContext.Current.Response.Write("</Table>");
        HttpContext.Current.Response.Write("</font>");
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();
    }


Export Grieview Data into MS Word, Excel,CSV and PDF format using asp.net

////////////////////////////   Use Namespace

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;

using iTextSharp.text.html.simpleparser;

////////////////////////////   Excel format

   protected void Btn_ExportToExcel(object sender, EventArgs e)
    {
        string strQuery = "select CustomerID, ContactName, City, PostalCode from customers";
        SqlCommand cmd = new SqlCommand(strQuery);
        DataTable dt = GetData_Gridview(cmd);
        //Create a dummy GridView
        GridView GridView1 = new GridView();
        GridView1.AllowPaging = false;
        GridView1.DataSource = dt;
        GridView1.DataBind();

        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition","attachment;filename=Result.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridView1.Rows[i].Attributes.Add("class", "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();


    }

////////////////////////////  CSV  format


protected void Btn_ExportToCSV(object sender, EventArgs e)
    {
        string strQuery = "select CustomerID, ContactName, City, PostalCode from customers";
        SqlCommand cmd = new SqlCommand(strQuery);
        DataTable dt = GetData_Gridview(cmd);

        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition","attachment;filename=Result.csv");
        Response.Charset = "";
        Response.ContentType = "application/text";
        StringBuilder sb = new StringBuilder();
        for (int k = 0; k < dt.Columns.Count; k++)
        {
            //add separator
            sb.Append(dt.Columns[k].ColumnName + ',');
        }

        //append new line
        sb.Append("\r\n");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            for (int k = 0; k < dt.Columns.Count; k++)
            {
                //add separator
                sb.Append(dt.Rows[i][k].ToString().Replace(",", ";") + ',');
            }
            //append new line
            sb.Append("\r\n");
        }

        Response.Output.Write(sb.ToString());
        Response.Flush();
        Response.End();

    }



////////////////////////////  MS Word format

protected void Btn_ExportToMSWord(object sender, EventArgs e)
    {
        string strQuery = "select CustomerID, ContactName, City, PostalCode from customers";
        SqlCommand cmd = new SqlCommand(strQuery);
        DataTable dt = GetData_Gridview(cmd);
        //Create a dummy GridView
        GridView GridView1 = new GridView();
        GridView1.AllowPaging = false;
        GridView1.DataSource = dt;
        GridView1.DataBind();
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition","attachment;filename=Result.doc");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-word ";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.RenderControl(hw);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();


    }

////////////////////////////   PDF format

protected void Btn_ExportToPFD(object sender, EventArgs e)
    {
        string strQuery = "select CustomerID, ContactName, City, PostalCode from customers";
        SqlCommand cmd = new SqlCommand(strQuery);
        DataTable dt = GetData_Gridview(cmd);
        //Create a dummy GridView
        GridView GridView1 = new GridView();
        GridView1.AllowPaging = false;
        GridView1.DataSource = dt;
        GridView1.DataBind();

        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition","attachment;filename=Result.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();


    }


How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...