471,610 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How to read Hyphenated Values from CSV File?

Hi,

I'm using the OleDB namespace to open a CSV File. I can read the file
sucessfully except for hyphenated values. My CSV File contains phone numbers
and sometimes these come separated with hyphens (ie: 555-123-4567). These
fields come as empty using the OleDB namespace and as dbNull when using the
ODBC namespace. Is there a way to prevent this from happening?

Thanks in advance,
Dec 7 '05 #1
4 2514
On Wed, 7 Dec 2005 10:38:02 -0800, "Antonio Tirado"
<An***********@discussions.microsoft.com> wrote:
Hi,

I'm using the OleDB namespace to open a CSV File. I can read the file
sucessfully except for hyphenated values. My CSV File contains phone numbers
and sometimes these come separated with hyphens (ie: 555-123-4567). These
fields come as empty using the OleDB namespace and as dbNull when using the
ODBC namespace. Is there a way to prevent this from happening?

Thanks in advance,


CSV should not have any problem with hyphens. What is your phone
number field expecting? If it is expecting digits only then hyphens
will cause a problem.

Try breaking the problem into two steps:
1 Load the phone number field into a simple text field and check
that it arrives correctly.
2 Once you have the text field part working then put the text into
the right format for your phone number field.

You can recombine the two steps into one once you are happy you
understand what is going on.

rossum

The ultimate truth is that there is no ultimate truth
Dec 7 '05 #2
Hi,

I used a DataReader to parse the CSV File. And for my Phone number I'm
expecting a string because sometimes phone numbers can be hyphenated. I tried
using GetString but this fails for other fields. With GetValue the phone
number comes either empty or null. I don't understand how to use the function
GetValues(). Nothing I tried seems to work.

Just in case, this is the code I'm using to get the values from the CSV File:

System.Data.OleDb.OleDbDataReader ParseCSVFile = CSVFile.ExecuteReader();

