Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6941951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:57:07+00:00 2026-05-27T12:57:07+00:00

Prob something im overlooking but this problem has annoyed me greatly. im trying to

  • 0

Prob something im overlooking but this problem has annoyed me greatly.
im trying to get a value from a dataset and then use it to do some calculations.
in the dataset its seen as an object, so i need to cast it to an int or double.
For some reason im getting a stupid error thats getting on my nerves. heres the code.

private void SpendsAnalysis()
    {
        float tempQty = 0;
        float tempPrice = 0;
        double tempTot = 0;
        double total = 0;

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            tempQty = (float)row.Cells["Qty"].Value;
            tempPrice = (float)row.Cells["Unit"].Value;

            tempTot = tempQty * tempPrice;
            total += tempTot;
        }

        textBox7.Text = total.ToString();
    }

Thrown: “Specified cast is not valid.” (System.InvalidCastException)
when casting form a number, must be less than inifnite. This is the annoying error im getting. now i get the data from my dataset, which gets its data from a stored procedure.
I believe the “Qty” field type is currency(yeh, why is qty currency haha, not my tables!). in my datagrid view it looks like 1.000, is this due to a type conversion? how would i rectify this?
Many Thanks in Advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:57:08+00:00Added an answer on May 27, 2026 at 12:57 pm

    A type cast has to succeed, and unfortunately, casting directly from an object to a different type than the underlying object is not going to work, even if casting from a decimal (the .NET type for currency) to a float would normally work.

    If the type in the database is currency, I would try this:

    = (float)(decimal)row.Cells["Qty"].Value;
    

    or, you can use this:

    = Convert.ToSingle(row.Cells["Qty"].Value);
    

    which will take a look at the actual value and figure out the right type of conversion to perform.

    To answer your comment, the above expression involves two distinct conversions:

    • An unboxing conversion from object to a value-type, in this case to a decimal
    • An explicit conversion from decimal to float

    The first, the unboxing conversion, is documented in the C# language specification section 4.3.2 (this is the C# 4.0 specification):

    An unboxing operation to a non-nullable-value-type consists of first checking that the object instance is a boxed value of the given non-nullable-value-type, and then copying the value out of the instance.

    (my emphasis)

    I also have the annotated version of the specification, and Eric Lippert summarizes this as:

    Although it is legal to convert an unboxed int to an unboxed double, it is not legal to convert a boxed int to an unboxed double—only to an unboxed int.

    The second, the explicit conversion, is documented in the C# language specification section 6.2.1:

    6.2.1 Explicit Numeric Conversions
    The explicit numeric conversions are the conversions from a numeric-type to another numeric-type for which an implicit numeric conversion (§6.1.2) does not already exist:
    …
    From decimal to sbyte, byte, short, ushort, int, uint, long, ulong, char, float, or double.

    (again, my emphasis)

    To summarize:

    • When “casting” from an object to a value-type, you’re actually unboxing the boxed value, and you first have to unbox the actual underlying value into its correct type, before you can convert it to a different type.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this has prob been answered, but I've searched for almost an hour
Hi folks this is prob quiet simple but not sure how to do it.
how to make something like this:(Screen is not from my app) When is change
I'm trying to do something like: public static final JSONObject MYOBJ = new JSONObject().put(value,
This is prob more of a geometry question, but I have an array of
I came across this problem in javabat( http://www.javabat.com/prob/p183562 ): We want to make a
I expierencing something strange. Prob it's easy, but my css knowhow stops here. In
Small prob i'm using the following line of code customerName = window.opener.form2.custName.value; The problem
this is prob pretty easy but if someone could explain the easiest way to
I know how to copy an input field to something else. No prob. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.