Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

ASP.NET Forum


You are currently viewing our ASP.NET Forum as a guest. Please register to participate.
Login



Reply
C# calculations of specific fields in a datatable
Old 02-15-2012, 09:34 AM C# calculations of specific fields in a datatable
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
Hi guys.
Been a while since I posted here. Since you all have been so helpful to me in the past with issues, I figured I'd come back and throw another one at ya'.

So, I have two datatables. One is feeding another. Strange, I know.

The first datatable is being pulled from a stored procedure, here:
Code:
 DataTable dataTable = new DataTable();
            //populate datatable
            dataTable = db.StoredProcedures.FlashDataTable(strFyStartDate, strFyEndDate, strCntrStartDate, strCntrEndDate);
From there, I'm setting values of some variables needed to populate my second datatable - (then creating my second data table)
Code:
foreach (DataRow dRow in dataTable.Rows)
            {
                #region SetVariableValues

                //Sets the values to the given variables in "dataTable"
                string strProfName = dataTable.Rows[0]["Event Profile Contact"].ToString();
                string strRebId = dataTable.Rows[0]["Rebate Id"].ToString();
                string strPromoName = dataTable.Rows[0]["Promo Name"].ToString();

                DateTime strPromoStartDate = Convert.ToDateTime(dataTable.Rows[0]["Promo Start Date"]);
                DateTime strPromoEndDate = Convert.ToDateTime(dataTable.Rows[0]["Promo End Date"]);
                DateTime strPromoFinalDate = Convert.ToDateTime(dataTable.Rows[0]["Promo Final RDMT Date"]);
                DateTime strCntrSetDate = Convert.ToDateTime(dataTable.Rows[0]["Counter Set Date"]);

                int iTotalRebAmount = Convert.ToInt32(dataTable.Rows[0]["Total Rebate Amount"]);
                //Used in calculation within second foreach Loop
                int iBgtFy2012 = Convert.ToInt32(dataTable.Rows[0]["BUDGET FY'2012 or Accrued from FY'2010"]);
                int iBgtAmt = Convert.ToInt32(dataTable.Rows[0]["Budget Amount"]);

                #endregion

                //create a secondary datatable
                DataTable dt = new DataTable();
                dt.Columns.Add("EventProfileContact");
                dt.Columns.Add("RebateId");
                dt.Columns.Add("Promo Name");
                dt.Columns.Add("Start Date");
                dt.Columns.Add("End Date");
                dt.Columns.Add("Final Date");
                dt.Columns.Add("W # Of RBTs");
                dt.Columns.Add("W TTL DOLLARS");
                dt.Columns.Add("W AVG $ PER RBT");
                dt.Columns.Add("FY # Of RBTs");
                dt.Columns.Add("FY AVG $ PER RBT");
                dt.Columns.Add("FY TTL DOLLARS");
                dt.Columns.Add("Budget Amount");
                dt.Columns.Add("Variance TO Budget");
                dt.Columns.Add("Ending Budget FY2011");

                DataRow dr = dt.NewRow();
                string newContact = "";
Then, I'm creating the datarows and will be populating them with the variables. After that, I'm TRYING to make some calculations... and this is where I'm having issues.
Code:
if (newContact != strProfName)
                {
                    dr["EventProfileContact"] = strProfName;
                    dr["RebateId"] = strRebId;
                    dr["Promo Name"] = strPromoName;
                    dr["Start Date"] = Convert.ToDateTime(strPromoStartDate).ToString("d");
                    dr["End Date"] = Convert.ToDateTime(strPromoEndDate).ToString("d");
                    dr["Final Date"] = Convert.ToDateTime(strPromoFinalDate).ToString("d");

                    if (Convert.ToDateTime(strCntrStartDate) <= pastDate)
                    {
                        //wtd counts
                        dr["W # Of RBTs"] = dt.Compute("COUNT(" + strRebId + ")", "");
                        dr["W TTL DOLLARS"] = dt.Compute("COUNT(" + iTotalRebAmount + ")", "");
                        dr["W AVG $ PER RBT"] = dt.Compute("Sum(" + iTotalRebAmount + ") / COUNT(" + strRebId + ")", null);
                    }

                    if (Convert.ToDateTime(strFyStartDate) >= fiscalYearStartDate)
                    {
                        //fiscal ytd counts
                        dr["FY # Of RBTs"] = dt.Compute("COUNT(" + strRebId + ")", null);
                        dr["FY AVG $ PER RBT"] = dt.Compute("Sum(" + iTotalRebAmount + ") / COUNT(" + strRebId + ")", null);
                        dr["FY TTL DOLLARS"] = dt.Compute("Sum(" + iTotalRebAmount + ")", null);

                        //fiscal ytd budget numbers
                        if (iBgtFy2012 == null || iBgtFy2012 == iBgtAmt)
                        {
                            dr["Budget Amount"] = iBgtAmt;
                            dr["Variance TO Budget"] = dt.Compute("SUM(" + iBgtAmt + "-" + iTotalRebAmount + ")", null);
                            dr["Ending Budget FY2011"] = iBgtAmt;
                        }
                        else
                        {
                            dr["Budget Amount"] = iBgtFy2012;
                            dr["Variance TO Budget"] = dt.Compute("SUM(" + iBgtFy2012 + "-" + iTotalRebAmount + ")", null);
                            dr["Ending Budget FY2011"] = iBgtFy2012;
                        }
                    }   
                }

                dt.Rows.Add(dr);

                newContact = strProfName;

                reportView.DataSource = dt;
                reportView.DataBind();
            }
I can't get the calculations to work... what's happening is it's reaching that first calculation and it thinks that the value of the variable is the name of the column, and it can't find that column.

