Solved

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

Posted on 2004-09-28
9
218 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 50 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

803 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