How To Read and BindData Text File To GridView Using Asp.net
Please solve,
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Upendra Pratap ShahiPosted Aug 27, 2015, 6:13 AM
Design Page-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReadTexFile.aspx.cs" Inherits="_Default" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Pagetitle>
head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
asp:GridView>
<br />
<asp:Label ID="lblError" runat="server" ForeColor="Red">asp:Label> div>
form>
body>
html>
Code behind page-
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string OpenPath,contents;
int tabSize = 4;
string[] arInfo;
string line;
// Create new DataTable.
DataTable table = CreateTable();
DataRow row;
try
{
OpenPath = Server.MapPath(".") + @"\MyFile.log";
string FILENAME = OpenPath;
//Get a StreamReader class that can be used to read the file
StreamReader objStreamReader;
objStreamReader = File.OpenText(FILENAME);
while ((line = objStreamReader.ReadLine()) != null)
{
contents = line.Replace(("").PadRight(tabSize, ' '), "\t");
// define which character is seperating fields
char[] textdelimiter = { ']' };
arInfo = contents.Split(textdelimiter);
for (int i = 0; i <= arInfo.Length; i++)
{
row = table.NewRow();
if (i < arInfo.Length)
row["Type"] = arInfo[i].ToString().Replace("[", " ");
if (i + 1 < arInfo.Length)
row["Source"] = arInfo[i + 1].ToString().Replace("[", " ");
if (i + 2 < arInfo.Length)
row["Time"] = arInfo[i + 2].ToString().Substring(1);
if (i + 3 < arInfo.Length)
{
row["Description"] = arInfo[i + 3].ToString().Replace("[", " ");
table.Rows.Add(row);
}
i = i + 2;
}
}
objStreamReader.Close();
// Set to DataGrid.DataSource property to the table.
GridView1.DataSource = table;
GridView1.DataBind();
}
catch (Exception ex)
{
lblError.Text = ex.Message;
}
}
}
private DataTable CreateTable()
{
try
{
DataTable table = new DataTable();
// Declare DataColumn and DataRow variables.
DataColumn column;
// Create new DataColumn, set DataType, ColumnName
// and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "Type";
table.Columns.Add(column);
// Create second column.
column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "Time";
table.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "Source";
table.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "Description";
table.Columns.Add(column);
return table;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
MyFile.txt data
[GENTS][EROR][7/13/2006 3:37:17 PM]Failed to Initialize Server
[AGENTS][INFO][7/14/2006 11:38:14 AM]Microsoft Excel Agent completed successfully
[AGENTS][INFO][7/14/2006 11:51:27 AM]Microsoft Excel Agent completed successfully
[AGENTS][INFO][7/14/2006 11:52:06 AM]Microsoft Excel Agent completed successfully
[AGENTS][EROR][7/14/2006 1:39:18 PM]Unable to save file. Invalid file path or path does not exist.
Manas MohapatraPosted Aug 27, 2015, 6:01 AM
Rajeesh MenothPosted Aug 27, 2015, 5:56 AM