Hi,
I have a tab delimeted file. I want to read this file and want to put in
data table. I have the following code. Tab delimeted has 3c columns and 43
lines of data. The following code just reads the columns after it crashes.
How to fix it and How can I populate the data for each column.
public void CreateTable()
{
DataColumn col = new DataColumn();
col.ColumnName = "Name";
col.DataType = System.Type.GetType("System.String");
myTable.Columns.Add(col);
DataColumn col1 = new DataColumn();
col1.ColumnName = "Age";
//col1.DataType = System.Type.GetType("System.Int32");
col1.DataType = System.Type.GetType("System.String");
myTable.Columns.Add(col1);
DataColumn col2 = new DataColumn();
col2.ColumnName = "3";
col2.DataType = System.Type.GetType("System.String");
myTable.Columns.Add(col2);
string path = "";
do
{
path = GetFile();
}
while (path == ""); // repeat til you select a file! probably
need a better way of handling this that doesn't loop infinitely without a
file specified
string[] myContents = ReadFileLines(path);
for (int z = 0; z < myContents.Length; z++)
{
//string[] line = String.Split(myContents[z],"\t");
string[] line = myContents[z].Split('\t');
DataRow row = myTable.NewRow();
row["Name"] = line[0];
row["Age"] = line[1];
row["3"] = line[2];
myTable.Rows.Add(row);
}
}