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;
}
}

Wednesday, November 25, 2009

Encrypting/Decrypting any section of web.config file

To Encrypt on IIS
aspnet_regiis.exe -pe "[Section To Encrypt]" -app "/[Virtual Directory on IIS]"

eg---
aspnet_regiis.exe -pd "connectionStrings" -app "/vitualdirectory"
To decrypt the same use de instead of pe

Tuesday, November 24, 2009

Code to Open Pdf File(Download)

Code to Open pdf/doc/any file(Download)

This post is for opening any document in website
// function to open file
protected void OpenDocument()
{
// Get the physical Path of the file(test.doc)
string filepath = Server.MapPath("Document Location");

// Create New instance of FileInfo class to get the properties of the file being downloaded
FileInfo file = new FileInfo(filepath);

// Checking if file exists
if (file.Exists)
{
// Clear the content of the response
Response.ClearContent();

// LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

// Add the file size into the response header
Response.AddHeader("Content-Length", file.Length.ToString());

// Set the ContentType
Response.ContentType = ReturnExtension(file.Extension.ToLower());

// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
Response.TransmitFile(file.FullName);

// End the response
Response.End();
}
}
// Function to get extenstion of the file
private string ReturnExtension(string fileExtension)
{
switch (fileExtension)
{
case ".htm":
case ".html":
case ".log":
return "text/HTML";
case ".txt":
return "text/plain";
case ".doc":
return "application/ms-word";
case ".tiff":
case ".tif":
return "image/tiff";
case ".asf":
return "video/x-ms-asf";
case ".avi":
return "video/avi";
case ".zip":
return "application/zip";
case ".xls":
case ".csv":
return "application/vnd.ms-excel";
case ".gif":
return "image/gif";
case ".jpg":
case "jpeg":
return "image/jpeg";
case ".bmp":
return "image/bmp";
case ".wav":
return "audio/wav";
case ".mp3":
return "audio/mpeg3";
case ".mpg":
case "mpeg":
return "video/mpeg";
case ".rtf":
return "application/rtf";
case ".asp":
return "text/asp";
case ".pdf":
return "application/pdf";
case ".fdf":
return "application/vnd.fdf";
case ".ppt":
return "application/mspowerpoint";
case ".dwg":
return "image/vnd.dwg";
case ".msg":
return "application/msoutlook";
case ".xml":
case ".sdxl":
return "application/xml";
case ".xdp":
return "application/vnd.adobe.xdp+xml";
default:
return "application/octet-stream";
}
}

Followers