Double question mark operator in C#…

12 09 2010

Here is just a very quick overview of the double question mark operator (or ??) in C#. More information can be found here.

This operator simply means that if anything to the left is NOT NULL – use it, else move to the next untill you find a vaule that is not null. I.E.

int? k = null;
int? j = null;
int? s = 7;

return (k ?? j ?? s);

Simple as that!
Happy coding!

———
aQuila22

Advertisements




XML Document Transform (XDT) in VS 2010 (incl ‘.sitemap’ files)

16 08 2010

NOTE: The original blog on this can be found here.

Visual Studio 2010 has an added feature for web developers and this feature is called ‘Config Transform’. This enables web developers to modify the project’s Web.config file in an automated fashion during deployment of your project to various server environment (for more information on this go here).

Thanks to Vishal, we can now implement this XML Document Transformation technology into our own projects for any XML file. However, this will require a little bit of extra work from our side as this technology must be added for XML files other than the Web.config.
The reason for this document is not because I wanted to change normal XML files, but because of changes I wanted to make to the Web.sitemap XML file.

IMPORTANT: Keep backups of all the files you want to make changes to. Especially the “XmlDocumentTransform.targets” & “Microsoft.WebApplication.targets” files.
Here are the steps:

1. Download the XmlDocumentTransform.targets file here and save it to the following directory:

%ProgramFiles%\MSBuild\Microsoft\VisualStudio\v10.0\Web\XmlDocumentTransform.targets

For Web.sitemap changes to work, edit the above file on line 100 like the following (or just leave it if you focus on normal XML files):

FROM

Settings.xml;app.Config

TO

Web.sitemap;app.Config

XmlDocumentTransform.targets Edit..
Figure 1: XmlDocumentTransform.targets Edit..

Don’t worry about the Settings.xml you are replacing, in fact you can add any XML file to your project without further editing the “XmlDocumentTransform.targets” file and the XML Document transformation will still work for the extra added XML file(s).

2. In the ”Microsoft.WebApplication.targets” file located at: %ProgramFiles%\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications…

Add the following line of code just before the tag:

Microsoft.WebApplication.targets Edit..
Figure 2: Microsoft.WebApplication.targets Edit..

NOTE: Changing the above file (Microsoft.WebApplication.targets) will allow you to use this technology with all Web Applications Projects on your machine. If you want this only for the current project put the above node into the .csproj or .vsproj as well. You can even check this file into Source Control so that the whole team can use it.

3. Open your .csproj or .vsproj file by right-clicking on your project (in the solution explorer) and selecting Unload Project. Then right-click again on the project and select “Edit ProjectName.csproj/.vsproj”. (A shorter version of this is by just right-clinking on the project and selecting “Edit Project File” like below.)

Edit Project File..
Figure 3: Edit Project File..

Now that the your project file is open, add the following code in the first tag, again just before the ending tag:

Web.sitemap;app.config
TransformXml;

IMPORTANT: Here, in the tag, you need to add the document names which you would like to make use of the transformation technology. I.E. I can put here Web.sitemap;Settings.xml;AnotherXMLDoc.xml;app.config

But do not under any circumstance change the TransformXml; line. This is a link which ensures that the transformation on the listed documents are run.

Project File Edited (1)..
Figure 4: Project File Edited (1)..

When the changes are done close the project file, right-click the project in the solution tab and select “Reload Project”. If you did not close the project file (which is open), Visual Studio will ask you if you want to close the project file first. If you select “Yes” it will reload the project but if you select “No” nothing will happen (SO SELECT YES).

4. If not already created, create your XML files, including their child files and add them to the project. In order for them to look like figure 5 you will again have to edit the project file (.csproj or .vsproj).

Project Files Hierarchy..
Figure 5: Project Files Hierarchy..

To make the above changes locate (or add if it is not already in the file) the names of the files that you created (they will be somewhere in one of the tags inside the project file). Make sure that the ‘DepentUpon’ property is set for your files. Here is an example for the above figures outcome:

Project File Edited (2)..
Figure 6: Project File Edited (2)..

That’s it. You’re done. Build your project and publish it.
If there were no changes inside the Web.sitemap file (after publishing), make sure that there is no namespace reference other than the xmlns:xdt=”http://schemas.microsoft.com/XML-Document-Transform” in the sitemap XML file(s).

The reason for this: At the moment (August of 2010) there are a few limitations of the XML Document Transformation Engine (Vishal R. Joshi, 2010) and one of these limitations is that the engine cannot handle any namespaces. So to see this in action glance at the following figures:

Web.sitemap WRONG!
Figure 7: WRONG!!

Web.sitemap Correct!
Figure 8: Correct!

Web.Debug.sitemap Correct!
Figure 9: Correct!

———–
aQuila22





CommandArgument concatenation in VS2010

15 06 2010

Just a quick look on how i got my command argument concatenation to work in VS2010. My code in the .aspx page:

<asp:ImageButton ID=”btnBidRemove” runat=”server” AlternateText=”Verwijder” ImageUrl=”~/Images/remove.png” ToolTip=”Verwijder” onclick=”butRemove_Click” CommandArgument=’<%# DataBinder.Eval(Container.DataItem,”dbid”) + “|” + DataBinder.Eval(Container.DataItem,”adid”) %> /> 
As simple as that! 

 ———-
aQuila22
 

 





ASP.NET & a Regular Expression Validator

30 05 2010

Right so this is my second blog.  Just last week I was struggling with using an ASP.NET built in validator control to validate a text box control for values between certain amounts and that it must be a decimal number (I also had one for “required field”).

