Solved

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

Posted on 2004-09-28
9
216 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:srussell705
Comment Utility

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 96

Accepted Solution

by:
Bob Learned earned 50 total points
Comment Utility
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
Comment Utility
Smacking fore head!

Thanks.

0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
Comment Utility
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
Comment Utility
Is there an echo in here?


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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now