problem with cast

p.floLatitude = dr.GetFloat(dr.GetOrdinal("Latitude"));
{"Specified cast is not valid."}
mathieu_cuprykAsked:
Who is Participating?
 
CB_ThirumalaiCommented:
The GetOrdinal gives you the column position.  What are you trying to cast to? The type that is returned should be SqlFloat. You can assign the value to an object variable, and see the type of the object returned in the debugger.
0
 
leakim971PluritechnicianCommented:
Hello mathieu_cupryk,

You need to prevent null value with IsDBNull, see end of this page : http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getfloat.aspx

Regards.
0
 
mathieu_cuprykAuthor Commented:
I am getting
            dr.GetOrdinal("Latitude")      44      int
which is not right

the value should be 54.2333335876465
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
mathieu_cuprykAuthor Commented:
not sure what to do?
0
 
mathieu_cuprykAuthor Commented:
 if (!dr.IsDBNull(dr.GetOrdinal("Latitude")))
                        p.floLatitude = dr.GetFloat(dr.GetOrdinal("Latitude"));-----<
                    else
                        p.floLatitude = 0.0F;
                    if (!dr.IsDBNull(dr.GetOrdinal("Longitude")))
                        p.floLongitude = dr.GetFloat(dr.GetOrdinal("Longitude"));
                    else
                        p.floLongitude = 0.0F;

still invalid cast.
0
 
HainKurtSr. System AnalystCommented:
is this fine?

if (! DbNull.Value.Equals(dr["Latitude"])) p.floLatitude = dr["Latitude"];
0
 
HainKurtSr. System AnalystCommented:
or this

p.floLatitude = DbNull.Value.Equals(dr["Latitude"]) ? 0 : dr["Latitude"];
0
 
mathieu_cuprykAuthor Commented:
     p.floLatitude = (float) dr.GetDouble(dr.GetOrdinal("Latitude"));
                    p.floLongitude = (float) dr.GetDouble(dr.GetOrdinal("Longitude"));
              i did this and seems to work.
0
 
HainKurtSr. System AnalystCommented:
did you try 26123646 or 26123647?
0
 
mathieu_cuprykAuthor Commented:
p.floLatitude      54.2333336      float
54.2333335876465 value in database
what do i do hear?
0
 
mathieu_cuprykAuthor Commented:
maybe doing this with generics.
public static T GetValue<T>(this IDataReader dr,string columnName) where T : IConvertible

       {

           T retValue;

           object obj = dr.GetValue(dr.GetOrdinal(columnName));

           if (obj != null)

               retValue = (T)Convert.ChangeType(obj, typeof(T));

           return retValue;

       }

I am confused
0
 
RameshSCommented:
It seems that the data type returned from the data reader is equivalent to either double or decimal type. But you are trying to store in float (Single) data type.

Try the  SqlDataReader.GetDecimal() Method. It might give the acurate value. But the
0
 
RameshSCommented:
Sorry I couldn't complete my previous post.

But the p.floLatitude  must be of Decimal type.

0
 
mathieu_cuprykAuthor Commented:
Specified cast is not valid.
Ram, u consfuse me.
This one does not work.
   p.floLatitude = (float) dr.GetDecimal(dr.GetOrdinal("Latitude"));
0
 
CB_ThirumalaiCommented:
What is the column datatype that you are trying to get the value?
0
 
mathieu_cuprykAuthor Commented:
@Latitude FLOAT,
    @Longitude FLOAT,
0
 
mathieu_cuprykAuthor Commented:
 float data = (dr.IsDBNull(dr.GetOrdinal("Latitude")))  ? 0 : float.Parse(dr["Latitude"].ToString());
                    float data1 = (dr.IsDBNull(dr.GetOrdinal("Longitude"))) ? 0 : float.Parse(dr["Longitude"].ToString());

i tried Latitude      54.2333336      float
54.2333335876465 value in database
I will let this question up to u guys. I am stuck.
0
 
CB_ThirumalaiCommented:
The memory for SqlDbType.Float is 8 bytes and the C# float stores in 4 bytes.  So when you convert it to Float it truncates the value.  Use Double to get the actual value.  You may need to change the p.floLatitude to Double.
0
 
mathieu_cuprykAuthor Commented:
good job CB
0
 
mathieu_cuprykAuthor Commented:
both of you done well.
thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.