Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read the string value of a cell #452

Closed
jamesgurung opened this issue Dec 22, 2019 · 2 comments
Closed

Read the string value of a cell #452

jamesgurung opened this issue Dec 22, 2019 · 2 comments

Comments

@jamesgurung
Copy link

I am trying to get the string value of a cell which might contain text or numeric data, or might be blank. However, when I call reader.GetString(i) and the column is blank or contains numeric data, I receive an error, for example:

Unable to cast object of type 'System.Double' to type 'System.String'

Therefore, to get the value as a string, I am needing to write:

string val = reader.IsDBNull(i) ? null : (
  reader.GetFieldType(i) == typeof(double)
  ? reader.GetDouble(i).ToString()
  : reader.GetString(i)
);

Is there a better way of doing this?

@appel1
Copy link
Collaborator

appel1 commented Dec 22, 2019

What is your goal here?

If you want the string to be like what is shown in Excel then look at https://github.com/ExcelDataReader/ExcelDataReader/blob/develop/README.md#formatting

If you just want the value converted to some string using the current culture them something like string s = reader.GetValue(i)?.ToString() should work I think.

@jamesgurung
Copy link
Author

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants