Thursday, December 17, 2009

Export Excel Sheet Data into DataTable

public DataTable CreateTable()
{
string FilePath = Server.MapPath("~/Studentlist.xls");
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+FilePath+";Extended Properties=Excel 8.0");
conn.Open();
DataTable dbSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);
if (dbSchema == null dbSchema.Rows.Count < 1)
{
throw new Exception("Error: Could not determine the name of the first worksheet.");
}
string WorkSheetName = dbSchema.Rows[0]["TABLE_NAME"].ToString();
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [" + WorkSheetName + "]", conn);
DataTable dt = new DataTable();
da.Fill(dt);
conn.Close();
return dt;
}

Wednesday, December 16, 2009

Finding a Control at RowCommand Event of a GridView

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Search") //Button click is causing event fire
{
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
Label lbl1 = row.FindControl("Label1") as Label;
string s = lbl1.Text;
}
}

Followers