I have .csv file, if user open file another location the message display "File open in another location" on main screen.
how to do this.
HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebForm3.aspx.cs" Inherits="CSV.WebForm3" %>
<meta http-equiv="refresh" content="3">
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1 style="width: 1228px; text-align: center">
Station-1(TS)</h1>
<!-- [COMMENT] -->
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#9AD6ED" HeaderStyle-ForeColor="#636363"
runat="server" AutoGenerateColumns="False" OnDataBound="OnDataBound" Width="1114px"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="3">
<Columns>
<asp:BoundField DataField="Parameters" HeaderText="Parameters" />
<asp:BoundField DataField="PM10" HeaderText="PM10" />
<asp:BoundField DataField="PM2.5" HeaderText="PM2.5" />
<asp:BoundField DataField="SO2" HeaderText="SO2" />
<asp:BoundField DataField="NO" HeaderText="NO" />
<asp:BoundField DataField="NO2" HeaderText="NO2" />
<asp:BoundField DataField="NOx" HeaderText="NOx" />
<asp:BoundField DataField="CO2" HeaderText="CO2" />
<asp:BoundField DataField="Temp" HeaderText="Temp" />
<asp:BoundField DataField="WD" HeaderText="WD" />
<asp:BoundField DataField="WS" HeaderText="WS" />
<asp:BoundField DataField="RH" HeaderText="RH" />
<asp:BoundField DataField="SR" HeaderText="SR" />
<asp:BoundField DataField="Rain_Gauge" HeaderText="Rain_Gauge" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" ForeColor="White" Font-Bold="True"></HeaderStyle>
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
</div>
</form>
</body>
</html>
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Drawing;
namespace CSV
{
public partial class WebForm3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//string filePath = "//10.10.4.15/daily hr data/AQ1/AQ1.csv";
//string filePath = @"\\10.10.4.15\daily hr data\AQ1\AQ1.csv";
string filePath = @"D:\AAQMS Data\AQ1.csv";
string csvData = " ";
using (var stream = File.Open(filePath, FileMode.Open))
{
using (var reader = new StreamReader(stream))
{
//Read the contents of CSV file.
csvData = reader.ReadToEnd();
}
}
//Create a DataTable.
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("Parameters", typeof(string)),
new DataColumn("PM10", typeof(string)),
new DataColumn("PM2.5",typeof(string)),
new DataColumn("SO2", typeof(string)),
new DataColumn("NO", typeof(string)),
new DataColumn("NO2", typeof(string)),
new DataColumn("NOx", typeof(string)),
new DataColumn("CO2", typeof(string)),
new DataColumn("Temp" ,typeof(string)),
new DataColumn("WD", typeof(string)),
new DataColumn("WS", typeof(string)),
new DataColumn("RH",typeof(string)),
new DataColumn("SR", typeof(string)),
new DataColumn("Rain_Gauge",typeof(string)),
new DataColumn(" ",typeof(string)),
new DataColumn(".",typeof(string)),
});
// dt.Rows.Add("UoM", "µg/m3", "µg/m3", "µg/m3", "µg/m3", "µg/m3", "µg/m3", "µg/m3", "degreC", "DEGREE", "m/s", "%", "W/m2", "m m");
//Execute a loop over the rows.
bool firstRow = true;
// foreach (string row in csvData.Split('\n'))
foreach (string row in System.IO.File.ReadAllLines(filePath).Skip(6)) // .Skip(1) is for skipping header
{
if (firstRow)
{
firstRow = false;
}
else
{
if (!string.IsNullOrEmpty(row))
{
int i = 0;
DataRow dr = dt.NewRow();
foreach (string cell in row.Split(';'))
{
dr[i] = cell;
i++;
}
dt.Rows.Add(dr);
}
}
}
//Bind the DataTable.
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void OnDataBound(object sender, EventArgs e)
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
TableHeaderCell cell = new TableHeaderCell();
cell.Text = "UoM";
cell.ColumnSpan = 1;
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "µg/m3";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "µg/m3";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "µg/m3";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "µg/m3";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "µg/m3";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "µg/m3";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "µg/m3";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "degreC";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "degree";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "m/s";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "%";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "w/m2";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "mm";
row.Controls.Add(cell);
row.BackColor = ColorTranslator.FromHtml("#3AC0F2");
GridView1.HeaderRow.Parent.Controls.AddAt(0, row);
}
}
}