Tuesday, August 31, 2010

Size of Asp.Net Session Object

Since the past couple of days, I've been trying to determine a way to find out the size of a session object. Although this does not give an accurate picture of the size, it gives a decently rough estimate.


long totalSessionBytes;
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.MemoryStream m = new System.IO.MemoryStream();
b.Serialize(m, UserData);
totalSessionBytes = m.Length;


Of course, there are some profilers that can be used such as the following:
  • CLR Profiler
  • Eqatec
However, I think for my current purpose, the above code is good.

Friday, March 19, 2010

VS 2008 Debugger does not step through

Over the past week, I've had a horrible time with my VS 2008 debugger. Breakpoints hit. There is no problem in that. However, its when I try to step through the code that it fails. It just skips all over the place as though I've hit the F5 key and it lands up breaking at another point.

I tried everything from re-installing/repairing VS 2008; installing SP1 as well. Nothing seemed to fix the problem.

After some time chasing down wrong errors on my output window, I finally decided to call it quits and post on StackOverflow, hoping the community could help me figure out the solution.

Sure enough, one of the questions already posted brought me to the solution.

Apparently this is a known issue, the gist of which is the following:

  • Breakpoints in parallelized loops are eventually ignored after multiple hits
  • Stopping and starting threads causes breakpoints to be missed
  • Visual Studio may crash when you debug multiple processes at the same time
  • Stepping over a disabled breakpoint when you debug a native application turns into a "go"
  • Stepping when you debug a managed multithreaded application can randomly turn into a "go"
  • Running the "Step Over" command while you are debugging multiple processes causes a deadlock
The one in red above was my exact problem.

Thankfully, there is a fix for it which can be downloaded: Fix for Visual Studio 2008 debugger not working

Once using the above said fix, I came across another issue:

Loaderlock was detected

Attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.

After a bit of investigation it looks like some other running applications might cause this problem. Of what I saw the most, many people seemed to be complaining about iTunes. The other software I noticed causing this was grooveshark (flash since it is what grooveshark is based on. GrooveShark itself does not cause the problem).

ModalPopupExtender does not display at the center

Recently I had to work with the ModalPopupExtender that comes with the AjaxControlToolkit. I’ve worked with it before, but this time it posed a couple of problems (that were caused not because of the toolkit, but rather other things).

Without specifying the “X” and “Y” attributes of the ModalPopUpExtender(MPE) it is supposed to, by default, render at the center of the screen. Sometimes this doesn’t happen. The solution to getting it to render at the center of the screen is adding the following doctype to your page:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

Even then, my page didn’t seem to display the popup right. Its only later that I discovered that I was using Response.flush on the page to display a “Loading…” screen. This causes the html used for the “Loading…” screen to be the first thing on the page.

Since the DOCTYPE is now forced down the page, the rest of the page does not render as per the specified DOCTYPE. This causes the MDE to render at some other position on the screen (other than the center).

To fix this, Response.Write and Flush your DOCTYPE tag first, so it becomes the first line on the page.

Monday, March 8, 2010

Access dynamically created javascript elements in code behind

For the longest time I wondered why Asp.net would not provide a mechanism to access elements I had created dynamically using javascript in my code behind. It turns out they do, its just that since we hardly ever use that feature any more (and are more used to accessing elements by their ID’s after dragging and dropping them) it has vanished.

What I was attempting to do was create an input box using javascript, fill it with details and post it back to the server.

Code to create the input box and place it in a div:


function addtb()
{
var getdiv = document.getElementById('holddiv');
getdiv.innerHTML = getdiv.innerHTML + "";
count++;
}



On the code behind though, rather than accessing it using the id/name directly (you cannot do something like [input-box-id].Text = ’some text’) you have to get its value from the Request variable and use the data.



string c = Request.Form["tbs"].ToString();



Also if you wanted it to get displayed on the page the next time around you would have to recreate the elements manually in c#




TextBox t = new TextBox();
t.ID = f;
t.Attributes.Add("name", "tbs");
t.Text = f;
Page.Form.Controls.Add(t);


Although this is cumbersom, it works. I’m not sure if there is an easier way to accomplish this.

Thursday, March 4, 2010

Order By clause not so useful

If you have ever come across this error in SQL

The ORDER BY clause is invalid in views, inline functions, derived 
tables, subqueries,
and common table expressions, unless TOP or FOR XML is also specified.

then all SQL is trying to tell you is that you cannot use your ORDER BY clause within a subquery.

So if you have something like this, it sure as hell won’t work:


SELECT x, y, z

UNION

SELECT * FROM(
SELECT DISTINCT x, y, z
FROM (

SELECT x, y, z
FROM a
WHERE

UNION
SELECT x, y, z
FROM b
WHERE
)

ORDER BY z
)

Work arounds? A little hunting around showed that you could “use” a ORDER BY in a subquery if you used the TOP X PERCENT in your SELECT statement which would go something like this:


SELECT x, y, z

UNION

SELECT * FROM(
SELECT TOP 100 PERCENT x, y, z
FROM (

SELECT x, y, z
FROM a
WHERE

UNION
SELECT x, y, z
FROM b
WHERE
)

ORDER BY z
)

Why I stressed on “use” there is because although you can use it, it doesn’t make life any easier. Basically it allows the usage of ORDER BY, but it never does ORDER BY anything. You get back an unordered result set.

The workaround I used was adding an extra column that had blank data for the row I wanted sorted on the top and the actual data I wanted sorted by for the rest of the rows; then sorted it based on this extra column. Crude, but it works.

