How to get float value with SqlDataReader?
It's time for a little table, I think.
T-SQL type name | .NET equivalent | C# type name | DataReader method |
---|---|---|---|
FLOAT | System.Double | double | IDataReader.GetDouble() |
REAL | System.Single | float | IDataReader.GetFloat() |
How to get double value with SqlDataReader
The reason I asked for the table definition is because there are particular column types on the DB side that map to particular data types on the C# side. The screenshot posted is not enough to realistically determine the column types. The lat and long aren't real
, otherwise they'd be truncated but other than that we can't really say
Broadly speaking:
- If you've used
real
column type, callGetFloat(..)
- If you've used
float
column type, callGetDouble(..)
- If you've used
decimal
(ornumeric
) column type, callGetDecimal(..)
GetXxx will not convert for you because it does an unboxing cast. If your reader has a decimal
in column 0 you can only call GetDecimal
on it. You cannot call GetDouble
and expect you really-a-decimal to be converted to a double, for the same reason as this doesn't work:
object o = 0.1m; //decimal boxed inside object
double d = (double)o;
Unboxing casts and converting casts are different things. If you want your decimal-in-an-object as a double you have to cast twice, once to unbox and once to convert:
object o = 0.1m; //decimalboxed inside object
double d = (double)(decimal)o;
Similarly if your thing inside reader column 0 is a decimal
and you want a double
you must get it as a decimal, because that's what it really is, and then convert it:
var dbl = (double)reader.GetDecimal(0);
Simple rule:
- Pull it out according to what it really is, then convert it
As to what you have, we don't know; your yaw
could be a real
and cannot be GetDouble
'd (has to be GetFloat
'd). Your alt
is probably a decimal (do you really need altitude to the tenth of a picometer/pico-ft?) and cannot be GetDouble
'd (has to be GetDecimal
'd) .. but you have all the necessary info to solve this one yourself.
If all else fails and the above just went completely over your head "try changing GetDouble to GetDecimal for the line that doesn't work. And if that doesn't work, try changing it to GetFloat. And if that doesn't work, supply the table definition and we'll tell you what it should be"
Update: youve indicated they're all varchar. Personally I would convert them to float or real - open SSMS, Design the table, change the column type to eg float and save the table. Then they can be GetDouble'd
Can't read float value from SQL Server database
Oh I have found the answer. Nothing wrong with the code I wrote. The problem is that I place the breakpoint on the 'double d = (double) rd.GetValue(0)' line. That is, 'd' value is not assigned yet so that on the debug screen it returns '0.0'.
Sorry for this mistake and thank you all Stack-Over-flowers for spending your time helping me!!!!
How to cast SQLiteDataReader values into float in c#
My suggestion is
First try to read the column value as a string like
Pricing pricing = new Pricing();
string string_val=convert.ToString(reader["actual_price"]);
Then use a TryParse
If your property is of type double use double.TryParse
float _val=0;
float.Tryparse(string_val,out _val);
Then assign model property like
pricing.ActualPrice =_val;
The benefit of doing string conversion & then Tryparse is it will helps you to handle null value exceptions
Might be this can be done in some other simpler ways as well
ASP.NET: What is DataReader return type if my sql data type is float?
The return type for reader["Price"]
is object
. To do what you're trying to do use this:
price = decimal.Parse(reader["Price"].ToString());
However, if there's the slightest change you can end up with a string that cannot be converted to a Decimal consider using the Decimal.TryParse Method which will allow you to determine if the conversion was successful.
EDIT
The SqlDataReader has a number of different GetXXX methods that allow you to retrieve typed data from a column in a DataReader without first having to cast to/from an intermediate type. I originally advised you to use SqlDataReader.GetDecimal
because I saw the type you wanted to retrieve is a decimal. I see now that the SQL data type of the this data is actually float so I was going to advise that you use SqlDataReader.GetFloat
. I tested this though and got an invalid cast exception. The reason for this is that the SQL float is not compatible with the .NET float data type.
So I checked table in SQL Server Data Types and Their .NET Framework Equivalents on MSDN and found that the SQL float is the equivalent to the .NET double. I tried SqlDataReader.GetDouble
and was able to retrieve data without error. Since you want a decimal though, you'll need to cast:
price = (decimal)reader.GetDouble(reader.GetOrdinal("Price"));
Related Topics
C#/.Net Analysis Tool to Find Race Conditions/Deadlocks
Recursive Linq Query: Select Item and All Children with Subchildren
Internal Server Error While Running a Simple .Net Core MVC Application on Ubuntu 16.04
Solving a Timeout Error for SQL Query
Irregular Shaped Windows Form (C#)
Insert Text into the Textbox of Another Application
Detect Browser Close on Asp.Net
Asp.Net/HTML: HTML Button's Onclick Property Inside ASP.NET (.Cs)
Razor View Page as Email Template
Hmc Sha1 Hash - C# Producing Different Hash Output Than Ruby
Ef5 Code First - Changing a Column Type with Migrations
Generate PDF Based on HTML Code (Itextsharp, Pdfsharp)
How to Dllexport a C++ Class for Use in a C# Application
How to Hook into Hardware Keyboard Events at an Application Level