My mission was to join all three seperate validation controls into one but in the end only joined two of them which was the “between values” and “must be decimals”.  I could also join the third one but was lazy so don’t judge me.

I tried to first see if I can join, in a regular expression,  the “only decimals” and “required field” validations but found that only the “required field” validator control can return a true or false for null or !null text boxes.  So it didn’t matter even if i tried more because this block me instantly (I’m using the validation control where you can add your own regular expression for the validation criteria). 

I then tried to see if I can join the “only decimal” and “between values” validator controls with a regular expression but in the end this was a dead end because my “between values” is set dynamically through my code behind page from database values.  If one were able to build such an expression with dynamically set “between values” it would be an enormous not to mention VERY complicated expression. 

So with all this done I decided to just use the ASP.NET custom validator control and use client script to do the validation for both the “decimal only” and “between values”.  Following is a sample of my code:

<asp:TextBox ID=”txtBidAmount” CssClass=”invoer” runat=”server” CausesValidation=”true” ></asp:TextBox>
           

In my ASPX page:

<script type=”text/javascript”>
      function CheckPrime(sender, args) {
          args.IsValid = true;
         
          var bid = document.getElementById(‘<%=txtBidAmount.ClientID %>’).value;
          var validateMin = document.getElementById(‘<%=hidValidatorMin.ClientID %>’).value;
          var validateMax = document.getElementById(‘<%=hidValidatorMax.ClientID %>’).value;
         
          if (bid.match(/^\d+(,)?\d{0,2}?$/) == null)
          { args.IsValid = false; }

          if (parseInt(bid) ‘SmallerThan’ parseInt(validateMin))
{ args.IsValid = false; }

if (parseInt(bid) ‘GreaterThan’ parseInt(validateMax))
{ args.IsValid = false; }
         
          return;         
      }
  </script>
        <asp:RequiredFieldValidator ID=”BidValidatorNotNull” runat=”server”
        ControlToValidate=”txtBidAmount”
        meta:resourcekey=”BidValidatorNotNullRes”>
        </asp:RequiredFieldValidator>
       
        <asp:CustomValidator ID=”BidCustomValidator”
        ControlToValidate=”txtBidAmount”
        ClientValidationFunction=”CheckPrime”
        meta:resourcekey=”BidCustomValidatorRes”
        runat=”server”></asp:CustomValidator>

———–
aQuila22





jQuery Sliders (Ajax equivalent)

18 05 2010

Hi, 

As this being my first blog, I’ll begin with something simple and easy.  I’m currently doing my IT Bachelors internship in Belgium and well, I’m enjoying it!  But lets start with the blog. 

Recently my boss gave me a project where users must be able to search for houses/apartments in the Belgium area for buying or renting.  So we continued the idea by using something like Google maps but in the end ended up using Microsoft’s Bing maps.  

At first AJAX sliders was used but found that we couldn’t really make them do what we want (in respect to how the project must be built).  So I decided to see if there was an alternative and found this amazing jQuery slider plugin. (The original post can be found here: http://plugins.jquery.com/project/jslider-safari or http://blog.egorkhmelev.com/2010/03/jquery-slider-update/comment-page-1/#comment-935

This is just what I wanted.  The style and everything was perfect and fitted with the projects theme instantly.  As I worked with the sliders there arose some problems here and there but in the end I finally got it to work perfectly.  In this blog I would just like to show you how I achieved to dynamically set the sliders (values, scales etc…) as this was one of my major issues.  

First of all you need to include all the files (.js and styles) that can be found here.  The code, this is inside my .aspx page: 

<div class=”layout”>
<div class=”layout-slider” style=”width: 100%”>
<span style=”display: block; width: 350px; padding: 6px 0px 0px 2px;“><input id=”Pricing” type=”slider” name=”price” value=”0;500000″ /></span> </div>
</div>
<div style=”padding-top:8px”></div
 
Yes as simple as that.  I also have a seperate javascript file in which I initiate & set the slider’s values according to some criteria like the following:
  
var jQ = jQuery.noConflict();
//These vals are just to set the min and max of the sliders
var pslidStart = “get this val from database”;
var pslidEnd =
“get this val from database”;
//These vals are to set where the handles of the sliders must be
var 
sPrMin = “get this val from database”;
var sPrMax = “get this val from database”;
  
//This initiates the above slider
jQ(document).ready( function
() {
if
(destID == 1) {
jQ(
“#Pricing”).slider( {  from: pslidStart, to: pslidEnd, scale: [0, ‘|’, 100000, ‘|’, 200000, ‘|’, 300000, ‘|’, 400000, ‘|’, 500000], step: 25000, smooth: false, round: 0, dimension: “&nbsp;€”, skin: “plastic”, limits: false  } );
}
else if (destID == 2) {
jQ(
“#Pricing”).slider( {  from: pslidStart, to: pslidEnd, scale: [200, ‘|’, 400, ‘|’, 600, ‘|’, 800, ‘|’, 1000, ‘|’, 1200, ‘|’, 1400, ‘|’, 1600, ‘|’, 1800, ‘|’, 2000, ‘|’, 2200, ‘|’, 2400, ‘|’, 2600], step: 100, smooth: false, round: 0, dimension: “&nbsp;€”, skin: “plastic”, limits: false  }) ;
}
 
//This set the slider values (*Note this is a multi-handle slider)
jQ(
“#Pricing”).slider(“value”, sPrMin, sPrMax);
 
NOTE: Please refer to the original documentation for further help if needed. (Also the site this is implemented in is not up and running yet so please feel free to come back for an update later.)
 
Here is a screen shot of the slider(s):

SliderExample

SliderExample

———–
aQuila22