I thought this would be an easy thing to do, but it doesn’t seem so.

Wednesday, March 3, 2010

Web 2.0 in a flash...

This is one really well done video, I loved it...



Here is the link to the original content - Mediatedcultures

Monday, March 1, 2010

GPS is in the algorithm


I recently had the opportunity to use two different GPS systems, one by Mio and the other by Magellan. Apart from the fact that the interfaces varied greatly I was wondering how the underlying technology worked. Reading lots of reviews and stumbling upon a site catered to GPS systems (
GPS Magazine ) I finally found that Mio, Magellan, Garmin, Google, Yahoo!, Microsoft and probably every other person who makes use of maps in their products uses data provided by either Navteq or TeleNav. After all the reviews (that have been extremely well done by the site) it boils down to the fact that it is the algorithm that makes the difference in providing the routes.

How intelligence is built into the system is a whole different issue, but just providing the shortest routes from the point of departure to the point of destination itself seems like a major task. I was just wondering how primitive the traveling salesman/shortest path algorithms might be in comparison to what these guys might have built to provide the routing.

However, none of the systems seem to have enough intelligence to know how to take shortcuts. Probably its there on the higher end systems, but then I haven't tested/tried any of them. Sometime in the future I see myself sitting in a car, punching in my destination and not even having to lift another finger. I will be there. (Considering Lexus already has the autopark feature done)

Friday, February 19, 2010

Take your LJ and run with it...


Photo from (Vijeth)

I was always skeptical of shifting to another blogging platform because I thought that all of my earlier posts would be lost. I did recently shift however and thanks to the requirement of a friend, learnt that I can now safely backup my posts as well as comments and move.

We’ve both had our blogs on LJ and she recently shifted over as well. So she asked me if I knew any way to transfer the posts as well as comments. A little Googling and I found this tool called LJArchive. It allows you to take a complete backup of your data on LJ and export it to a XML file that Wordpress can then import.

Amazingly (and I’m not sure since when), Wordpress can consume quite a few formats. Plus I can also now take a backup of my existing wordpress blog, which is a feature I’ve always looked for in blogging platforms. I believe the same should hold good for any web application out on the Internet. You should be able to take your data and run with it. If not, its not “your” data per se.

Sunday, February 14, 2010

Blogging with your own domain

I always wanted to host my own blog on my domain, but I never got around to figuring out how to do so. So over the past week I decided to take a look and get it done. What gave me a head-start was Blogger’s configuration panel wherein it allows you to configure your blogspot address. By default it is “your-cool-name”.blogspot.com, which is good, but I’ve been having a domain name that has been rotting for a while and decided to make use of it.







So you click on the ‘Custom Domain’ setting and then you have an option of purchasing a domain (Google has partnered with registrars but does not register domains themselves. Although they do give a Google Apps for your domain free if you register via them).

If you already do own a domain then you can go ahead and put in the address you want so that it points to your blog. So you have told Blogger what address you want for your blog (Ex: http://mylovelyblog.mydomain.com). But how does your domain know where to point to when it sees mylovelyblog as the subdomain.

Luckily Blogger gives you directions on how to do this. Add a CName record to your domain that points to ghs.google.com. Ability to add CName’s should be part of your registrar’s administration pages.

So once your CName is setup in your domain administration panel, and blogger knows what subdomain you are going to use, anyone who goes to http://mylovelyblog.mydomain.com is automatically redirected to your blogspot blog.

In my search, I read a lot about DNS and Apache. More on that later.

Wednesday, February 10, 2010

Destiny

Photo from ( Vijeth )


Go confidently in the direction of your DREAMS!
Live the life you’ve always IMAGINED.

– Henry David Thoreau


Move is in progress (to blogger)

Friday, January 22, 2010

Scanning is for snails


Recently I was faced with the task of quickly scanning quite a few books. There was a scanner at home I decided to use. A hp scanner; which I thought will do the job just fine. The last time I had such a requirement, I borrowed a scanner from a friend and did the job. 3 days later (with intermittent breaks) I had completed scanning 101 pages of my first book.

I soon realized that using a camera to take a picture of each of the pages would get the job done just as well. Whipped out the camera, and completed 5 books in one day (actually not even a day, more like an evening). What's the difference in quality? Well, the scans are definitely better that the photos, but my main requirement was that the text be legible. The camera was more than enough for the task. Moreover the difference in quality was due to uneven light sources for the photos (which I'm sure can be alleviated once the technology is perfected).

It then got me thinking about Google's task of creating a searchable archive of books. I sure hope they didn't use a scanner to scan all the pages of all the books. A photo of the pages and then using a OCR software should get the job done faster (I think). My next thought is, why don't they make scanners based on camera technology? Wouldn't scanning time then be reduced to a fraction of what it currently is?

Maybe its a cost factor, but hopefully the next generation of scanners will perform much faster (or be based off of photographic technology)

Blue Lagoon

A less known part of Mysore, the 'Blue Lagoon' (is what I was told it was called), is basically the backwaters of the river that runs in to the KRS dam. What really interested me though is an island; it instantly reminded me of Lost. What if we had our very own island which allowed people to time travel as they went back and forth from it? Maybe there are people of the Dharma Initiative. A seemingly harmless island wrought with perils.

But it is actually just a few hundreds of meters away. Also, the island is reachable at some point in the year via a road that is exposed when the water levels reduce.

Also paid a visited to KRS (from the outside). The throngs of people were way too much to push myself to wander inside to see a musical fountain and another pressure induced fountain (which increases in height depending on the amount of water in the reservoir {is what I was told}).