Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Adding numeric data from a ds? Am I making this to hard?

Posted on 2004-09-28
9
Medium Priority
?
224 Views
Last Modified: 2010-04-15

I have a ds with a couple of tables.  Each one is bound to a combo box where the user will select an option.  I need to keep the running total as they work through the form.

I have this:
Tot = System.Convert.ToString(dsBuilder.Tables["InputGear"].Rows[this.mmComboBox4.SelectedIndex]["se_price"].ToString());

Why is pulling data from the ds always in a string, that I have to convert to decimal, and then back to a string for show in a textbox?

I have to add a total of 8 selections.  Is there an easier way to do the math ?

TIA  
0
Comment
Question by:srussell705
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 10

Expert Comment

by:eternal_21
ID: 12172139
Why don't you try this?

Tot = String.Format("{0:#.##}", dsBuilder.Tables["InputGear"].Rows[this.mmComboBox4.SelectedIndex]["se_price"]);
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12173495
Pulling data from the datatable is not always a string.  It should be the type for the field.  You need to make sure that 'se_price' is a numeric value.

Bob
0
 
LVL 1

Expert Comment

by:ryeandi
ID: 12173943
hi srussell,

when you define the columns of the datatable you can set the data type for the integer column as such:

dsBuilder.Tables["InputGear"].Columns["se_price"].DataType = System.Type.GetType("System.Int16");

hope this helps,

rye
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:srussell705
ID: 12173962

I hear what your saying.  It is typed to numeric against the real data.  This gets to my inital ? then.  

How do you pull it out of the object :
dsBuilder.Tables["InputGear"].Rows[this.mmComboBox4.SelectedIndex]["se_price"]

so you can add it into a allot of other calls like the above.  This is my actual code in 1 aspect of the switch

nTot = System.Convert.ToDecimal(dsBuilder.Tables["InputGear"].Rows[this.mmComboBox4.SelectedIndex]["se_price"].ToString());

nTot += System.Convert.ToDecimal(dsBuilder.Tables["GearRatio"].Rows[this.mmComboBox3.SelectedIndex]["se_price"].ToString());

nTot += System.Convert.ToDecimal(dsBuilder.Tables["Mounts"].Rows[this.mmComboBox2.SelectedIndex]["se_price"].ToString());

nTot += System.Convert.ToDecimal(dsBuilder.Tables["Lube"].Rows[this.mmComboBox5.SelectedIndex]["se_price"].ToString());

nTot += System.Convert.ToDecimal(dsBuilder.Tables["ShifterType"].Rows[this.mmComboBox6.SelectedIndex]["se_price"].ToString());

nTot += System.Convert.ToDecimal(dsBuilder.Tables["AssemblyArrange"].Rows[this.mmComboBox7.SelectedIndex]["se_price"].ToString());

nTot += System.Convert.ToDecimal(dsBuilder.Tables["OutputType"].Rows[this.mmComboBox8.SelectedIndex]["se_price"].ToString());
                              

I then take that value for presentation to the user:
this.mmTextBox99.Text=System.Convert.ToString(nTot);
this.mmTextBox99.Visible=true;

I think that I'm missing the obvious here in dealing with numerics in a ds.  

Thanks for the reply.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 200 total points
ID: 12173979
Stop converting ToString, and then back to decimal:

nTot = dsBuilder.Tables["InputGear"].Rows[this.mmComboBox4.SelectedIndex]["se_price"];

Bob
0
 
LVL 1

Author Comment

by:srussell705
ID: 12174011
Smacking fore head!

Thanks.

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12174026
You're welcome.   Just remember not to smack too hard, you might leave a dent, and then people will notice *GRIN*

Bob
0
 
LVL 1

Expert Comment

by:ryeandi
ID: 12174413
you can refer the the data directly from the DataTable without converting:

Decimal nTot = dsBuilder.Tables["InputGear"].Rows[this.mmComboBox4.SelectedIndex]["se_price"];

just data type your column as such:

dsBuilder.Tables["InputGear"].Columns["se_price"].DataType = System.Type.GetType("System.Decimal");
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12180583
Is there an echo in here?


Closing Questions
http://www.experts-exchange.com/help.jsp#hi5
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

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

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

Join & Ask a Question