473,385 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Get Data

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);
}
}
Nov 9 '07 #1
3 1380
On 2007-11-09 13:58:00 -0800, bobby <bo***@discussions.microsoft.comsaid:
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

Nov 9 '07 #2
Liz

"bobby" <bo***@discussions.microsoft.comwrote in message
news:B9**********************************@microsof t.com...
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);
}
}

Nov 10 '07 #3
I believe that the following allows you to specify the delimiters, and
exposes itself via the common IDataReader interface - this makes it
trivial for loading data from csv/tsv formats:

http://www.codeproject.com/cs/database/CsvReader.asp

Marc

Nov 10 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Chris | last post by:
Could someone please provide me an effective means of exporting data from a data set (or data grid) to Excel?
9
by: Tony Lee | last post by:
Some time a ago, on this newsgroup the following comments were made in recommending good references for Access (2003) >I used to recommend Dr. Rick Dobson's, "Programming Access <version>" for...
1
by: djozy | last post by:
Please, I want to insert data into SQL Server database. I know for this commmand: SqlCommand myCommand= new SqlCommand("INSERT INTO table (Column1, Column2) " + "Values ('string', 1)",...
1
by: T8 | last post by:
I have a asp.net (framework 1.1) site interfacing against SQL 2000. It runs like a charm 99% of the time but once in a while I get the following "unspecified error". Sometimes it would resolve by...
0
by: NicK chlam via DotNetMonster.com | last post by:
this is the error i get System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. at System.Data.Common.DbDataAdapter.Update(DataRow dataRows, DataTableMapping tableMapping) at...
3
by: bbernieb | last post by:
Hi, All, Is it possible to access a variable inside of a data binding, without the variable being out of scope? (Note: On the DataBinder line, I get an error message that says "Name 'i' is...
5
by: Gene | last post by:
What can I do if I want to get the result using the sql command? for example, the select command is "select Name from Employee where StaffID=10" How to get the "Name"??? dim Name as string and...
5
by: DC Gringo | last post by:
I am having a problem reading a simple update to the database. Basically I'm testing a small change to the pubs database -- changing the price of the Busy Executive's Database Guide from 19.99 to...
14
by: Rolf Welskes | last post by:
Hello, I have an ObjectDataSource which has as business-object a simple array of strings. No problem. I have an own (custom) control to which I give the DataSourceId and in the custom-control...
0
by: Winder | last post by:
Computer Data Recovery Help 24/7 Data recovering tools and services is our focus. We will recover your data in a cost effective and efficient manner. We recover all operating systems and media....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.