Avatar of irfantak
irfantak

asked on 

ASP .NET String Assignment Problem

Strange ASP .NET Problem!
In the following code:
Response.Write(totalArrayTemp[0] + " - " + totalArrayTemp[1] + " - " + totalArrayTemp[2] + "<br>");//FINE!
shows the values of each variables fine. But , outside of the For Loop, the
Response.Write("TOTAL OWNER " + totalOwner.ToString());
Response.Write("TOTAL IDLE " + totalIdle);
Response.Write("TOTAL CONDOR " + totalCondor.ToString() + "<br>");

Don't show the output. What is even bizarre is that if I comment out the output in the For loop then
Response.Write("TOTAL OWNER " + totalOwner.ToString());
shows fine. And totalIdle and totalCondor never show any output outside of the For loop.

Seems to me like something wrong with using the StringBuilder() coding but how else can I achieve this? Basically, I need to have three variables (string type) to show data as:
totalOwner = "23, 44, 55, 66 77...";
totalIdle = "11, 22, 33, 44, 55....";
totalCondor = "99,88,77, 66, 55....";

Thanks

----------------------------
....
string[] totalArray = totalData.ToString().Split(new char[] { '#' });
system.Text.StringBuilder totalOwner = new System.Text.StringBuilder();
System.Text.StringBuilder totalIdle = new System.Text.StringBuilder();
System.Text.StringBuilder totalCondor = new System.Text.StringBuilder();
for (int i = 0; i < totalArray.Length; i++) {
//let's get the data for TotalOwner
//Response.Write(totalArray[i].ToString() + "<br>");
string[] totalArrayTemp = totalArray[i].ToString().Split(new char[] { ','});
Response.Write(totalArrayTemp[0] + " - " + totalArrayTemp[1] + " - " + totalArrayTemp[2] + "<br>");
totalOwner.Append(totalArrayTemp[0] + ",");//total Owner
totalIdle.Append(totalArrayTemp[1] + ",");//total Idle
totalCondor.Append(totalArrayTemp[2] + ",");//total Condor
}
Response.Write("TOTAL OWNER " + totalOwner.ToString());
Response.Write("TOTAL IDLE " + totalIdle);
Response.Write("TOTAL CONDOR " + totalCondor.ToString() + "<br>");
-------------------------------
Editors IDEsASP.NETC#

Avatar of undefined
Last Comment
irfantak
Avatar of gelbert
gelbert

After call to first Responce.Write() execution control shifts back to ASP.NET and whatever code you have after it is never executed. Use one StringBuilder or combined multiple of them object to build single output string and ten use Responce.Write()
Avatar of irfantak
irfantak

ASKER

Dont quite understand you. I do need three different Stringbuilder objects (totalOwner, totalIdle, totalCondor). Could you please write me a little psedo-code? By the way, the Response.Write within the loop is just for debugging purposes and commeting it out did not solve the problem.
Thanks!
Avatar of irfantak
irfantak

ASKER

This is just frustratingly strange. Look at this modified code. If I get the value of totalArrayTemp[1]  to totalOwner, instead of the needed totalArrayTemp[0]  then totalOwner does not get any values. And if I do debugging output within the loop then I can see that totalArrayTemp[1]  does output correct values.
Any ideas?
----------------
for (int i = 0; i < totalArray.Length; i++) {
string[] totalArrayTemp = totalArray[i].ToString().Split(new char[] { ','});
totalOwner.Append(totalArrayTemp[1] + ",");//total Owner
//totalIdle.Append(totalArrayTemp[1] + ",");//total Owner                  
}
Response.Write("TOTAL OWNER " + totalOwner.ToString() + "<br>" + totalIdle.ToString());
Avatar of irfantak
irfantak

ASKER

Okay, may be I am making it more complicated then it needs to be. But how can I parse a comma separated string like below into totalOwner, totalIdle, totalCondor. In my code above, totalArrayTemp is the one which gets populated with a string like this:
--------------
59.2 ,1202.8 ,0.0
Avatar of irfantak
irfantak

ASKER

Correction to my above post: Sorry, it would be totalArray which gets populated with a string like 59.2,1208.8,0.0
Avatar of gelbert
gelbert

Get rid of Responce.Write inside loop,
Instead of
Response.Write("TOTAL OWNER " + totalOwner.ToString());
Response.Write("TOTAL IDLE " + totalIdle);
Response.Write("TOTAL CONDOR " + totalCondor.ToString() + "<br>")
use
Response.Write( string.Format( "TOTAL OWNER {0}  TOTAL IDLE {1} TOTAL CONDOR {2} <br>",  totalOwner.ToString(),  totalIdle, totalCondor.ToString() );
Avatar of irfantak
irfantak

ASKER

Thanks. But nothing outputs with this change. Here is the new code. Please help! I have spent too much time on it already. Seems to me like something to do with Stringbuilder?

------------------
System.Text.StringBuilder totalOwner = new System.Text.StringBuilder();
System.Text.StringBuilder totalIdle = new System.Text.StringBuilder();
System.Text.StringBuilder totalCondor = new System.Text.StringBuilder();
for (int i = 0; i < totalArray.Length; i++) {
string[] totalArrayTemp = totalArray[i].ToString().Split(new char[] { ','});
totalOwner.Append(totalArrayTemp[0] + ",");//total Owner
totalIdle.Append(totalArrayTemp[1] + ",");//total Owner
totalCondor.Append(totalArrayTemp[2] + ",");//total Owner
}
                        
                        
                  
ASKER CERTIFIED SOLUTION
Avatar of gelbert
gelbert

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of irfantak
irfantak

ASKER

Okay, let me try this....
Thanks!
Avatar of irfantak
irfantak

ASKER

Yes,
http://68.159.97.140/RichChartServer/examples/DataSources/_tempstrings.aspx

the output does show up using your code above. I wonder what is causing my code to fail! Let me take a closer look...
Thanks.
Avatar of irfantak
irfantak

ASKER

Hey, it works now!
http://68.159.97.140/RichChartServer/examples/DataSources/irf_scraping.aspx

Thanks a bunch for your help and patience!
Avatar of irfantak
irfantak

ASKER

No, sorry, I spoke too soon. The output in the above page is simply what the output has been working from the Loop. Let me double check as to what is going wrong.
Avatar of irfantak
irfantak

ASKER

I see the difference between your code and my code. In my code, totalData is is defined in StringBuilder and gets populated in a loop (which you have not seen in my code). While in your code, totalData is a simple String variable.
//the following line is in a Loop
totalData.Append(LineData[1] +  "#");//Get the second array element

PS. You are not obligated to help me further because I have already Closed the Questions and given you the points. But somehow I still need to figure out a solution.
Avatar of irfantak
irfantak

ASKER

Figured out: There was "#" at the very end of my totalData and that was unneeded. Attaching that to your harcoded values for totalData broke your code too.
Thanks for your help.
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo