Monday, 30 June 2014

export gridview data using asp .net C#

public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
    private void ExportPostExcelSheet(GridView gvDetails, string FileName)
    {
        try
        {
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", FileName));// "Customers.xls"
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gvDetails.AllowPaging = false;
            // BindGridview();
            //Change the Header Row back to white color
            gvDetails.HeaderRow.Style.Add("background-color", "#FFFFFF");
            //Applying stlye to gridview header cells
            for (int i = 0; i < gvDetails.HeaderRow.Cells.Count; i++)
            {
                gvDetails.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
            }
            gvDetails.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Monday, 16 June 2014

Delete All table

d
If this is a one-time issue, use SQL Server Management Studio to delete the tables.
If you must run a script very, very carefully use this:
EXEC sp_msforeachtable 'DROP TABLE ?'

Friday, 6 June 2014

Read CSV file to return DataTable using C#

 private DataTable ReadToEnd(string filePath)
    {
        DataTable dtDataSource = new DataTable();
        string[] fileContent = File.ReadAllLines(@"C:\Users\Amit Lakra\Documents\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\"+filePath);
        if (fileContent.Count() > 0)
        {
            //Create data table columns
            string[] columns = fileContent[0].Split(',');
            for (int i = 0; i < columns.Count(); i++)
            {
                dtDataSource.Columns.Add(columns[i]);
            }

            //Add row data
            for (int i = 1; i < fileContent.Count(); i++)
            {
                string[] rowData = fileContent[i].Split(',');
                dtDataSource.Rows.Add(rowData);
            }
        }
        return dtDataSource;
    }

Sunday, 1 June 2014

Json Serialization Length Error?

<system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="50000000"/>
      </webServices>
    </scripting>
  </system.web.extensions>