Thursday, September 1, 2011

Passing arrays from jQuery to C# Webservice

Step 1:
Download json2.js from https://github.com/douglascrockford/JSON-js
Include in your <head>
<script type="text/javascript" src="scripts/json2.js"></script>


Step 2:
Assuming you are populating an array from a select list, create and populate an array

var Voltages = new Array();
// AddedVoltage is my select list
$("#AddedVoltage option").each(function () {
        Voltages.push($(this).val());
});

Step 3:
Convert your array to a json string using json2.js
var jsonString = JSON.stringify(Voltages)


Step 4:
You can now pass the jsonString in your ajax request and recieve it as a string in your webservice.

Step 5:
Download jayrock json from http://code.google.com/p/jayrock/downloads/detail?name=jayrock-0.9.12915.zip&can=2&q= and add reference to your .net project.
More info here (http://msdn.microsoft.com/en-us/library/bb299886.aspx)

Step 6:
You can now convert the received json string in your web service to a list using

public List<String> ConvertToList(string jsonString)
    {
        List<string> SelectedVoltages = new List<string>();
        using (JsonTextReader reader = new JsonTextReader(new StringReader(jsonString)))
        {
            while (reader.Read())
            {
                if (reader.TokenClass == JsonTokenClass.String)
                {
                    SelectedVoltages.Add(reader.Token.Text);
                }
            }
        }
        return SelectedVoltages;
    }



Wednesday, August 24, 2011

jQuery access textbox within GridView

If you want to access the textbox in the gridview example below, use
$("table[id*=ResultGridView] input[type=text][id*=date]")

<asp:GridView ID="ResultGridView" runat="server" OnRowDataBound="ResultGridView_RowDataBound">
       
<Columns>

           
<asp:TemplateField>

               
<ItemTemplate>

                   
<asp:TextBox ID="date" runat="server"></asp:TextBox>
dd/MM/yyyy                  </ItemTemplate>
           
</asp:TemplateField>

       
</Columns>

   
</asp:GridView>
 

Auto-resize textbox

Facebook like textboxes

Download this jquery plugin
http://james.padolsey.com/demos/plugins/jQuery/autoresize.jquery.js

Then apply this for your textbox

$('textarea#comment').autoResize({
    // On resize:
    onResize : function() {
        $(this).css({opacity:0.8});
    },
    // After resize:
    animateCallback : function() {
        $(this).css({opacity:1});
    },
    // Quite slow animation:
    animateDuration : 300,
    // More extra space:
    extraSpace : 40
});

Tuesday, August 9, 2011

ASP.net Submit does not work on enter press

Submit button wont get fired on press of Enter button on IE

<!-- Fix for IE bug submit on pressing "Enter") --> 
<div style="display:none">
           
<input type="text" name="hiddenText"/>
 
</div> 

SQL truncate decimal points, Get only Date


SQL get only date and not time

For Ex: 12/07/2009 and not 12/07/2009 10:09:20...

convert(varchar(10),@YOUR_DATE,101)

SQL Remove decimal points

For Ex: 203.98 and not 203.98079
select cast((123.456-(123.456%.001)) as decimal (18,2))

Monday, August 1, 2011

Disabling ipv6 in Ubuntu

Installed netbeans on my Ubuntu 11.04 and it was failing to connect and install updates. Disabling ipv6 solved the issue.

Check status of ipv6
Terminal:
cat /proc/sys/net/ipv6/conf/all/disable_ipv6
0 means it's enabled and 1 - disabled

echo "#disable ipv6" | sudo tee -a /etc/sysctl.conf
echo "net.ipv6.conf.all.disable_ipv6 = 1" | sudo tee -a /etc/sysctl.conf
echo "net.ipv6.conf.default.disable_ipv6 = 1" | sudo tee -a /etc/sysctl.conf
echo "net.ipv6.conf.lo.disable_ipv6 = 1" | sudo tee -a /etc/sysctl.conf

Done. Restart and check status of ipv6 again.

Cheers
Bhushan


Friday, July 29, 2011

Check integrity of two files

This method is used to check the integrity of two files using MD5 checksum. It accepts two string parameters. The source file location and the destination file location.

public bool PerformHashCheck(string sourcepath, string destinationpath)
        {
            bool result = false;
            FileStream sourcefile = new FileStream(sourcepath, FileMode.Open);
            FileStream destinationfile = new FileStream(destinationpath, FileMode.Open);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] sourceBytes = md5.ComputeHash(sourcefile);
            byte[] destinationBytes = md5.ComputeHash(destinationfile);
            sourcefile.Close();
            destinationfile.Close();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < sourceBytes.Length; i++)
            {
                sb.Append(sourceBytes[i].ToString("x2"));
            }
            string sourceHash = sb.ToString();
            StringBuilder db = new StringBuilder();
            for (int i = 0; i < destinationBytes.Length; i++)
            {
                db.Append(destinationBytes[i].ToString("x2"));
            }
            string desntinationHash = db.ToString();
            if (sourceHash.Equals(desntinationHash))
            {
                result = true;
            }
            return result;
        }