Dear Sir,
Need your help on urgent base.
I want to read .csv file but i have getting mention error.
Server Error in '/' Application.
The process cannot access the file 'D:\AAQMS Data\AQ1.csv' because it is being used by another process.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.IO.IOException: The process cannot access the file 'D:\AAQMS Data\AQ1.csv' because it is being used by another process. Source Error:
Line 20: {
Line 21:
Line 22: string csvFromFile = (new WebClient()).DownloadString("D:/AAQMS Data/AQ1.csv");
Line 23: lines = csvFromFile.Split('\n');
Line 24: DataTable dt = new DataTable();
|
namespace ReadTxtFile
{
public partial class WebForm5 : System.Web.UI.Page
{
static string[] lines;
int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
string csvFromFile = (new WebClient()).DownloadString("D:/AAQMS Data/AQ1.csv");
lines = csvFromFile.Split('\n');
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Unit#2", System.Type.GetType("System.String")));
for (int i = 0; i < lines.Length; i++)
{
DataRow dr = dt.NewRow();
try
{
dr["Unit#2"] = "" + " " + " " + " " + lines[i].Substring(lines[i].IndexOf(' ', 0, lines[i].Length), lines[i].Trim().Length - lines[i].IndexOf(' ', 0, lines[i].Length)) + " " + "";
}
catch (Exception e1)
{
int j = 0;
};
dt.Rows.Add(dr);
dr = dt.NewRow();
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Gridview1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].ForeColor = System.Drawing.Color.White;
string line = e.Row.RowIndex % 2 != 0 ? lines[i] : "";
if (e.Row.RowIndex % 2 != 0)
{
if (line.Length <= 200)
{
e.Row.Cells[0].BackColor = System.Drawing.Color.Red;
}
else if (line.Length >= 100 && line.Length <= 0)
{
e.Row.Cells[0].BackColor = System.Drawing.Color.Orange;
}
else
{
e.Row.Cells[0].BackColor = System.Drawing.Color.Green;
}
}
i++;
}
}
}
}