[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-09-28
9
Medium Priority
?
226 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
Technology Partners: 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!

 
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

Technology Partners: 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 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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

872 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