Monday, January 25, 2010

Exporting GridView Data into Excel in Asp.Net

Add the Folllowing namespace:

using System.IO;

Write the following lines on the click of the Export button.
protected void btnExport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "application/ms-excel";
Response.Charset = "";
Page.EnableViewState = false;
Response.AddHeader("Content-Disposition", "inline;filename=report.xls");
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
//Here grid1 is the name of the GridView Control.
grid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}

Enjoy Exporting data from GridView to Excel.

Calling Stored Procedure of Oracle in Asp.Net

Add Reference
System.Data.OracleClient;

After That Add the Following Code.

public void AddEmployee(int EmpId,string EmpName)
{
string strConn = "Data Source=Data Source;User ID=system;Password=pwd;";
OracleConnection con = new OracleConnection();
con.ConnectionString = strConn;
con.Open();
OracleCommand cmd = new OracleCommand();
//CommandText=Procedure Name.
cmd.CommandText = "InsertData";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
//ID is the name of Parameter in SP.
cmd.Parameters.Add("ID", OracleType.Number).Value = EmpId;
//NAME is the name of parameter in SP
cmd.Parameters.Add("NAME", OracleType.VarChar).Value = EmpName;
try
{
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
throw ex;
}
}


Enjoy....

Using Oracle Connection To List data in Asp.Net

Add the Reference of System.Data.OracleClient.
and then ...
using System.Data.OracleClient;
using System.Data;

public void LoadData()
{
string strConn = "Data Source=Data Source Name;User ID=userId;Password=password;";
using (OracleConnection objConnection = new OracleConnection())
{
objConnection.ConnectionString = strConn;
try
{
objConnection.Open();
OracleCommand objCommand = new OracleCommand();
objCommand.Connection = objConnection;
objCommand.CommandText = "select ID,NAME from Employee";
objCommand.CommandType = System.Data.CommandType.Text;
OracleDataAdapter objAdapter = new OracleDataAdapter(objCommand);
DataTable objTable = new DataTable();
objAdapter.Fill(objTable);
grid1.DataSource = objTable;
grid1.DataBind();
objConnection.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
objConnection.Close();
}
}
}

Enjoy....