Newbie...multiplying decimals??

  • Thread starter Thread starter Bruce D
  • Start date Start date
B

Bruce D

I'm new to C# .NET and I'm trying to figure out this line of code and why I
get an error for this section of code....

**************
int ShipQty = 0;
foreach(DataRow row in table.Rows)
{
ShipQty += Int32.Parse(row["orderqty"].ToString());
}
**************

....and I get the same error in this section of code too!

**************
foreach(DataRow row in table.Rows)
{
row["extprice"] = Single.Parse(row["orderqty"].ToString()) *
Single.Parse(row["unitprice"].ToString());
}
**************

both of these..."orderqty" and "unitprice" are decimal fields in a table.

The error is an unhandled exception...blah blah...input string was not in
correct format...blah blah.

I think I need to check for the value != null or empty before I use Parse.
I'm not sure how to do that.

My question is why do I get an error or better yet...how do I multiply two
decimal values. I'd like to read up on Single.Parse if someone can send me
to a good online resource. Should I be looking into the Multiply function?
I tried using the Convert.ToInt32 instead of the Int32.Parse but that didn't
work either.

Any advice is greatly appreciated!

-bruce duncan
 
Bruce,

Why are you parsing the result? Unless orderqty and unitprice are
strings, you should just cast it to the appropriate type, like so:

float ShipQty = 0;
foreach(DataRow row in table.Rows)
{
ShipQty += (float) row["orderqty"];
}

And:

foreach(DataRow row in table.Rows)
{
row["extprice"] = (float) row["orderqty"] * (float) row["unitprice"];
}


This way, you don't have to do any conversions.
 
Nicholas,
I don't know why the parsing was in there...I'm debugging someone else's
code.

Thanks for the idea...I tried it but now I get this error:
System.InvalidCastException: Specified cast is not valid

Any ideas? Do I need to include something?

-bruce


Nicholas Paldino said:
Bruce,

Why are you parsing the result? Unless orderqty and unitprice are
strings, you should just cast it to the appropriate type, like so:

float ShipQty = 0;
foreach(DataRow row in table.Rows)
{
ShipQty += (float) row["orderqty"];
}

And:

foreach(DataRow row in table.Rows)
{
row["extprice"] = (float) row["orderqty"] * (float) row["unitprice"];
}


This way, you don't have to do any conversions.


Bruce D said:
I'm new to C# .NET and I'm trying to figure out this line of code and why
I
get an error for this section of code....

**************
int ShipQty = 0;
foreach(DataRow row in table.Rows)
{
ShipQty += Int32.Parse(row["orderqty"].ToString());
}
**************

...and I get the same error in this section of code too!

**************
foreach(DataRow row in table.Rows)
{
row["extprice"] = Single.Parse(row["orderqty"].ToString()) *
Single.Parse(row["unitprice"].ToString());
}
**************

both of these..."orderqty" and "unitprice" are decimal fields in a table.

The error is an unhandled exception...blah blah...input string was not in
correct format...blah blah.

I think I need to check for the value != null or empty before I use Parse.
I'm not sure how to do that.

My question is why do I get an error or better yet...how do I multiply two
decimal values. I'd like to read up on Single.Parse if someone can send
me
to a good online resource. Should I be looking into the Multiply
function?
I tried using the Convert.ToInt32 instead of the Int32.Parse but that
didn't
work either.

Any advice is greatly appreciated!

-bruce duncan
 
Bruce,

