Friday, 15 October 2010

Execute multiple select statements to a DataSet in one command

I recently had the need to improve performance on a large web app. Looking in SQL profiler I could see that there were a lot of SQL commands happening that would be more efficient if I could do multiple queries in one command. Turns out it is possible and not that hard!

Here is an abstract of the data class that handles reading data from the database:

And here is how you would call it:

The only thing you have to remember is to specify the table names so you can access them from the dataset. Ideally it would be nice to read the table names from the SQL command itself but this is for later.

Tuesday, 12 October 2010

Popup Control Extenders in a grid view

If you have seen this error before then you may have been dabbling in javascript and Ajax. I wanted to hover over an icon on each row of a gridview to show more details.

For this I added an image button to the item template of the gridview and attached a Hover Menu Extender from the Ajax control toolkit. This worked great but performance became an issue when there were more records being displayed in the grid view. Eventually Internet explorer will show the above error message.

After a day of struggling I cam up with the following solution;

Hooking up a new Hover menu extender on each row looked like the source of the problem. Taking an idea from the Script Manager i decided I should have some kind of manager control I can add to the page to manage the scripts for each gridview item rather than seperately on each row.

I have an IconViewer control which contains a few icons that can be hovered, and an IconViewerManager control to store most of the JavaScript.

The part to note in the IconViewer code behind is the BindIcons method, this sets the images for my buttons but more important adds JavaScript to the onMouseOver and onMouseLeave events. The JavaScript is calling    
scripts that exists in the IconViewerManager control.

The IconViewerManager control contains the popup control extenders, this means that only 2 will exist on the page instead of one or two for every row in a gridview.

Its the JavaScript functions here that do the real work. When the icon is hovered over the Show method will be called. This sets the dynamic context key which is passed to the JavaScript method from the IconViewer control. This is to dynamically load the data from a page method added to the page that contains the gridview.

the set_parentElement method repositions the popup on the control that is also passed from the IconViewer control, this is so the popup is moved to the icon that is being hovered.

finally displays the popup on screen.

IconViewer source:

Code Behind:

IconViewerManager source:

Saturday, 9 October 2010

Ajax ComboBox Object reference not set to an instance of an object.

I was getting the following error after completing an operation in a web application and trying to click a link to enter a new Invoice on the same page.

This turned out to be due to one of the combo boxes trying to load its postback data (because I was navigating to the same page). The reason the error was being thrown is because I disabled the controls on the form and if they are disabled then the Ajax Combo box cannot load its post back data.

One work around would be to Response Redirect to another page after the invoice is posted. This way the controls would not try to load postback data again.

Removing the method to disable the controls also stopped the issue so replacing the combo box with a text box was a simple work around.

Just to re iterate its the control being disabled that has caused the problem when the control is trying to load the postback data.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   AjaxControlToolkit.ComboBox.LoadPostData(String postDataKey, NameValueCollection postCollection) +106
   AjaxControlToolkit.ComboBox.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +36
   System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +693
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1743