Solved

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

Posted on 2004-09-28
9
217 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Image(7) 1 53
IO.FileSystemWatcher dies when used in Powershell 1 45
XML & .net 5 41
What is Thread.Sleep? 5 46
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

862 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

23 Experts available now in Live!

Get 1:1 Help Now