Friday, December 26, 2008

How to show "<--Select--> " in drop down box on page load?

First DropDownlist load Data from Database,Then the first text in DropDownlist is "--Select--" Text.

ListItem li = new ListItem();
li.Text = "<--Select-->";
SqlDataAdapter ada = new SqlDataAdapter("select MOVIENAME,MOVIECODE from CURMOVI", conn);
DataTable dt = new DataTable();
ada.Fill(dt);
ddlMovieName.DataSource = dt;
ddlMovieName.DataTextField = "MOVIENAME";
ddlMovieName.DataValueField = "MOVIECODE";
ddlMovieName.DataBind();
ddlMovieName.Items.Insert(0, li);


******************************** (OR) ******************************


SqlDataAdapter ada = new SqlDataAdapter("select MOVIENAME,MOVIECODE from CURMOVI", conn);
DataTable dt = new DataTable();
ada.Fill(dt);
ddlMovieName.DataSource = dt;
ddlMovieName.DataTextField = "MOVIENAME";
ddlMovieName.DataValueField = "MOVIECODE";
ddlMovieName.DataBind();
ddlMovieName.Items[0].Text = "--Select--";

Friday, December 19, 2008

DataGrid to Excel Sheet Report Generator in ASP.NET with C#

First include the Header files ::

using System.Data.SqlClient;
using System.Xml;
using System.IO;
using System.Xml.Xsl;
----------------------------------------------------------------------------
Connection string ::

SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["DBConString"]);
DataSet ds;
SqlDataAdapter ada;
----------------------------------------------------------------------------
DataBase values to load in DataGrid ::

try
{
string cmdstr = "select Cls from abi_pay";
if (conn.State == ConnectionState.Closed)
conn.Open();
ada = new SqlDataAdapter(cmdstr, conn);
ds = new DataSet();
ada.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
}
catch (Exception cEx)
{
Response.Write(cEx.Message);
}

----------------------------------------------------------------------------
Export Excel reports in One Button click event::

protected void Button1_Click(object sender, EventArgs e)
{
// Data grid to Excel Report Generator
HtmlForm form = new HtmlForm();
string attachment = "attachment; filename=AbiramiReports.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
form.Controls.Add(GridView1);
this.Controls.Add(form);
form.RenderControl(htextw);
Response.Write(stw.ToString());
Response.End();
}


------------------------------------------------------------------------

Another Methos for Data Grid to Excel Report Without Paging..
--------------------------------------------------------------------------
First Assign your DataSet to SessionLike :

Session["DataSet"] = ds;

in Grid Load Area.

protected void btnExcel_Click(object sender, EventArgs e)
{
DataSet dstest = new DataSet();
dstest = (DataSet)Session["DataSet"];
ConvertToExcel(dstest, Response);
}

public static void ConvertToExcel(DataSet ds, HttpResponse Response)
{
try
{
Response.Clear();
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
catch (Exception ex)
{
}
}