• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

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>");
-------------------------------
0
irfantak
Asked:
irfantak
  • 11
  • 3
1 Solution
 
gelbertCommented:
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()
0
 
irfantakAuthor Commented:
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!
0
 
irfantakAuthor Commented:
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());
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
irfantakAuthor Commented:
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
0
 
irfantakAuthor Commented:
Correction to my above post: Sorry, it would be totalArray which gets populated with a string like 59.2,1208.8,0.0
0
 
gelbertCommented:
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() );
0
 
irfantakAuthor Commented:
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
}
                        
                        
                  
0
 
gelbertCommented:
I have created a simple app, there is nothing wrong with your code it seems to work fine. something else somewhere does not let your output be printed
1. Make sample ASPNET application
2. Inside Page_Load() of Default.aspx add
            string totalData = "23, 44, 55, 66 77#11, 22, 33, 44, 55#99,88,77, 66, 55";

            string [] totalArray = totalData.ToString().Split( new char [] { '#' } );
            StringBuilder totalOwner = new System.Text.StringBuilder();
            StringBuilder totalIdle = new System.Text.StringBuilder();
            StringBuilder totalCondor = new System.Text.StringBuilder();
            for ( int i = 0; i < totalArray.Length; i++ )
            {
                //let's get the data for TotalOwner
                string [] totalArrayTemp = totalArray [i].ToString().Split( new char [] { ',' } );
                Response.Write( totalArrayTemp[0] + " - " + totalArrayTemp [1] + " - " + totalArrayTemp [2] + "<br>" );
                totalOwner.Append( totalArrayTemp[0].Trim() + "," );//total Owner
                totalIdle.Append( totalArrayTemp [1].Trim() + "," );//total Idle
                totalCondor.Append( totalArrayTemp [2].Trim() + "," );//total Condor
            }
            Response.Write( "TOTAL OWNER " + totalOwner.ToString() );
            Response.Write( "TOTAL IDLE " + totalIdle.ToString() );
            Response.Write( "TOTAL CONDOR " + totalCondor.ToString() + "<br>" );
3. Run application, you should see in browser
23 - 44 - 55
11 - 22 - 33
99 - 88 - 77
TOTAL OWNER 23,11,99,TOTAL IDLE 44,22,88,TOTAL CONDOR 55,33,77

0
 
irfantakAuthor Commented:
Okay, let me try this....
Thanks!
0
 
irfantakAuthor Commented:
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.
0
 
irfantakAuthor Commented:
Hey, it works now!
http://68.159.97.140/RichChartServer/examples/DataSources/irf_scraping.aspx

Thanks a bunch for your help and patience!
0
 
irfantakAuthor Commented:
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.
0
 
irfantakAuthor Commented:
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.
0
 
irfantakAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 11
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now