No, but it indicates that the types in the database are not Single
(float is the alias in C#), as you indicated.

I would check the types of the columns returned, and cast to those
types, and make sure the type that you store the result of the product and
the quantity can hold the result.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Bruce D said:
Nicholas,
I don't know why the parsing was in there...I'm debugging someone else's
code.

Thanks for the idea...I tried it but now I get this error:
System.InvalidCastException: Specified cast is not valid

Any ideas? Do I need to include something?

-bruce


in
message news:%[email protected]...
Bruce,

Why are you parsing the result? Unless orderqty and unitprice are
strings, you should just cast it to the appropriate type, like so:

float ShipQty = 0;
foreach(DataRow row in table.Rows)
{
ShipQty += (float) row["orderqty"];
}

And:

foreach(DataRow row in table.Rows)
{
row["extprice"] = (float) row["orderqty"] * (float) row["unitprice"];
}


This way, you don't have to do any conversions.


Bruce D said:
I'm new to C# .NET and I'm trying to figure out this line of code and why
I
get an error for this section of code....

**************
int ShipQty = 0;
foreach(DataRow row in table.Rows)
{
ShipQty += Int32.Parse(row["orderqty"].ToString());
}
**************

...and I get the same error in this section of code too!

**************
foreach(DataRow row in table.Rows)
{
row["extprice"] = Single.Parse(row["orderqty"].ToString()) *
Single.Parse(row["unitprice"].ToString());
}
**************

both of these..."orderqty" and "unitprice" are decimal fields in a table.

The error is an unhandled exception...blah blah...input string was not in
correct format...blah blah.

I think I need to check for the value != null or empty before I use Parse.
I'm not sure how to do that.

My question is why do I get an error or better yet...how do I multiply two
decimal values. I'd like to read up on Single.Parse if someone can
send
me
to a good online resource. Should I be looking into the Multiply
function?
I tried using the Convert.ToInt32 instead of the Int32.Parse but that
didn't
work either.

Any advice is greatly appreciated!

-bruce duncan
 
Bruce D said:
I don't know why the parsing was in there...I'm debugging someone else's
code.

Thanks for the idea...I tried it but now I get this error:
System.InvalidCastException: Specified cast is not valid

Any ideas? Do I need to include something?

Try using decimal instead of float. If that doesn't work, print out the
type of the data you're trying to convert, so you'll know what to cast
it to.
 
So should I be trying something like this???

string xx1
xx1 = row["orderqty"].GetType();
this.lblQtyTotal.Text = xx1.ToString();

Sorry...I'm trying to figure out the code as I go...do you have any
suggestions on web sites that I can go through to figure this stuff out?

-bruce
 
Bruce D said:
So should I be trying something like this???

string xx1
xx1 = row["orderqty"].GetType();
this.lblQtyTotal.Text = xx1.ToString();

Not quite, as GetType() returns a Type, not a string.
Sorry...I'm trying to figure out the code as I go...do you have any
suggestions on web sites that I can go through to figure this stuff out?

Not this kind of stuff particularly, I'm afraid.
 
Can you help me to get the type for row["orderqty"]?
How can I find out what type it is...and then display it on the page. I
look at the database schema and the field is a decimal.
Here's the code to the function:

private void loadDetail()
{
string orderdtlSQL = "select orderqty, ium, partnum, linedesc, unitprice
from pub.orderdtl where company='OGI' and ordernum = " + ordernum + " and
custnum = " + custnum;
DataTable table = d.getDataTable("vntg",orderdtlSQL);
table.Columns.Add("extprice");
float ShipQty = 0;
foreach(DataRow row in table.Rows)
{
//row["extprice"] = Single.Parse(row["orderqty"].ToString()) *
Single.Parse(row["unitprice"].ToString());
//ShipQty += Int32.Parse(row["orderqty"].ToString());
}
this.lblQtyTotal.Text = ShipQty.ToString();
this.rptOrderDetail.DataSource = table;
this.rptOrderDetail.DataBind();
}

Thanks for your help Jon!
-bruce

Jon Skeet said:
Bruce D said:
So should I be trying something like this???

string xx1
xx1 = row["orderqty"].GetType();
this.lblQtyTotal.Text = xx1.ToString();

Not quite, as GetType() returns a Type, not a string.
Sorry...I'm trying to figure out the code as I go...do you have any
suggestions on web sites that I can go through to figure this stuff out?

Not this kind of stuff particularly, I'm afraid.
 
Bruce D said:
Can you help me to get the type for row["orderqty"]?

Just use

lblQtyTotal.Text = row["orderqty"].GetType().Name;
How can I find out what type it is...and then display it on the page. I
look at the database schema and the field is a decimal.

In that case you should be able to cast it to decimal.
 
Back
Top