Link to home
Start Free TrialLog in
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>");
-------------------------------
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

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!
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());
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
Correction to my above post: Sorry, it would be totalArray which gets populated with a string like 59.2,1208.8,0.0
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() );
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

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay, let me try this....
Thanks!
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.
Hey, it works now!
http://68.159.97.140/RichChartServer/examples/DataSources/irf_scraping.aspx

Thanks a bunch for your help and patience!
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.
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.
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.