HttpPostedFile MyFile;
int FileLen;
System.IO.Stream MyStream;
System.Web.HttpFileCollection MyFileCollection = Request.Files;
MyFile = MyFileCollection[0];
FileLen = MyFile.ContentLength;
Wednesday, January 7, 2009
Wednesday, October 29, 2008
Export CSV to Database
public DataTable ParseCSV(string inputString)
{
DataTable dt = new DataTable();
// declare the Regular Expression that will match versus the input string
Regex re = new Regex("((?[^\",\\r\\n]+)|\"(?([^\"]|\"\")+)\")(,|(?\\r\\n|\\n|$))");
ArrayList colArray = new ArrayList();
ArrayList rowArray = new ArrayList();
int colCount = 0;
int maxColCount = 0;
string rowbreak = "";
string field = "";
MatchCollection mc = re.Matches(inputString);
foreach (Match m in mc)
{
// retrieve the field and replace two double-quotes with a single double-quote
field = m.Result("${field}").Replace("\"\"", "\"");
rowbreak = m.Result("${rowbreak}");
if (field.Length > 0)
{
colArray.Add(field);
colCount++;
}
if (rowbreak.Length > 0)
{
// add the column array to the row Array List
rowArray.Add(colArray.ToArray());
// create a new Array List to hold the field values
colArray = new ArrayList();
if (colCount > maxColCount)
maxColCount = colCount;
colCount = 0;
}
}
if (rowbreak.Length == 0)
{
// this is executed when the last line doesn't
// end with a line break
rowArray.Add(colArray.ToArray());
if (colCount > maxColCount)
maxColCount = colCount;
}
// create the columns for the table
for (int i = 0; i < maxColCount; i++)
dt.Columns.Add(String.Format("col{0:000}", i));
// convert the row Array List into an Array object for easier access
Array ra = rowArray.ToArray();
for (int i = 0; i < ra.Length; i++)
{
// create a new DataRow
DataRow dr = dt.NewRow();
// convert the column Array List into an Array object for easier access
Array ca = (Array)(ra.GetValue(i));
// add each field into the new DataRow
for (int j = 0; j < ca.Length; j++)
dr[j] = ca.GetValue(j);
// add the new DataRow to the DataTable
dt.Rows.Add(dr);
}
// in case no data was parsed, create a single column
if (dt.Columns.Count == 0)
dt.Columns.Add("NoData");
return dt;
}
public DataTable ParseCSVFile(string path)
{
string inputString = "";
// check that the file exists before opening it
if (File.Exists(path))
{
StreamReader sr = new StreamReader(path);
inputString = sr.ReadToEnd();
sr.Close();
}
return ParseCSV(inputString);
}
{
DataTable dt = new DataTable();
// declare the Regular Expression that will match versus the input string
Regex re = new Regex("((?
ArrayList colArray = new ArrayList();
ArrayList rowArray = new ArrayList();
int colCount = 0;
int maxColCount = 0;
string rowbreak = "";
string field = "";
MatchCollection mc = re.Matches(inputString);
foreach (Match m in mc)
{
// retrieve the field and replace two double-quotes with a single double-quote
field = m.Result("${field}").Replace("\"\"", "\"");
rowbreak = m.Result("${rowbreak}");
if (field.Length > 0)
{
colArray.Add(field);
colCount++;
}
if (rowbreak.Length > 0)
{
// add the column array to the row Array List
rowArray.Add(colArray.ToArray());
// create a new Array List to hold the field values
colArray = new ArrayList();
if (colCount > maxColCount)
maxColCount = colCount;
colCount = 0;
}
}
if (rowbreak.Length == 0)
{
// this is executed when the last line doesn't
// end with a line break
rowArray.Add(colArray.ToArray());
if (colCount > maxColCount)
maxColCount = colCount;
}
// create the columns for the table
for (int i = 0; i < maxColCount; i++)
dt.Columns.Add(String.Format("col{0:000}", i));
// convert the row Array List into an Array object for easier access
Array ra = rowArray.ToArray();
for (int i = 0; i < ra.Length; i++)
{
// create a new DataRow
DataRow dr = dt.NewRow();
// convert the column Array List into an Array object for easier access
Array ca = (Array)(ra.GetValue(i));
// add each field into the new DataRow
for (int j = 0; j < ca.Length; j++)
dr[j] = ca.GetValue(j);
// add the new DataRow to the DataTable
dt.Rows.Add(dr);
}
// in case no data was parsed, create a single column
if (dt.Columns.Count == 0)
dt.Columns.Add("NoData");
return dt;
}
public DataTable ParseCSVFile(string path)
{
string inputString = "";
// check that the file exists before opening it
if (File.Exists(path))
{
StreamReader sr = new StreamReader(path);
inputString = sr.ReadToEnd();
sr.Close();
}
return ParseCSV(inputString);
}
Thursday, October 16, 2008
Friday, August 22, 2008
get all sub directories and files from those directories
private void getFilesInSubs()
{
MyOrionDS.FileInfo.Clear();
string path = textBox1.Text;
DirectoryInfo subdirs = new DirectoryInfo(path);
DirectoryInfo[] dirs = subdirs.GetDirectories("*");
foreach (DirectoryInfo directory in dirs)
{
System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo(directory.FullName);
if (tbxFileType.Text.Length == 0)
{
pattern = "*.*";
}
else
{
pattern = "*." + tbxFileType.Text;
}
System.IO.FileInfo[] fInfo = dInfo.GetFiles(pattern);
foreach (System.IO.FileInfo fi in fInfo)
{
//string fileInfoStuff = "";
string fileName = fi.FullName;
//string fileversion = "";
DateTime creationTime = fi.CreationTime;
DateTime LastModifyDate = fi.LastWriteTime;
System.Diagnostics.FileVersionInfo myFileVersionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(fileName);
//MessageBox.Show(myFileVersionInfo.ToString());
//fileInfoStuff = fi.Name + " | " + creationTime + " | " + myFileVersionInfo.FileVersion;
OrionDataSet.FileInfoRow newFileInfoRow = (OrionDataSet.FileInfoRow)MyOrionDS.FileInfo.NewFileInfoRow();
newFileInfoRow.FileName = fi.FullName;
newFileInfoRow.LastModifyDate = LastModifyDate;
newFileInfoRow.CreateDate = creationTime;
newFileInfoRow.VersionNumber = myFileVersionInfo.FileVersion;
MyOrionDS.FileInfo.Rows.Add(newFileInfoRow);
MyOrionDS.FileInfo.AcceptChanges();
//lbxPreview.Items.Add(fileInfoStuff);
}
}
dataGridView1.DataSource = MyOrionDS.FileInfo;
}
{
MyOrionDS.FileInfo.Clear()
DirectoryInfo subdirs = new DirectoryInfo(path);
DirectoryInfo[] dirs = subdirs.GetDirectories("*"
foreach (DirectoryInfo directory in dirs)
{
System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo(di
if (tbxFileType.Text.Length == 0)
{
pattern = "*.*";
}
else
{
pattern = "*." + tbxFileType.Text;
}
System.IO.FileInfo[] fInfo = dInfo.GetFiles(pattern);
foreach (System.IO.FileInfo fi in fInfo)
{
//string fileInfoStuff = "";
string fileName = fi.FullName;
//string fileversion = "";
DateTime creationTime = fi.CreationTime;
DateTime LastModifyDate = fi.LastWriteTime;
System.Diagnostics.FileVer
//MessageBox.Show(myFileVe
//fileInfoStuff = fi.Name + " | " + creationTime + " | " + myFileVersionInfo.FileVers
OrionDataSet.FileInfoRow newFileInfoRow = (OrionDataSet.FileInfoRow)
newFileInfoRow.FileName = fi.FullName;
newFileInfoRow.LastModifyD
newFileInfoRow.CreateDate = creationTime;
newFileInfoRow.VersionNumb
MyOrionDS.FileInfo.Rows.Ad
MyOrionDS.FileInfo.AcceptC
//lbxPreview.Items.Add(fil
}
}
dataGridView1.DataSource = MyOrionDS.FileInfo;
}
Monday, August 18, 2008
To retrieve the query execution time
declare @qry varchar(100), @StartLoc int
DECLARE @StartTime datetime,
@EndTime datetime
SET @StartTime = CURRENT_TIMESTAMP;
-- **********QUERY TO BE CHECKED *************
SELECT *
FROM [Table1]
-- *****************************************
SET @EndTime = CURRENT_TIMESTAMP
SELECT DATEDIFF(ms, @StartTime, @EndTime)
GO
DECLARE @StartTime datetime,
@EndTime datetime
SET @StartTime = CURRENT_TIMESTAMP;
-- **********QUERY TO BE CHECKED *************
SELECT *
FROM [Table1]
-- *****************************************
SET @EndTime = CURRENT_TIMESTAMP
SELECT DATEDIFF(ms, @StartTime, @EndTime)
GO
Wednesday, July 9, 2008
Dynamic Stylesheet Assignment Dependent on Browser
I've two stylesheets. One for Internet Explorer & other for other browsers. When the client is IE, the web page should have IE-specific stylesheet attached. For rest of the browsers, the web page must use the other stylesheet.
Add the following code to Page_Load event handler:
body
{
background-color:blue;
}
{
background-color:blue;
}
body
{
background-color:black;
}
{
background-color:black;
}
Add the following code to Page_Load event handler:
protected void Page_Load(object sender, EventArgs e)
{
HtmlLink lnk = new HtmlLink();
if (Request.Browser.Type.Contains("IE"))
{
lnk.Href = "~/StyleSheet.css";
}
else
{
lnk.Href = "~/StyleSheet2.css";
}
lnk.Attributes.Add("rel", "stylesheet");
lnk.Attributes.Add("type", "text/css");
this.Header.Controls.Add(lnk);
}
Test with Internet Explorer & some other browser like Mozilla, Opera etc.
{
HtmlLink lnk = new HtmlLink();
if (Request.Browser.Type.Contains("IE"))
{
lnk.Href = "~/StyleSheet.css";
}
else
{
lnk.Href = "~/StyleSheet2.css";
}
lnk.Attributes.Add("rel", "stylesheet");
lnk.Attributes.Add("type", "text/css");
this.Header.Controls.Add(lnk);
}
How to download file from Server and How to Display PDF file in new browser using C#.net
How to download file from Server and How to Display PDF file in new browser using C#.net
1) Download files from file server into client machine with dialog box (Save/Open)
private void StartDownLoad(string filePath)
{
try
{
filePath = "C:\backup\" + filePath;
//to download the file
FileInfo myFile = new FileInfo(filePath);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + myFile.Name );
Response.AddHeader("Content-Length", myFile.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(myFile.FullName);
Response.Flush();
Response.End();
}
catch (Exception Ex)
{
throw Ex;
}
}
2) Display PDF document into new web browser without dialog box
(below methods works fine with Acrobat Reader 5.0)
private void DisplayPDF(string filePath)
{
try
{
string file_Path = @"C:\backup\standards.pdf";
Response.Buffer = false; //transmitfile self buffers
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
//transmitfile keeps entire file from loading into memory
Response.TransmitFile(file_Path);
Response.TransmitFile(file_Path);
//Or
FileInfo my_File = new FileInfo(file_Path);
Response.WriteFile(my_File.FullName);
FileInfo my_File = new FileInfo(file_Path);
Response.WriteFile(my_File.FullName);
Response.End();
}
catch (Exception Ex)
{
throw Ex;
}
}
{
Response.ContentType = "application/pdf";
//Get the File path from product Library Tab
FileStream oStrm = new FileStream(filePath,FileMode.Open, FileAccess.Read);
byte[] arbData = new byte[10000];
int nC = 0;
//Conert PDF file to byte
while (0 != (nC = oStrm.Read(arbData, 0, 10000)))
{
Response.OutputStream.Write(arbData, 0, nC);
Response.OutputStream.Flush();
Array.Clear(arbData, 0, nC);
Response.Flush();
}
Response.End();
application/msword" etc
Implementation
{
if (contentType.Contains("pdf"))
{
Int32 bufferInByte = 2500;
Response.ClearHeaders();
Response.ClearContent();
Response.Clear();
Response.ContentType = contentType;
Response.BufferOutput = true;
Response.Buffer = true;
Response.AppendHeader("Accept-Ranges", "bytes");
Response.StatusCode = 200;
Response.AppendHeader("Content-Type", contentType);
FileStream oStrm = new FileStream(filePath, FileMode.Open, FileAccess.Read);
long lEndPos = oStrm.Length;
Response.AppendHeader("Content-Length", oStrm.Length.ToString());
Response.AppendHeader("Accept-Header", oStrm.Length.ToString());
Response.AppendHeader("connection", "close");
if (Request.HttpMethod.Equals("HEAD"))
{
Response.Flush();
return;
}
byte[] arbData = new byte[bufferInByte];
int nC = 0;
//Conert PDF file to byte
while (0 != (nC = oStrm.Read(arbData, 0, bufferInByte)))
{
if (nC <>(ref arbData, nC); }
if (Response.IsClientConnected){
Array.Clear(arbData, 0, nC);
Response.Flush();
if (nC <>(ref arbData, bufferInByte); }
}
else {
break;
}
oStrm.Close();
Response.Flush();
}
else
{
//to download others file
FileInfo myFile = new FileInfo(filePath);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + myFile.Name);
Response.AddHeader("Content-Length", myFile.Length.ToString());
Response.ContentType = ConfigurationManager.AppSettings["DefaultContentType"].ToString();
Response.WriteFile(myFile.FullName);
Response.Flush();
Response.End();
}
}
private string GetFileSize(string filePath, ref double fileSizeMB, ref double fileSizeKB)
{
lblError.Text = string.Empty;
double fileSize = 0;
if (filePath.Length > 0)
{
FileStream FSIn = new FileStream(filePath.ToString(), FileMode.Open
, FileAccess.Read);
BinaryReader rFSIn = new BinaryReader(FSIn);
fileSize = FSIn.Length;
fileSize = Math.Round(fileSize, 2);
FSIn.Close();
rFSIn.Close();
fileSizeMB = fileSize / FILE_SIZE_MB;;
if (Math.Round(fileSizeMB,1) == 0.0)
{
fileSizeKB = fileSize / FILE_SIZE_KB;
fileSizeKB = Math.Round(fileSizeKB, 2);
if (fileSizeKB == 0)
return fileSize + " Byte";
else
return fileSizeKB + " KB";
}
else
{
fileSizeMB = Math.Round(fileSizeMB,2);
return fileSizeMB + " MB";
}
}
else
{
lblError.Text = "FILE_NOT_FOUND Error";
return fileSize + "";
}
}
Subscribe to:
Posts (Atom)