views:

32

answers:

3

I have an Excel spreadsheet with, among onther things, numbers that are identifiers.
My odbc reader should return me something like '55201562000016', but too often it returns me something like '5.52016e+008'.

I only do the following, but apparently it does not suffice to tell the reader that it should be treated as a string and not an exponential integer :

siren = rdrxls("entr_siren").ToString().Trim()
siret = rdrxls("entr_siret").ToString().Trim()

I have tried this :

siren = Convert.ToInt64(rdrxls("entr_siren")).ToString().Trim()
siret = Convert.ToInt64(rdrxls("entr_siret")).ToString().Trim()

It should work, but sometimes it throws exceptions, because sometimes the data is erroneous (with letters for example) ; but I still need to fetch it.

Any idea ?

Thanks

A: 

I think, the following way could help you to overcome the problem :

double converted=0;
double.TryParse("5.52016e+008", out converted);
Siva Gopal
It solves my problem for the fields with the letters, but it seems to think '5.52016e+008' is not a number...
cosmo0
When i tested the code which i posted above, it was able to convert the number 5.52016e+008 into 552016000. Isn't the case with you? Just let me know. If tryparse(...) is provided with a string, suppose like "abc", then since it can't convert it to specified numeric type, simply it will return you the default value for the specified numeric type.
Siva Gopal
A: 

IIRC, the Excel ODBC reader looks at the first eight rows to "infer" the data type for the columns.

The first row is taken as column headers, so putting text in the second row should be enough to force it to infer a string data type.

Once you've done that

rdrxls("entr_siren")

should return a string, not any kind of numeric.

Bevan
I don't think it's a solution, as I don't have much control over which rows will be the first.
cosmo0
+1  A: 

You could use Int64.TryParse() to see if you can cast the result into an Int64. TryParse() returns a boolean, indicating whether the cast was successfull or not. If it failed, keep the value as is, if it was successfull, overwrite the previously stored value with the ToString() of the cast result.

double sirenValue;
double siretValue;

siren = rdrxls("entr_siren").ToString().Trim();
siret = rdrxls("entr_siret").ToString().Trim();


if (double.TryParse(siren, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out sirenValue))
{
    siren = sirenValue.ToString();
}

if (double.TryParse(siret, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out siretValue))
{
    siret = siretValue.ToString();
}

Hope this helps.

François
Same thing as Siva Gopal : it does not seems to be able to convert '5.52016e+008'.
cosmo0
appologies cosmo0, please find I edited my answer to solve that issue now. I tested it in my app and it did convert 5.52016e+008 into 552016000. You must specify the Number Style, which in this case must be a float for it to convert.
François
Great, it works ! Thanks !
cosmo0