How to read Hyphenated Values from CSV File?

G

Guest

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,
 
R

rossum

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
 
G

Guest

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 = ParseCSVFile.GetValue(i).ToString();
(...)

Thanks in advance,
 
R

rossum

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 = ParseCSVFile.GetString(i);

/*** Diagnostic code ***/
string message = "Field[" +
i.ToString() +
"] = " +
Line;
MessageBox.Show(message, "Phone Number");
/***********************/
}
else {
Line = 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,


The ultimate truth is that there is no ultimate truth
 
G

Guest

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 said:
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 = ParseCSVFile.GetString(i);

/*** Diagnostic code ***/
string message = "Field[" +
i.ToString() +
"] = " +
Line;
MessageBox.Show(message, "Phone Number");
/***********************/
}
else {
Line = 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,


The ultimate truth is that there is no ultimate truth
 

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