Why WebMethods cannot be static methods

by rbellantoni 3/31/2010 7:15:00 AM

I have not found a very clear explanation as to why static methods cannot be used in conjuncture with web service calls and page methods.

Well the answer is pretty simple. In order to work with javascript ASP.NET creates a proxy class from the class the web service points to. So when you make a call from javascript to the web service your calling the generated proxy class. However, static methods are inherently separate from the class and can be called without making an object reference to the class. Therefore it cannot be generated with the javascript proxy class and as such cannot be referenced.



XP Restart over Remote Desktop

by ebarcza 3/30/2010 3:57:00 AM
I just noticed that I could not restart a computer over Remote Desktop. Here's a working

  1. Start > Run then type in cmd
  2. in the command window type in "shutdown -r", this will restart your computer

      to see the full options of the shutdown command type in "shutdown /?". this will give you a full list of options

Tags: ,


How to create custom System.Drawing.Color

by rbellantoni 3/26/2010 12:00:00 PM

You can create your own custom system.drawing.color from HTML color codes like this:


System.Drawing.Color customGreen = System.Drawing.ColorTranslator.FromHtml("#CCFFCC");


Custom RequiredFieldValidator

by rbellantoni 3/26/2010 8:36:00 AM

There are a few ways you could go about extending the ASP.NET validation controls, but i've come up with a quick way to use a customvalidator and get it to work perfectly as a requiredfieldvalidator.

The goal was I wanted to set the background-color of the element to red and display a required message when the user did not enter a value. The requirefieldvalidator does not allow you to do this normally (since you can't add a custom JS function to the extender.) You could do validation on the element itself, but I wanted something a little more reusable.

So here we create our custom validator:

<asp:TextBox runat="server" ID="txtName"></asp:TextBox>

<asp:CustomValidator ControlToValidate="txtName" runat="server" 

                                            ID="cvReqName" ClientValidationFunction="RequiredValidation" 

                                            ErrorMessage="Required" ValidateEmptyText="True"></asp:CustomValidator>

We make sure ValidateEmptyText is set to true and we call a generic RequiredValidation javascript function we created below:

// **********************************************************************************//

// This function is used for a asp.net custom validator control. Call this function  //

// and it will determine what type of control is being used check the value and if   //

// the value is blank then it will set the background color of the element to red    //

// **********************************************************************************//

function RequiredValidation(sender, args) {


    var ctrl = document.getElementById(sender.controltovalidate);

    var ctrlValue;


    // Determine the ctrl 

    switch (ctrl.tagName.toLowerCase()) {

        case "input":

            ctrlValue = ctrl.value;


        case "span":

            ctrlValue = ctrl.innerHTML;


        case "select":

            ctrlValue = GetDDLSelectedValue(ctrl);



    if (ctrlValue == "") {

        ctrl.style.backgroundColor = "#E77471";

        args.IsValid = false;


    else {

        ctrl.style.backgroundColor = "#FFFFFF";

        args.IsValid = true;




This javascript function checks to see what type of control it is and if the value is "" if it is, it sets the isvalid flag on the validator and changes the background-color of the element to a red color. Simple as pie.




DataTable.Delete() vs DataTabel.Remove()

by rbellantoni 3/26/2010 7:13:00 AM

The biggest difference between the delete function and the remove function on a datatable, is that the delete does not actually delete the row. It simply marks it for deletion using rowstate. When the datatable is displayed, the "marked" rows are not shown, however the record is still in the table and the rows.count is still the same.

When you remove a row, the entire row is gone forever and the count is changed.

Thats the difference :)


Using a DataTable with JSON

by rbellantoni 3/26/2010 5:09:00 AM

As some of you have probably run into. You cannot simply return a DataTable object from a web service and have it automatically convert/serialize into a JSON object, without first performing some kind of XML to JSON serialization. However there is a work around that I use for this! JSON does serialize HashTables. So I wrote a function to create a generic list of hashtables from a datatable:

private List<Hashtable> ConvertDataTableToHashTable(DataTable dtIn)


        List<Hashtable> hashList = new List<Hashtable>();

        foreach (DataRow drIn in dtIn.Rows)


            Hashtable ht = new Hashtable();

            foreach (DataColumn dc in dtIn.Columns)


                ht.Add(dc.ColumnName, drIn[dc.Ordinal].ToString());




        return hashList;



This creates a hashtable for every row and uses the column name as the hash name and the value of that column as the hash value. It then adds the hashtable to the generic list and when you pass back the generic list of hashtables from the web service call like so:


    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

    public List<Hashtable> GetCounties(int stateID)


        DataTable dtCounties = GetCountyList(stateID);

        List<Hashtable> htCounties = ConvertDataTableToHashTable(dtCounties);

        return htCounties;



You get a perfect JSON object with each column name as a property and it can be referenced like obj[0].CountyName, obj[1].CountyName etc.


SQL Server decimal Scale and Precision

by ebarcza 3/25/2010 4:23:00 AM
I always forget this... so I'm bloggin it!

Precision is the number of digits in a number. Scale is the number of digits to the right of the decimal point in a number. For example, the number 555.44 has a precision of 5 and a scale of 2.

Tags: ,


Additional Parameters in an Ajax Web Service Call

by rbellantoni 3/23/2010 9:56:00 AM

I have been wondering for sometime how to pass additional objects or values over to the OnSuccess or OnFailure functions of an ajax service call. I looked through a microsoft Ajax book and with their online documentation and found that it is not shown in either place. So here is how to do it!

Here is the success and failure signatures:

function succeededCallback(result, userContext, methodName){

function failedCallback(exception, userContext, methodName){

Now, userContext is what we are interested in. You can pass whatever you want over as userContext and you can access it in either onSuccess or onFail functions.

You make the call like this:

YourWebService.YourWebMethod(parameters, succeededCallback, failedCallback, userContext);

Then you can do whatever you want with the additional parameters!


Powered by BlogEngine.NET
Theme by Mads Kristensen

About the author

Name of author Author name
Something about me and what I do.

E-mail me Send mail


<<  October 2022  >>

View posts in large calendar

Recent comments



The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2022

Sign in