Solved

ASP .NET String Assignment Problem

Posted on 2007-03-23
14
291 Views
Last Modified: 2013-11-07
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
Comment
Question by:irfantak
  • 11
  • 3
14 Comments
 
LVL 8

Expert Comment

by:gelbert
ID: 18780782
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
 

Author Comment

by:irfantak
ID: 18781290
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
 

Author Comment

by:irfantak
ID: 18781422
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
 

Author Comment

by:irfantak
ID: 18781621
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
 

Author Comment

by:irfantak
ID: 18781648
Correction to my above post: Sorry, it would be totalArray which gets populated with a string like 59.2,1208.8,0.0
0
 
LVL 8

Expert Comment

by:gelbert
ID: 18781678
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
 

Author Comment

by:irfantak
ID: 18781869
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Accepted Solution

by:
gelbert earned 500 total points
ID: 18782478
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
 

Author Comment

by:irfantak
ID: 18782566
Okay, let me try this....
Thanks!
0
 

Author Comment

by:irfantak
ID: 18782658
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
 

Author Comment

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

Thanks a bunch for your help and patience!
0
 

Author Comment

by:irfantak
ID: 18782801
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
 

Author Comment

by:irfantak
ID: 18782838
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
 

Author Comment

by:irfantak
ID: 18782933
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
eclipse formatting 6 40
Please explain: Aspect Oriented Programming 2 51
Image(2) 3 22
Hidden Field Value 10 34
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now