Hello All,
I am working on a project that displays error log files in GridView. My current code opens .txt files but I am having problem opening files in .log file format.
Can someone please help me around this issue?
Here is my Code:
aspx code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DisplayTextFile.aspx.cs" Inherits="CSV_to_Browser.DisplayTextFile" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link href="StyleSheet1.css" rel="stylesheet" type="text/css" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="wrapper">
<div id="content" style="margin-left: 0px">
<h2 style="text-align: center; margin-top: 10px;">Error Logs Tracker</h2>
<p style="text-align: left"> </p>
<br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<br />
<asp:GridView CssClass="" ID="GridView1" runat="server" AllowSorting="True" OnSorting="GridView1_Sorting" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<br />
</div>
</div>
<br />
<br />
</form>
</body>
</html>
aspx.cs code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Diagnostics;
namespace CSV_to_Browser
{
public partial class DisplayTextFile : System.Web.UI.Page
{
private string SortDirection = "ASC";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string spath = Server.MapPath("~/App_Data/2016-05-16 ETRAccuracyWinService_Transaction.txt");
DataTable dt = ConvertCSVtoDataTable(spath);
GridView1.DataSource = dt;
GridView1.DataBind();
ViewState["tables"] = dt;
}
}
public DataTable ConvertCSVtoDataTable(string strFilePath)
{
StreamReader sr = new StreamReader(strFilePath);
string[] headers = sr.ReadLine().Split(',' , ']');
DataTable dt = new DataTable();
foreach (string header in headers)
{
dt.Columns.Add(header);
}
while (!sr.EndOfStream)
{
string[] rows = sr.ReadLine().Split(',', ']');
DataRow dr = dt.NewRow();
for (int i = 0; i < headers.Length; i++)
{
dr[i] = rows[i];
Debug.WriteLine(i.ToString());
}
dt.Rows.Add(dr);
}
return dt;
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
if (ViewState["tables"] != null)
{
DataTable dt = (DataTable)ViewState["tables"];
//Sort the data.
dt.DefaultView.Sort = e.SortExpression + " " + SortDirection;
GridView1.DataSource = dt;
GridView1.DataBind();
}
SetSortDirection(SortDirection);
}
protected void SetSortDirection(string sortDirection)
{
if (sortDirection == "ASC")
{
SortDirection = "DESC";
}
else
{
SortDirection = "ASC";
}
}
}
}