Get Data

G

Guest

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);
}
}
 
P

Peter Duniho

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.

Define "crashes". Is the code throwing an exception? If so, what
exception? On which line?

I don't see anything fundamentally wrong with the code, but it has
basically no error checking. So if the data is even a little bit
different from that which the code requires, it will fail.

Without more specific information from you, it's not really possible to
say what's wrong. But I'd suspect the data is in fact not precisely
that which is required. Fortunately, it's very easy to find out in
what way, simply by running the code in the debugger and looking at the
various variables when an exception occurs. The variable "z" will tell
which you line of data causes the problem, "myContents[z]" will tell
you the contents of that line, and of course "line" will tell you how
the line was split up by the String.Split() method.

Debuggers are wonderful things. They can really help you out when
things aren't working right. This would be a good opportunity to use
one. :)

Pete
 
L

Liz

bobby said:
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.

should we assume you're declared and instantiated "myTable" ? and
"ReadFileLines" is defined and accessible? and returns string[] ?

what do you mean it "reads the columns after it crashes" ? you mean it's
*supposed* to read them ?

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);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top