Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a web page that reads a list of client using a stored procedure. This is loaded into a datareader then I'm supposed to display it inbetween some HTML <SELECT> tags on the client side.

 

I used the Page_Load event to create the connection and use a command to call the stored procedure and read the results into a datareader.

 

Here's where it gets weird. The first time I tried this, I created a public string (strList) and then looped through the datareader adding the html Option text, so that it looked something like this:

 

Do While objSQLSurveyDR.Read() = True
 strList = strList & "<OPTION Value=""" & objSQLSurveyDR("ecode") & """>" & objSQLSurveyDR("name")
Loop

 

Then in the aspx html part of the page, I had <%=strList%> between the HTML <SELECT> tags. This was taking about 30-45 seconds, for about 3000 records. I thought this was taking too long, so I tried something a but different. I added an public ArrayList item (so that I didn't have to make my datareader public and keep the connection open for too long.). Now it looked like this:

 

Do While objSQLSurveyDR.Read() = True
 objArrayList.Add("<OPTION Value=""" & objSQLSurveyDR("ecode") & """>" & objSQLSurveyDR("name"))
Loop

For i = 0 to objArrayList.count - 1
 strList = strList & objArrayList.Item(i)
Next

 

This shaved off a marginal 5 seconds. So I tried something else, and this is what I don't understand. I moved this last loop into the aspx file and put the loop directly inbetween the <SELECT> tags and used response.write, so that it looks like this:

 

For i = 0 to objArrayList.count - 1
 response.write(objArrayList.Item(i))
Next

 

This takes less than 2 seconds. But why? Should I be putting more code into the aspx file and ignoring the aspx.vb file altogether?? Whats going on here? I'd really like to understand it...

 

thanks

 

Sam

Posted

Sam,

 

I just started getting into ASP.NET myself, so I'm no expert in the subject. It's been a weird transition for me; I use to do a lot of IIS Web Apps in VB6 before moving to C#. The documentation I've read imply's (and from what I've experimented with so far it seems true) that the old fashioned building of static html with your ASP code is pretty much now obsolete with ASP.NET; in other words you should only see on extremely rare occasions the old <%= blah %> signs. Also if you use these tags, .NET starts up the old ASP .DLL file (which is going to cost you time), ASP.NET (ASPX) uses a differant DLL and it only allows the old <%= blah %> signs for backwards compatiblity and I think that is probably where you lag is coming from? Again I'm not an expert (yet), but I'm one of those that really reads up on stuff as I learn it, so take it for what you will, I'm still in the midst of learning it.

Posted
Thats what I thought too (coming from the old ASP/Vb6 com world too), and I was really disappointed to see so many ASP.NET web applications have the code in the ASPX file instead of the ASPX.VB file. It seemed MS was contradicting their own marketing strategies (again!).
Posted

Sam,

 

The code you have that runs quickly does not concatenate the strings. This takes a long time, as in most cases, the string will need to be copied from one area of memory to another. The time this takes is proportional to the length of the string. When you get to the 3000th record, the string is long and this time is not insignificant.

 

The loop therefore takes time proportional to 3000 squared (so the time taken will increase drastically if you have more than 3000 records)

 

The last example does not concatenate the strings, and takes time proportional to the number of records (not the number of records squared)

 

If as Jake suggests you use StringBuilder and set its Capacity property to something larger than the total length of the string, it will not have to make a copy of the entire string each time, and should take a time proportional to the number of records, not the number of records squared. Alternatively, you could set up an array of characters and copy the strings to the appropriate point in the array yourself, converting it back to a string when all 3000 records had been processed.

 

Richard

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...