Solved

# problem with cast

Posted on 2009-12-25
624 Views
Last Modified: 2012-05-08
p.floLatitude = dr.GetFloat(dr.GetOrdinal("Latitude"));
{"Specified cast is not valid."}
0
Question by:mathieu_cupryk
20 Comments

LVL 12

Accepted Solution

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

LVL 81

Assisted Solution

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

Author Comment

I am getting
dr.GetOrdinal("Latitude")      44      int
which is not right

the value should be 54.2333335876465
0

Author Comment

not sure what to do?
0

Author Comment

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

LVL 51

Assisted Solution

is this fine?

if (! DbNull.Value.Equals(dr["Latitude"])) p.floLatitude = dr["Latitude"];
0

LVL 51

Assisted Solution

or this

p.floLatitude = DbNull.Value.Equals(dr["Latitude"]) ? 0 : dr["Latitude"];
0

Author Comment

p.floLatitude = (float) dr.GetDouble(dr.GetOrdinal("Latitude"));
p.floLongitude = (float) dr.GetDouble(dr.GetOrdinal("Longitude"));
i did this and seems to work.
0

LVL 51

Assisted Solution

did you try 26123646 or 26123647?
0

Author Comment

p.floLatitude      54.2333336      float
54.2333335876465 value in database
what do i do hear?
0

Author Comment

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

LVL 10

Assisted Solution

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

LVL 10

Assisted Solution

Sorry I couldn't complete my previous post.

But the p.floLatitude  must be of Decimal type.

0

Author Comment

Specified cast is not valid.
Ram, u consfuse me.
This one does not work.
p.floLatitude = (float) dr.GetDecimal(dr.GetOrdinal("Latitude"));
0

LVL 12

Assisted Solution

What is the column datatype that you are trying to get the value?
0

Author Comment

@Latitude FLOAT,
@Longitude FLOAT,
0

Author Comment

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

LVL 12

Assisted Solution

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

Author Comment

good job CB
0

Author Closing Comment

both of you done well.
thank you.
0

## Join & Write a Comment Already a member? Login.

### Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on aâ€¦
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this aâ€¦
how to add IIS SMTP to handle application/Scanner relays into office 365.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the fileâ€¦

#### 731 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!