Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 664
  • Last Modified:

problem with cast

p.floLatitude = dr.GetFloat(dr.GetOrdinal("Latitude"));
{"Specified cast is not valid."}
0
mathieu_cupryk
Asked:
mathieu_cupryk
  • 11
  • 3
  • 3
  • +2
9 Solutions
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 11
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now