Problemer med DataRow igen! C#
Hej igen.Fik det til at virke i går, med kyndig hjælp. Så skulle jeg selvfølgelig prøve at smide det i en klasse, og så kagede det helt ud for mig.
Jeg får flg. fejl :
Server Error in '/' Application.
--------------------------------------------------------------------------------
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0165: Use of unassigned local variable 'drLine'
Source Error:
Line 53: for (int i = 0; i < strFields.Length; i++)
Line 54: {
Line 55: drLine[i] = strFields[i];
Line 56: }
Line 57: dt.Rows.Add(drLine);
Source File: Default.aspx.cs Line: 55
Source kommer her :
ASPX :
-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<div>
<asp:Button ID="btnAabn" runat="server" Text="Åbn log-fil" OnClick="btnAabn_Click" />
<asp:DataGrid ID="dgLog" runat="server" AutoGenerateColumns="true">
</asp:DataGrid>
<br />
</div>
</form>
</body>
</html>
CS :
-
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;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
//using BaseData;
public partial class _Default : System.Web.UI.Page
{
protected class LogFile
{
public String test="";
public String path;
public String filename;
public DataTable dt = new DataTable();
public void readLogFile()
{
DataRow drLine;
TextReader trLogFile = new StreamReader(path+filename, System.Text.Encoding.Default);
while (true)
{
string[] strFields;
char[] separators = { ' ' };
string strLine = trLogFile.ReadLine();
if (strLine == null) break;
if (strLine.Substring(0, 1).Equals("#"))
{
if (strLine.Substring(0, 8).Equals("#Fields:"))
{
strFields = strLine.Split(separators);
for (int i = 0; i < strFields.Length; i++)
{
if (i > 0)
{
if (!dt.Columns.Contains(strFields[i]) && !string.IsNullOrEmpty(strFields[i]))
{
dt.Columns.Add(new DataColumn(strFields[i], typeof(string)));
}
}
}
}
}
else
{
strFields = strLine.Split(separators);
for (int i = 0; i < strFields.Length; i++)
{
drLine[i] = strFields[i];
}
dt.Rows.Add(drLine);
}
}
dt.AcceptChanges();
trLogFile.Close();
} // readLogFile
} // class LogFile
protected class LogDatabase
{
String tmp;
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAabn_Click(object sender, EventArgs e)
{
LogFile lokallogfil = new LogFile();
lokallogfil.path = "c:\\";
lokallogfil.filename = "ex051208.log";
lokallogfil.readLogFile();
DataView dv = new DataView(lokallogfil.dt);
dgLog.DataSource = dv;
dgLog.DataBind();
// Response.Write("<br />" + "|" + lokallogfil.path + lokallogfil.filename + "|" + "<br />");
Response.Write("<br />" + lokallogfil.test + "<br />");
}
}
Håber I kan hjælpe mig med at finde fejlen, for - endnu en gang - er jeg helt blank.
Mvh.
/Mnc
