Archive for August, 2008

ObjectDataSource UpdateParameters null

August 18, 2008

Playing around with ObjectDataSource I found that ‘Update’ didn’t work. Specifically, I had an ObjectDataSource being used as a Data Source for a GridView. The ODS.Select was returning a DataSet drawn from an Access database. When I clicked on the GridView ‘Update’ link (after clicking the ‘Edit’ Command link) the data in the DataGrid would return to its pre-edited state. The Update was not being processed, but why ?

Through A Glass Darkly
Peering into the ODS.UpdateParameters I noticed that the parameters corresponding to the original DB values were null. This meant that the Update statement could not select any record to update. To check that the Update would work if correct values were passed in, I edited the DefaultValue of the UpdateParameters to a hardcoded value in Markup. With this in place the DB Update worked. So I had to discover why the Original Values were not being initialized.

This Blog post on Asp.Net Help, Using Parameters with the Object Data Source Control pointed me in the right direction by giving me a pointer to the MSDN article, “How a Data Source Control Creates Parameters for Data-bound Fields” which explains exactly how the ODS Parameters are generated.

Gold In Them Thar Incomprehensible Articles
The precise nugget of MSDN wisdom which assisted me was this:

For an update operation, the data source control populates its UpdateParameters collection with values from the name/value pairs in the Keys, NewValues, and OldValues collections. For a delete operation, the data source control populates its DeleteParameters collection with values from the name/value pairs in the Keys and OldValues collections.

The OldValues collection is not populated by default. It is populated only when the data-source control’s ConflictDetection property is set to CompareAllValues.

Too Easy (or ‘How to be a Genius With Retrospective Understanding’)
I had configured my Dataset to use Optimistic Concurrency, that’s why the ODS Update method was trying to generate parameters for the Original DB Values (original_ID and so on) BUT the KeysCollection from which these parameter values are read, ‘OldValues’, is not generated unless Markup has ConflictDetection=CompareAllValues.

asp:ObjectDataSource ID="teamDataSource" runat="server" DeleteMethod="Delete"
InsertMethod="Insert" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetData" ConflictDetection="CompareAllValues"

Now my Update goes sweeter than a hob-nailed boot through a Cane Toad’s spinal cord. Try one today.

ObjectDataSource Resources

Scott Mitchell of 4GuysFromRolla has a great set of tutorials on how to use ObjectDataSource in a multi-tier application. Tutorials 1, 2 and 4 through 6 give detailed instructions on how to create the basic tiers (Data Access, Business Logic, Presentation) using the ODS.

Breakpoints Never Hit But Symbols Appear To Be Loaded

August 11, 2008

I was mucking around practising Javascript on a toy website I had made (using Web Site Project in VS 2005) and started up the VS debugger. To my deep consternation, none of my breakpoints were getting hit even though the breakpoint was set property and there was no warning about ‘symbols not loaded’.

The reason for this turned out to be that I had inadvertently published my WebSite. A published website does not generate pdb symbols on build and hence cannot be debugged.

(NB: I only admit this publicly to save you the same embarrasment – so stop laughing and send money ūüôā )

How Can I Tell If My WebSite is Published ?
It will have a PrecompiledApp.config associated with it. You will also find the Project folder for your site your site find has a sub-folder called PrecompiledWeb which contains an ‘appname’¬† folder which itself contains PrecompliedApp.config. e.g. Let’s say your Web Project is called “DogFood”.¬†Your precompiled web site¬†will have¬†the following folder structure under ‘Visual Studio 2005’

  • Visual Studio 2005/Projects/DogFood
  • Visual Studio 2005/Projects/DogFood/PrecompiledWeb/DogFood
  • ¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬† PrecompiledApp.config

Related Posts
This post, “Why The Debugger Will Not Hit Your Breakpoints” on MSDN Blogs caused the necessary neurons to fire.

Keep Out Of The Reach Of Children
How did the web site accidently get published ? Well…err… it appears that SOME MANIAC COMPLETELY UNBEKNOWNST TO ME right-clicked on the web site project in Solution Explorer and selected ‘Publish Web Site’. Simple, quick and deadly. It should have a skull and crossbones icon on it, or be marked ‘don’t click under any circumstances’ or something…

How To Unpublish Your Web Site
I don’t think you can. But you can apparaently force a published Web Site to build with debug symbols using aspnet_compiler -d from a Visual Studio command prompt. See the post cited above for details.

typeof(Sys) == “Undefined” in ScriptResource.axd

August 7, 2008

A nasty happening popped up in DEV the other day where suddenly the pages were not rendering properly. They looked like raw HTML without the actual tags visible. Nothing was rendering correctly.

The debugger halted at an Exception thrown on the dynamic markup page here:

script src="/ScriptResource.axd?d=BjTbBXg2XH_V1XmJZ7yHuwSXPH-5-fSrlvV3UUmrU56yYz9IJKvRe5X8yQfCLMoTgHz2ltIqCKU4Xx5

&t=633324698329531250" type="text/javascript" /script
script type="text/javascript"

if (typeof(Sys) === 'undefined') throw new Error('ASP.NET Ajax client-side framework failed to load.');

The exception was being thrown because typeof(Sys) == “undefined”.

When In Doubt…
I Googled my brains out and ended up at this Microsoft ASP.NET forum thread which is full of lost souls wandering the dark shores of despair while watching a lucky few spontaneously slip into the parallel dimension of serendipitious suceess without anyone knowing why.

By working backwards from our last known good deployment, employing the 80/20 rule and examining the entrails of a slaughtered ferret the culprit turned out to be gzip compression of Script Resources in the Application Request. Please see this article in DDK OnLine for more details including why we were using gzip on the Request in the first place

As DDK explains, we stopped gzipping Script and Web Resources and our app returned rendered pages worthy of Da Vinci once more.

A BIG clue for us in the aforementioned thread was in the post by mking who turned off his or her WebResourceCompression httpModule and thus resumed sanity.

Why Does The Site Look Horrible ?

Script and Web Resources can contain CSS Style-Sheets. That’s why their failure to load makes the page look like raw HTML. In our case CSS Style-Sheets for Telerik controls were not loading.

Related Posts

Chris Riccio, Demystifying ‚ÄėSys is undefined‚Äô, explains just exactly why Sys is undefined.

Addendum 13-Aug-2008

Incorrect Visual Studio Project Template
You will get a similar error ‘Sys is undefined’ if you attempt to use AJAX controls where no HttpHandler exists for the Ajax Partial Postbacks. This can come about if you cut and paste markup code from an Ajax-enabled web site to a non-Ajax enabled web page.

For example, let’s say you want to use Microsoft’s ASP.NET AJAX UpdatePanel, so you cut and paste the markup for asp:ScriptManager and asp:UpdatePanel controls from a web page in one Visual Studio project you’ve got to another. On the second it doesn’t work and you get a JScript error ‘Sys is undefined’. What has probably happened is that the first Visual Studio project was created from the ‘ASP.NET Ajax Enabled Web Site’ project template and the other was not.

If you examine the web.config file for the first site you will find that it defines HttpHandlers for Ajax whereas the second does not viz:

add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"
add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

These configuration entries are for ASP.NET Ajax 1.0. They tell your web site where to find the Handler and definitions for the XMLHttpRequest generated by the Ajax Controls. If they don’t exist then the Sys namespace cannot be found and hence is undefined.
See ‘Demystifying Sys is Undefined’, above.

How Does AJAX Work Anyway ?
This article, Ajax Tutorial, from W3Schools gives a very simple and clear explanation of how AJAX works and even shows you how to hand-code an AJAX Handler. Recommended.