tags:

views:

161

answers:

2

I keep getting a InvalidCastException when I'm fetching any double from my SQLite database in C#. The exception says "Specified cast is not valid."

I am able to see the value in a SQL manager so I know it exists. It is possible to fetch Strings (VARCHARS) and ints from the database. I'm also able to fetch the value as an object but then I get "66.0" when it's suppose to be "66,8558604947586" (latitude coordination).

Any one who knows how to solve this?

My code:

using System.Data.SQLite;

...

SQLiteConnection conn = new SQLiteConnection(@"Data Source=C:\\database.sqlite;    Version=3;");
conn.Open();
SQLiteDataReader reader = getReader(conn, "SELECT * FROM table");

//These are working 
String name = reader.GetString(1);
Int32 value = reader.GetInt32(2);

//This is not working
Double latitude = reader.getDouble(3);

//This gives me wrong value
Object o = reader[3]; //or reader["latitude"] or reader.getValue(3)
A: 

You may want to check the data type you used to store the value in the database. It seems like you may be storing an integer but trying to retrieve it as a double.

LBushkin
It's stored as a double and SQLite Manager is able to fetch the whole value.
Irro
A: 

Is the actual table schema defined to use a DOUBLE? SQLite will do some translation since the DB engine is typeless. If it is actually a SQL DOUBLE column, you could try a cast as well.

More information on SQLite type affinity.

Stephen Cleary
I tried a "reader.GetFieldType(3)" as Jacob suggested and it returned that it was defined as a Double. What do you mean by "try a cast as well"?
Irro
I meant add a SQL cast to double, just to make absolutely sure it's the correct type. However, if GetFieldType does return double then the SQL cast wouldn't help.
Stephen Cleary