2

I am trying to read some decimal values from my DB. These values are the order of 10-6. I am trying to read it into a double variable in C# using code like this: sqltype in database is "float". sample value to be read is "1.99999999495049E-06" –

Double [] rmseValues = null;

while (dataReader.Read())
{
    // This shows my datatype is float (sql)
    string temp = dataReader.GetDataTypeName(0);

    // This returns a value of "0"
    string temp1 = dataReader.GetOrdinal("RmseAll").ToString();

    // This throws exception of invalid typecast
    rmseValues[0] = dataReader.GetFloat(0);
}
8
  • 4
    How are planning on putting a fraction in an integer? Commented Aug 24, 2015 at 8:21
  • Sorry, for mistake, i am reading in a "double" variable Commented Aug 24, 2015 at 8:24
  • 1
    @AnujDubey I suspect you should actually be using a decimal here... what is the sql type in the database, and what does the value represent? Commented Aug 24, 2015 at 8:25
  • 1
    If you are actually reading in a double, then: GetDouble Commented Aug 24, 2015 at 8:27
  • 1
    @Chris Thanks . modified question Commented Aug 24, 2015 at 8:41

3 Answers 3

1

Try to use GetDouble(0) instead of GetFloat(0)

I think you will also need to edit that line :

Double [] rmseValues = null;

In fact your are trying to put values inside a null object as solution you need to initialize your rmseValues array or just use a List of double

Double[] rmseValues = new Double[10];
Sign up to request clarification or add additional context in comments.

7 Comments

that throws an exception "{"Object reference not set to an instance of an object."}"
That is a different problem, you need to show the code in this case.
@AnujDubey if rmseValues[0] = dataReader.GetFloat(0); throws a typecase exception, and rmseValues[0] = dataReader.GetDouble(0); throws a null-reference exception, then: rmseValues is null - so go fix that
@LasseV.Karlsen I'm guessing Double [] rmseValues = null; isn't helping ;p
Still had the old copy of the question on my screen, that explains it.
|
0

Use GetInt64(0) instead of GetFloat(0)

1 Comment

That wouldn't help store a value like the one cited
0

In order not be dependent on RDBMS actual background type (say, NUMBER(10, 3) or alike) and its representation as .Net Type (e.g. Single) do a conversion:

  // rmseValues[0] should be Double
  rmseValues[0] = Convert.ToDouble(dataReader.GetValue(0));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.