while (ParseCSVFile.Read())
{
string[] Line = new string[ParseCSVFile.FieldCount];
for (int i = 0; i < ParseCSVFile.FieldCount; i++)
Line[i] = ParseCSVFile.GetValue(i).ToString();
(...)

Thanks in advance,
"rossum" wrote:
On Wed, 7 Dec 2005 10:38:02 -0800, "Antonio Tirado"
<An***********@discussions.microsoft.com> wrote:
Hi,

I'm using the OleDB namespace to open a CSV File. I can read the file
sucessfully except for hyphenated values. My CSV File contains phone numbers
and sometimes these come separated with hyphens (ie: 555-123-4567). These
fields come as empty using the OleDB namespace and as dbNull when using the
ODBC namespace. Is there a way to prevent this from happening?

Thanks in advance,


CSV should not have any problem with hyphens. What is your phone
number field expecting? If it is expecting digits only then hyphens
will cause a problem.

Try breaking the problem into two steps:
1 Load the phone number field into a simple text field and check
that it arrives correctly.
2 Once you have the text field part working then put the text into
the right format for your phone number field.

You can recombine the two steps into one once you are happy you
understand what is going on.

rossum

The ultimate truth is that there is no ultimate truth

Dec 8 '05 #3
On Thu, 8 Dec 2005 04:36:04 -0800, "Antonio Tirado"
<An***********@discussions.microsoft.com> wrote:
Hi,

I used a DataReader to parse the CSV File. And for my Phone number I'm
expecting a string because sometimes phone numbers can be hyphenated. I tried
using GetString but this fails for other fields. With GetValue the phone
number comes either empty or null. Then use GetString() for the phone number fields and GetValue for the
other fields.
I don't understand how to use the function
GetValues(). Nothing I tried seems to work.

Just in case, this is the code I'm using to get the values from the CSV File:
// Returns true if index refers to a phone number field,
// false otherwise.
private bool isPhoneNumberIndex(int index) { /***/ }
System.Data.OleDb.OleDbDataReader ParseCSVFile = CSVFile.ExecuteReader();

while (ParseCSVFile.Read())
{
string[] Line = new string[ParseCSVFile.FieldCount];
for (int i = 0; i < ParseCSVFile.FieldCount; i++) if (isPhoneNumberIndex(i) {
Line[i] = ParseCSVFile.GetString(i);

/*** Diagnostic code ***/
string message = "Field[" +
i.ToString() +
"] = " +
Line[i];
MessageBox.Show(message, "Phone Number");
/***********************/
}
else { Line[i] = ParseCSVFile.GetValue(i).ToString(); } // end if(...)
A minor point, in for-loops ++i is never slower and may be faster than
i++.

rossum

Thanks in advance,
"rossum" wrote:
On Wed, 7 Dec 2005 10:38:02 -0800, "Antonio Tirado"
<An***********@discussions.microsoft.com> wrote:
>Hi,
>
>I'm using the OleDB namespace to open a CSV File. I can read the file
>sucessfully except for hyphenated values. My CSV File contains phone numbers
>and sometimes these come separated with hyphens (ie: 555-123-4567). These
>fields come as empty using the OleDB namespace and as dbNull when using the
>ODBC namespace. Is there a way to prevent this from happening?
>
>Thanks in advance,


CSV should not have any problem with hyphens. What is your phone
number field expecting? If it is expecting digits only then hyphens
will cause a problem.

Try breaking the problem into two steps:
1 Load the phone number field into a simple text field and check
that it arrives correctly.
2 Once you have the text field part working then put the text into
the right format for your phone number field.

You can recombine the two steps into one once you are happy you
understand what is going on.

rossum

The ultimate truth is that there is no ultimate truth

The ultimate truth is that there is no ultimate truth
Dec 10 '05 #4
Hi, I managed to used the GetValues() function but the problem is still
happening. Thanks for the detail about ++i anyways.

Just to sum it up: WHen parsing hyphenated phone numbers from a CSV File
using the OleDB Space I'm getting DBNull values. Someone tried my exact code
in his own PC and it works perfectly. Could this be related to the file?

"rossum" wrote:
On Thu, 8 Dec 2005 04:36:04 -0800, "Antonio Tirado"
<An***********@discussions.microsoft.com> wrote:
Hi,

I used a DataReader to parse the CSV File. And for my Phone number I'm
expecting a string because sometimes phone numbers can be hyphenated. I tried
using GetString but this fails for other fields. With GetValue the phone
number comes either empty or null.

Then use GetString() for the phone number fields and GetValue for the
other fields.
I don't understand how to use the function
GetValues(). Nothing I tried seems to work.

Just in case, this is the code I'm using to get the values from the CSV File:

// Returns true if index refers to a phone number field,
// false otherwise.
private bool isPhoneNumberIndex(int index) { /***/ }
System.Data.OleDb.OleDbDataReader ParseCSVFile = CSVFile.ExecuteReader();

while (ParseCSVFile.Read())
{
string[] Line = new string[ParseCSVFile.FieldCount];
for (int i = 0; i < ParseCSVFile.FieldCount; i++)

if (isPhoneNumberIndex(i) {
Line[i] = ParseCSVFile.GetString(i);

/*** Diagnostic code ***/
string message = "Field[" +
i.ToString() +
"] = " +
Line[i];
MessageBox.Show(message, "Phone Number");
/***********************/
}
else {
Line[i] = ParseCSVFile.GetValue(i).ToString();

} // end if
(...)


A minor point, in for-loops ++i is never slower and may be faster than
i++.

rossum

Thanks in advance,
"rossum" wrote:
On Wed, 7 Dec 2005 10:38:02 -0800, "Antonio Tirado"
<An***********@discussions.microsoft.com> wrote:

>Hi,
>
>I'm using the OleDB namespace to open a CSV File. I can read the file
>sucessfully except for hyphenated values. My CSV File contains phone numbers
>and sometimes these come separated with hyphens (ie: 555-123-4567). These
>fields come as empty using the OleDB namespace and as dbNull when using the
>ODBC namespace. Is there a way to prevent this from happening?
>
>Thanks in advance,

CSV should not have any problem with hyphens. What is your phone
number field expecting? If it is expecting digits only then hyphens
will cause a problem.

Try breaking the problem into two steps:
1 Load the phone number field into a simple text field and check
that it arrives correctly.
2 Once you have the text field part working then put the text into
the right format for your phone number field.

You can recombine the two steps into one once you are happy you
understand what is going on.

rossum

The ultimate truth is that there is no ultimate truth

The ultimate truth is that there is no ultimate truth

Dec 10 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Nils Wogatzky | last post: by
2 posts views Thread by joerg pfeffer | last post: by
14 posts views Thread by Larry R Harrison Jr | last post: by
3 posts views Thread by Sobhan | last post: by
2 posts views Thread by RyanS09 | last post: by
13 posts views Thread by rohit | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

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.