What I want to do is calculate the count of that particular value.
I'm kind of stuck and I need some assistance with the proper syntax of the calculations.

Thoughts?
__________________
Need a vacation.

Last edited by mb2000inc; 02-15-2012 at 09:36 AM..
mb2000inc is offline
Reply With Quote
View Public Profile Visit mb2000inc's homepage!
 
 
Register now for full access!
Old 02-15-2012, 04:33 PM Re: C# calculations of specific fields in a datatable
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
Anyone?............ Anyone?
Bueller?............ Bueller?
Pretty please?
I'll add to your talkupation, I promise. :-)
__________________
Need a vacation.

Last edited by mb2000inc; 02-15-2012 at 04:34 PM..
mb2000inc is offline
Reply With Quote
View Public Profile Visit mb2000inc's homepage!
 
Old 02-16-2012, 10:34 AM Re: C# calculations of specific fields in a datatable
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
No one?
Alrighty then.
__________________
Need a vacation.
mb2000inc is offline
Reply With Quote
View Public Profile Visit mb2000inc's homepage!
 
Old 02-17-2012, 01:03 PM Re: C# calculations of specific fields in a datatable
Giselle's Avatar
"Happy Trails"

Posts: 10,207
Name: Giselle
Location: Washington State
Trades: 0
Hello Mark,

I shall see if I can rustle up some help for you, unfortunately I am unable to help you, not my expertise.

Best Wishes,
Giselle
Giselle is offline
Reply With Quote
View Public Profile
 
Old 02-17-2012, 01:22 PM Re: C# calculations of specific fields in a datatable
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
Thank you... any help is appreciated. :-)
chrishirst usually has phenomenal advice, but I think I ticked him off the last time. :P
__________________
Need a vacation.
mb2000inc is offline
Reply With Quote
View Public Profile Visit mb2000inc's homepage!
 
Old 02-17-2012, 01:42 PM Re: C# calculations of specific fields in a datatable
Giselle's Avatar
"Happy Trails"

Posts: 10,207
Name: Giselle
Location: Washington State
Trades: 0
I sent out a red alert on your behalf!!! Talkupation wasn't necessary, but I do thank you, very much appreciated, even though I couldn't help you.

Maybe Chris will mosey on in here, if not, we do have other people who are knowledgeable as well. If the red alert doesn't work, I will have to resort to drastic measures!
Giselle is offline
Reply With Quote
View Public Profile
 
Old 02-17-2012, 02:55 PM Re: C# calculations of specific fields in a datatable
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
Thank you. This is a project for work, and I'm stumped.
You deserve the talkupation - you're going out of your way to make sure I have assistance... besides, I like your image.
__________________
Need a vacation.
mb2000inc is offline
Reply With Quote
View Public Profile Visit mb2000inc's homepage!
 
Old 02-17-2012, 10:29 PM Re: C# calculations of specific fields in a datatable
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
I'm not an ASP.NET person, but you can't do calculation direct from the query?
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 02-20-2012, 12:38 PM Re: C# calculations of specific fields in a datatable
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
Yes, I can. but that's another issue in itself that I'm currently working on. I'm having issues with duplicating values in the query. So, until I finish THAT issue, I'm doing it in the back end code of asp.net.

But thanks for responding. :-)
__________________
Need a vacation.
mb2000inc is offline
Reply With Quote
View Public Profile Visit mb2000inc's homepage!
 
Old 02-21-2012, 10:25 AM Re: C# calculations of specific fields in a datatable
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
Essentially, I thought it would be easier, because of the duplicating values in my results, to do it programatically in C#.

Which is why I posted here, for the calculations.
Because in C# I can easily say that if there's a count of a single record > 1, to only select the first given value.

This is why I need the proper syntax for the calculations.
__________________
Need a vacation.
mb2000inc is offline
Reply With Quote
View Public Profile Visit mb2000inc's homepage!
 
Old 02-21-2012, 10:51 AM Re: C# calculations of specific fields in a datatable
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Sorry I cannot help furthur
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 02-21-2012, 11:08 AM Re: C# calculations of specific fields in a datatable
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
OK, well thanks anyway.

(PS, I don't supposed you're a SQL guru, are you?)
__________________
Need a vacation.
mb2000inc is offline
Reply With Quote
View Public Profile Visit mb2000inc's homepage!
 
Old 02-21-2012, 11:51 PM Re: C# calculations of specific fields in a datatable
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
I have been working a lot and getting more experienced in SQL this past year
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 02-22-2012, 01:20 PM Re: C# calculations of specific fields in a datatable
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
That's cool. If I post my SQL issue (related to this), in another area, perhaps, you could take a look...?
__________________
Need a vacation.
mb2000inc is offline
Reply With Quote
View Public Profile Visit mb2000inc's homepage!
 
Old 02-22-2012, 10:42 PM Re: C# calculations of specific fields in a datatable
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Yeah sure
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 02-27-2012, 03:54 PM Re: C# calculations of specific fields in a datatable
mb2000inc's Avatar
Extreme Talker

Posts: 150
Name: Mark
Location: Ohio
Trades: 0
Hey, just to keep you posted...
Long story short: I resolved the issue.
I took your advice and did it in SQL - come to find out, there was bad data.

I'd like to thank you for your assistance, though. :-)
__________________
Need a vacation.
mb2000inc is offline
Reply With Quote
View Public Profile Visit mb2000inc's homepage!
 
Old 02-28-2012, 08:06 PM Re: C# calculations of specific fields in a datatable
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Very cool you could figure it out - Kudos!
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to C# calculations of specific fields in a datatable
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.42053 seconds with 12 queries