Wednesday, June 30, 2010

Async Calls in C# – Should I blog about this?

Well, this may be for me to keep track but thought it would be usefull as I see more more and junior developer who do not understand the importance of Async programming and make blocking calls and it is frustrating especially in Web or Online apps.

There are too many frameworks available that you can take advantage of today. Another great video here I just wanted to give a very very simple steps to get this whole asynchronous business going. I assume that you know all the delegate concepts. Even if you dont know, dont worry, you really dont need to.

------------------------------------------------------------------------------------------------------

Scenario 1- Simplest of all. You want to call a function without blocking the main thread and dont care about return value dont want to know when called function finishes execution

a. Create a delegate –

 public delegate string  GetName (string myname);     –> note that you generally know what function to call and so you can decide on the delegate signature.

b. Then create object of delegate

GetName nameGetter = new GetName(GetMyRealName); <— GetMyRealName  is the actual function we want to call in Async manner.

c. Then Simply do

IAsyncResult result = nameGetter.BeginInvoke(name,null,null); <- Param 1 is parameter to original function, first null is Callback (explained latter) and last is object state (explained later)

A method GetMyRealName will be called without blocking your main thread. This is as simple as that.

*note that if your called function throws exception it will not come up

------------------------------------------------------------------------------------------------------

Scenario 2-  You want to call a function without blocking the main thread, At the same time do something else and then see what is the result of your async function

------------------------------------------------------------------------------------------------------

Scenario 3-  You want to call a function without blocking the main thread, then have another function called when your called function finishes execution

Tuesday, June 22, 2010

Showing HTML as is in Text box for ASP.NET 4.0

1. Simple thing but may get very annoying sometimes :)

2. you stored an HTML in DB. you want to show it as in Text box.

3. Note – Label and literal controls will show them as they encode by default. However Text box will not.

4. Fix – use HTML encode for the text box

ASP.NET Validate Request with .NET 4.0 – some breaking news

From old days -

1.  if you want to store HTML in DB – may be script you encode and store.

2. While post back you will get “potentially dangerous Request.Form value was detected” – To fix this you go ahead and make validateRequest=false on your page.

3. With .NET 4.0 this will not work Because this is now in the BeginRequest phase of a HTTP request, pages with validationRequest=”false”  will still get the dreaded message

Fix?

Set requestValidationMode=”2.0″ in which case the page setting will apply.

Put <httpRuntime requestValidationMode=”2.0″ /> in your web.config’s <syste.web> section

good article can be found here

Wednesday, June 16, 2010

SQL Server 2010 Express SP1 full install OR SQL Server 2010 Managements studio Express install After VS 2010

 

Invoke or BeginInvoke cannot be called on a control until the window handle has been created.

Hello all,

I had this nasty thing that came in my way when trying out SP1 for SQK 2k8 after VS 2010 install in Win 2k8 or Win7

Here are the steps

1. Start the install.

2. Let the UAC prompt come up and then say yes.

3. Install will start. Keep clicking next ok etc..the goal is to get to the Error dialog box

4. Once you get the error dialog, stop and do not click “OK”.  Instead go to c:"{GUID} and then copy the folder to some location. If you click ok the temp folder will be cleaned up

SQL Setup

5. Run the setup from this folder. For Win2k8, “Run as Administrator”

6. There are some more solutions given here but this worked for me 99% of the time