Hello, I have an error when trying to display gridview, and the way i use to display it is by reading from a notepad in a folder. It works for some notes but for others not. Is it because of the size of the data? eg. the one that can be display is 188KB
size while the one i cannot display is 35, 371KB. This is the error:
Index was outside the bounds of the array
This is my code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//initialize to a specified path
DirectoryInfo di = new DirectoryInfo(@"filepath");
//returns file list from the current directory
FileInfo[] fi = di.GetFiles();
//retrieve the file list
DropDownList1.DataSource = fi;
//sets the name of the textfile as ddl
//from FileInfo properties
DropDownList1.DataTextField = "Name";
//sets the path of the file to the ddl
DropDownList1.DataValueField = "Fullname";
//binds the data to ddl
DropDownList1.DataBind();
//If did not write AppendDataBoundItems="true" at .aspx, then can write this
//DropDownList1.Items.Insert(0, "- ALL -");
}
}
protected void Index_Changed(Object sender, EventArgs e)
{
//gets the value in form of gridview and bind to the selected ddl
GridView1.DataSource = tb(DropDownList1.SelectedValue);
GridView1.DataBind();
}
public DataSet tb(String read)
{
//create an array
string[] results;
//the type of delimiter
char[] delimiter = new char[] { '\t' };
//to stream the textfile
StreamReader sr = new StreamReader(read);
//create data table & data set
DataTable dt = new DataTable();
DataSet ds = new DataSet();
//Add columns
dt.Columns.Add("type");
dt.Columns.Add("session");
dt.Columns.Add("thread");
dt.Columns.Add("time");
dt.Columns.Add("ip");
dt.Columns.Add("text");
string input;
//while the data in the textfile is not at the end
while (!sr.EndOfStream)
{
//read the lines from the text data
input = sr.ReadLine();
//split the words and store into array
results = input.Split(delimiter);
string a;
string b;
string c;
string d;
string f;
string g;
if (results[0] == "\"APPLICATION\"")
{
//column====data
a = results[0];
b = results[1];
c = null;
d = results[2];
f = null;
g = results[3];
//Add rows
dt.Rows.Add(a, b, c, d, f, g);
}
else
{
a = results[0];
b = results[1];
c = results[2];
d = results[3];
f = results[4];
g = results[5];
dt.Rows.Add(a, b, c, d, f, g);
}
}
ds.Tables.Add(dt);
return ds;
}
colol
Member
117 Points
542 Posts
Error in displaying gridview
Nov 22, 2012 01:51 AM|LINK
Hello, I have an error when trying to display gridview, and the way i use to display it is by reading from a notepad in a folder. It works for some notes but for others not. Is it because of the size of the data? eg. the one that can be display is 188KB size while the one i cannot display is 35, 371KB. This is the error:
Index was outside the bounds of the array
This is my code:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //initialize to a specified path DirectoryInfo di = new DirectoryInfo(@"filepath"); //returns file list from the current directory FileInfo[] fi = di.GetFiles(); //retrieve the file list DropDownList1.DataSource = fi; //sets the name of the textfile as ddl //from FileInfo properties DropDownList1.DataTextField = "Name"; //sets the path of the file to the ddl DropDownList1.DataValueField = "Fullname"; //binds the data to ddl DropDownList1.DataBind(); //If did not write AppendDataBoundItems="true" at .aspx, then can write this //DropDownList1.Items.Insert(0, "- ALL -"); } } protected void Index_Changed(Object sender, EventArgs e) { //gets the value in form of gridview and bind to the selected ddl GridView1.DataSource = tb(DropDownList1.SelectedValue); GridView1.DataBind(); } public DataSet tb(String read) { //create an array string[] results; //the type of delimiter char[] delimiter = new char[] { '\t' }; //to stream the textfile StreamReader sr = new StreamReader(read); //create data table & data set DataTable dt = new DataTable(); DataSet ds = new DataSet(); //Add columns dt.Columns.Add("type"); dt.Columns.Add("session"); dt.Columns.Add("thread"); dt.Columns.Add("time"); dt.Columns.Add("ip"); dt.Columns.Add("text"); string input; //while the data in the textfile is not at the end while (!sr.EndOfStream) { //read the lines from the text data input = sr.ReadLine(); //split the words and store into array results = input.Split(delimiter); string a; string b; string c; string d; string f; string g; if (results[0] == "\"APPLICATION\"") { //column====data a = results[0]; b = results[1]; c = null; d = results[2]; f = null; g = results[3]; //Add rows dt.Rows.Add(a, b, c, d, f, g); } else { a = results[0]; b = results[1]; c = results[2]; d = results[3]; f = results[4]; g = results[5]; dt.Rows.Add(a, b, c, d, f, g); } } ds.Tables.Add(dt); return ds; }oned_gk
All-Star
31373 Points
6412 Posts
Re: Error in displaying gridview
Nov 22, 2012 03:53 AM|LINK