I'm reading data from SQLite database table.
If the SQLiteDataReader encounters a negative double an InvalidCastException is thrown.

Here is my code.

public void DBToArray()
        {
            string stm = "SELECT * FROM mtable";

            SQLiteCommand cmd = new SQLiteCommand(stm, sqlcon);

            SQLiteDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                Console.WriteLine(
                    rdr.GetInt32(0) + " " +
                    rdr.GetString(1) + " " +
                    rdr.GetString(2) + " " +
                    rdr.GetString(3) + " " +
                    rdr.GetInt32(4) + " " +
                    rdr.GetDouble(5) + " " + /* -5.0 no good */
                    rdr.GetString(6) + " " +
                    rdr.GetString(7)
                    );
            }


        }

Just wondering if anyone has encountered and solved this issue?

I could make that particular double column a string and cast it later with Convert.ToDouble but I'd sooner not if there is another solution.

I cannot find any information on System.Data.SQLite having a problem with negative double.

Thanks for looking.

The code above is fine, it was user error.

I'm very sorry if I've wasted anyone's time.

It was just a coincidence that I had another problem in the rows in which negative values are stored.

A default string was entered into column(0) when a value was not found during table creation.

Sorry for any inconvenience.

i have same problem, when i read this, i remember that i just forgot to change reader.GetInt32(0).ToString to reader.GetString(0)

[InvalidCastException: Specified cast is not valid.]
System.Data.SqlClient.SqlBuffer.get_Int32() +985292
System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i) +39
Allocate.Init() in c:\Inetpub\wwwroot\App_Code\Allocate.cs:156
Adjustments_AllocateDetails.Page_Load(Object sender, EventArgs e) in c:\Inetpub\wwwroot\Adjustments\AllocateDetails.aspx.cs:25
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.