Friday, August 21, 2009

Code: TSQL Select unique data

SELECT   Top(5) *
FROM [Reports].[Reporting].[ReportTracking]
WHERE ReportTrackingId IN
(
SELECT MAX(ReportTrackingId)
FROM [Reports].[Reporting].[ReportTracking]
WHERE UserId = 41603
GROUP BY ReportName
)
ORDER BY ReportTrackingId DESC

The SQL above can be used as a basis when you need to select data from your database that needs to contain only unique values for a particular column.  The inner query will retrieve the newest primary keys based on your unique column and the outer query grabs the entire row for that primary key.

Thursday, August 20, 2009

Code: 'Type' was not expected. Use the XmlInclude or SoapInclude attribute

I received the following error today while developing a web service:

System.InvalidOperationException: The type <CLASSNAME> was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically. 

This error was thrown because I was attempting to return an inherited class as a web service response.  There are a couple ways around this issue.

1. (My fix) Return the base class instead of the inherited class (obviously not always an option)
2. Add XmlInclude attributes to your BASE class as such:
[XmlInclude(typeof(InheritedClassName1))]
[XmlInclude(typeof(InheritedClassName2))]

3.  Write your own serialization code.

Wednesday, August 19, 2009

Code: jQuery $ is not defined

If you're using jQuery and you encounter an error that says $ is not defined it means that you are attempting to call a jQuery method before the jQuery .js file has been referenced. Make sure that your <script src=".."> reference tags are BEFORE any jQuery methods. This can also occur if your url to the .js file is incorrect or if the file can't be accessed due to security permissions.

Tuesday, August 18, 2009

Code: Enabling 32 bit builds in IIS 7 64 bit

I ran into a new error today:

Could not load file or assembly 'ClientPortal' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Turns out it was caused by referencing a 3rd party assembly built for 32 bit, on my 64 bit machine.  The fix was easy once I found it.  Just need to open IIS Manager, right click on the application pool that is hosting the web site, and click Advanced Settings.  Then change "Enable 32-Bit Applications" to True.

 image

Monday, August 17, 2009

Code: Use CSS to hide items instead of jQuery's ready method

if you are using $(document).ready() to hide items, it can cause the items you hide in that method to be shown for a short time before the method is executed, causing a flicker on the users screen.  To avoid this use style="display: none;" to make your item hidden.  Using visiblity:none won't work, when you call .show() jQuery won't override the style.  If you need to call a JavaScript method to determine if the item should be visible or not, set the display to hidden and then call your JavaScript method.  Its better for items to be hidden and then pop in than to have them display and pop out.

Friday, August 14, 2009

Code: Use Firebug to Fix CSS Issues


Firebug came in handy today when I couldn't figure out why Firefox wasn't honoring my table padding markup, luckily the culprit was easy to track down with Firebug.  Just click the element selector button (highlighted in red in the picture), and click on your item.  All of the CSS classes that are currently being applied to it show up in the right column and you can scroll through and find the style sheet and class that is causing your issues.

Thursday, August 13, 2009

Code: SQL Dynamic Where Clause

Select ProductID, Name, Color, Size
From Production.Product
Where (Color = Coalesce(@Color, Color) or (@Color is null and Color is null))
And (
Size = Coalesce(@Size, Size) or (@Size is null and Size is null))

The SQL statement above works against the AdventureWorks database included with the SQL Server 2008 express download.  It’s not unreasonable to want your stored procedures to play well with your user interface, it makes the whole development process a little nicer.  So if you have a web page or windows form that has a handful of options a user can specify to filter data, using a dynamic where clause will prevent you from using string concatenation to build your query.  If you were to pass ‘blue’ into this query as the Color parameter and Null as the Size parameter, the query will return all Products with the color blue regardless of value (or null value) in the size column.


However the query can be simplified if your columns do not contain null values or if you don’t want data returned if it does contain a null value, just remove the or statements as seen below
Select ProductID, Name, Color, Size
From Production.Product
Where Color = Coalesce(@Color, Color)
And
Size = Coalesce(@Size, Size)

Similarly, if you want your query to match every parameter as passed in, retrieving null values only when you pass in a null for the parameter modify it like this
Select ProductID, Name, Color, Size
From Production.Product
Where (Color = @Color or (@Color is null and Color is null))
And (
Size = @Size or (@Size is null and Size is null))

Wednesday, August 12, 2009

Code: Prevent an asp:Button from doing a postback

<asp:Button ID="btnRunRecent" runat="server" Text="Generate" 
OnClientClick="RunMyMethod(); return false;" />

If you want to run a JavaScript method on a button click for a richer client experience you’ll need to add an attribute for OnClientClick to your asp:Button.  When it renders the actual html input tag to your web page it will add an onclick attribute and set your JavaScript to its value (onclick=”RunMyMethod(); return false;”)   Just place the name of your JavaScript method as the attribute value to allow the page to postback after running your script, if you don’t want the form to do a postback after finishing your script follow your method name with “return false;” as shown in the example.  If you don’t want any custom JavaScript function to run and just want to prevent the button from causing a postback just put “return false;” as the OnClientClick value.

Tuesday, August 11, 2009

Code: Use jQuery to Bind an Event Handler to a DropDownList

<script type="text/javascript">
function EnableControls() {
}
$(document).ready(function() {
$("select#<%=ddDateType.ClientID %>").change(EnableControls);
});
</script>


If you haven’t yet spent time to learn what jQuery can do for your site, then I highly recommend you do.  Its benefits may seem trivial at first glance, but once you start digging in, its flexibility is very useful.  When the page loads (client side load, not to be confused with the Page_Load server event), jQuery will tell the DropDownList "ddDateType" that it should call the javascript method "EnableControls" when the selected value changes.  This comes in handy if you need to show/hide other controls (there’s also a jQuery method for that) or do some AJAX work (there’s also a jQuery method for that).

Monday, August 10, 2009

Code: Parse Query String Outside of a Web Page

NameValueCollection queryString = HttpUtility.ParseQueryString(myQueryString);
string myId = queryString["myid"];

There are many reasons why a person would want to parse a query string outside of a web page. In order to accomplish this in C# .Net you can use the System.Web.HttpUtility.ParseQuerystring() method. One thing to note however is that you should not pass a full URL, it expects the query string only (everything starting with the "?").

Source: MSDN

Sunday, August 9, 2009

Code: Capitalize first letter of every word in a string

Microsoft.VisualBasic.Strings.StrConv(item.Name, Microsoft.VisualBasic.VbStrConv.ProperCase, 0);

The string class in C# has built in methods for making all characters in a string upper or lower case, but nothing to capitalize the first letter in every work. However there is a method in the Microsoft.Visualbasic namespace that will do the trick. Just add a reference to Microsoft.VisualBasic and use the code above.