Thursday, 18 March 2010

Different ways of binding data to a drop down list

Different ways to bind data to a drop down list

* Binding to a Simple Array
* Binding to an Enum
* Binding to an ArrayList
* Binding to a hashtable
* Binding to a data table
* Binding to a data set
* Binding to a data view
* Binding to a set of objects
* Binding manually

Binding to a Simple Array

Dim months() As String = {"January", "February", "March", "April", "May", "June", "July"}
Me.DropDownList1.DataSource = months
Me.DropDownList1.DataBind()

Here both the “text” and “value” of the selected item of the “dropdownlist” would be same.

Binding to an Enum

public enum Color
{
RED,
GREEN,
BLUE
}

To bind "Color" to dropdownlist ddlColor

ddlColor.DataSource = Enum.GetNames(typeof(Color));
ddlColor.DataBind();

and to get the selected value

Color selectedColor = (Color)Enum.Parse(ddlColor.SelectedValue);

Binding to an ArrayList

ArrayList has lot of flexibilities that a simple array does not have. For example, we can remove, clear or do any manipulations within the “ArrayList”.

Dim ar As New ArrayList
ar.Add("January")
ar.Add("February")
ar.Add("March")

Me.DropDownList1.DataSource = ar
Me.DropDownList1.DataBind()

The “text” and “value” of any selected item within the “dropdownlist” would still be the same.

Binding to a hashtable

Hashtable is very similar to an “ArrayList.” but it stores information as key-value pair.

Dim months As New Hashtable
ht.Add("1001", "Jan")
ht.Add("1002", "Feb")

Me.DropDownList1.DataSource = months
Me.DropDownList1.DataTextField = "value"
Me.DropDownList1.DataValueField = "key"
Me.DropDownList1.DataBind()

Binding to a DataTable or DataSet or DataView

Me.DropDownList1.DataSource = DataTable / DataSet.Tables(index) / DataView
Me.DropDownList1.DataTextField = "ProductName"
Me.DropDownList1.DataValueField = "ProductID"
Me.DropDownList1.DataBind()

Binding to a set of objects

If you have your own structure (defined in the form of “class” or “structure”), you can still use them to create several objects and together bind to the “dropdownlist.”

At this moment, I just created a simple class (CStudent) which can hold a “regdno” and “sname”. I also defined two read/write properties to “set-get” the property values. Let us go through the class first.

Public Class Student

Private _regdno As String
Private _sname As String

Public Sub New(ByVal r As String, ByVal n As String)
RegNo = r
SName = n
End Sub
Public Property RegNo() As String
Get
Return _regdno
End Get
Set(ByVal Value As String)
_regdno = Value
End Set
End Property

Public Property SName() As String
Get
Return _sname
End Get
Set(ByVal Value As String)
_sname = Value
End Set
End Property

End Class

Once you create the above class, we need to declare objects (along with values) to work with those classes. Make sure that a “class” is simply a specification (or user-defined data type). It does not allocate any memory to hold the values (unless you create the objects). The following code creates a few objects based on the class “CStudent” and binds it to the “dropdownlist.”


Me.DropDownList1.Items.Clear()

Dim items As New ArrayList

items.Add(New Student("1001", "test1"))
items.Add(New Student("1002", "test2"))
items.Add(New Student("1003", "test3"))

Me.DropDownList1.DataSource = items
Me.DropDownList1.DataTextField = "SName"
Me.DropDownList1.DataValueField = "RegNo"
Me.DropDownList1.DataBind()


“ArrayList” can hold any type of object.

Binding Manually

We can manually add different types of items to the “dropdownlist.”

Dim li As ListItem
li = New ListItem
li.Text = "first"
li.Value = 1
Me.DropDownList1.Items.Add(li)

li = New ListItem("second")
Me.DropDownList1.Items.Add(li)

li = New ListItem("third", "3")
Me.DropDownList1.Items.Add(li)

For i As Integer = 4 To 10
Me.DropDownList1.Items.Add(New ListItem("item" & i, i))
Next

Fiddler - Web Debugging Tool

Fiddler is a Web Debugging Proxy which logs all HTTP(S) traffic between your computer and the Internet. Fiddler allows you to inspect all HTTP(S) traffic, set breakpoints, and "fiddle" with incoming or outgoing data. Fiddler includes a powerful event-based scripting subsystem, and can be extended using any .NET language.

Fiddler is freeware and can debug traffic from virtually any application, including Internet Explorer, Mozilla Firefox, Opera, and thousands more.

Dowload Fiddler from here http://www.fiddler2.com/fiddler2/

Wednesday, 17 March 2010

What does the term sticky session mean in a web-farm scenario?

Sticky session refers to the feature of many commercial load balancing solutions for web-farms to route the requests for a particular session to the same physical machine that serviced the first request for that session. This is mainly used to ensure that a in-proc session is not lost as a result of requests for a session being routed to different servers. Since requests for a user are always routed to the same machine that first served the request for that session, sticky sessions can cause uneven load distribution across servers.

Source : http://dev.fyicenter.com/Interview-Questions/JavaScript/What_does_the_term_sticky_session_mean_in_a_web_.html

What is the downside to sticky sessions with load balancers?

There are two main downsides:

1. Your load isn't evenly distributed. Sticky sessions will stick, hence the name. While initial requests will be distributed evenly, you might end up with a significant number of users spending more time than others. If all of these are initially set to a single server, that server will have much more load. Typically, this isn't really going to have a huge impact, and can be mitigated by having more servers in your cluster.

2. Proxies conglomerate users into single IP's, all of which would get sent to a single server. While that typically does no harm, again other than increasing individual server loads, proxies can also operate in a cluster. A request into your F5 from such a system would not necessarily be sent back to the same server if the request comes out of a different proxy server in their proxy cluster.

Source : http://serverfault.com/questions/46307/what-is-the-downside-to-sticky-sessions-with-load-balancers

Load balancer persistence (sticky sessions)

The Question:
If I have a load balancer in front of three servers (call them A, B, and C) and a client connects to a web service on server B using a WCF http binding that has a session (e.g., wsHttpBinding with sessions enabled), am I guaranteed that I will always communicate with server B until I close the client's proxy?

The Answer:
The answer to my question was "yes" if sticky sessions are enabled (Thanks to Pablo Cibraro MVP).

I discovered that a sticky session is one of several kinds of load balancer persistence and that you will have to check your load balancer's manual to determine what it supports. For example, here is a list of persistent types supported by the F5 load balancer:

* Cookie persistence - uses an HTTP cookie stored on a client’s computer to allow the client to reconnect to the same server previously visited at a web site.
* Destination address affinity persistence - Also known as sticky persistence, destination address affinity persistence supports TCP and UDP protocols, and directs session requests to the same server based solely on the destination IP address of a packet.
* Hash persistence - Hash persistence allows you to create a persistence hash based on an existing iRule.
* Microsoft Remote Desktop Protocol persistence - persistence tracks sessions between clients and servers running the Microsoft Remote Desktop Protocol (RDP) service.
* SIP persistence - is a type of persistence used for servers that receive Session Initiation Protocol (SIP) messages sent through UDP. SIP is a protocol that enables real-time messaging, voice, data, and video.
* Source address affinity persistence - Also known as simple persistence, source address affinity persistence supports TCP and UDP protocols, and directs session requests to the same server based solely on the source IP address of a packet.
* SSL persistence - is a type of persistence that tracks non-terminated SSL sessions, using the SSL session ID. Even when the client’s IP address changes, the LTM system still recognizes the connection as being persistent based on the session ID. Note that the term non-terminated SSL sessions refers to sessions in which the LTM system does not perform the tasks of SSL certificate authentication and encryption/re-encryption.
* Universal persistence - allows you to write an expression that defines what to persist on in a packet. The expression, written using the same TM expression syntax that you use in iRules, defines some sequence of bytes to use as a session identifier.

Other issues to keep in mind:

* Load balancer session timeout value.

Source : http://daveonsoftware.blogspot.com/2007/08/load-balancer-persistence-sticky.html

Tuesday, 16 March 2010

Select / Dropdownlist jQuery

1. Get the value of the selected item

$("#dropdownlist").val()

2. Get the text of the selected item

You would think this would be easy as well, but it isn’t. You cannot just use the text() method on the combobox. That will give you the text values of all of the options in a single string. Not what you want. The trick is to use the :selected query modifier on the option.

$("#dropdownlist option:selected").text()

3. Find out when the select value changed

$("#dropdownlist").change(function() { /* do something here */ });

4. Programmatically set the selected item.

$("#dropdownlist").val(2);

5. Modify the list.

Modifying a select element is not fundamentally different than modifying any other element in the DOM, but there are a few basics here to keep in mind. Mainly: try to avoid using the DOM directly. Create html strings instead.

Clear the list: $(“#dropdownlist”).html(“”);

Add to the list: $(“”).appendTo(“#dropdownlist”);

Using Windows Hosts File

The hosts file is a computer file used in an operating system to map hostnames to IP addresses. This method is one of several system facilities to address network nodes on a computer network. On some operating systems, the host file content is used preferentially over other methods, such as the Domain Name System (DNS), but many systems implement name service switches to provide customization. Unlike DNS, the hosts file is under the control of the local computer's administrator.

Location
“hosts” file is stored in the \Windows\ folder for 9X/Me systems and in \Windows\system32\drivers\etc\ for Windows 2000/XP.

Format of Hosts File
The “hosts” file is a plain text file named just that, hosts.Note that there is no extension on the file name.

Make particular note of the so-called “loopback” entry:
127.0.0.1 localhost
This entry is a not an actual Internet IP address but defines a local address and can be used to direct the computer to send a packet to itself. This function is used in ad blocking and is discussed below.

If a hosts file exists, it is automatically searched during any process using the Windows TCP/IP stack.

Creating a Hosts File
Generally, the entries in the hosts file have to be created one way or the other. The IP address corresponding to an URL has to be looked up and entered. While those familiar with the PING function can do this themselves, the task would rapidly become too tedious for more than a few sites. Also, many sites regard PING as a nuisance and block it. Fortunately, others have created hosts files that can be downloaded. Several references are given in the sidebar.

Speeding Up Browsing
Many of the so-called Web accelerators that are available as freeware or as part of commercial packages make use of the hosts file. The idea is that if you can resolve IP addresses on your own computer instead of waiting for a DNS server to do it, you can cut the time required to find a Web site. If you have a slow connection or if the servers are very busy, you might shave a second or two off the connection time of your most used sites. Or on the rare occasion when DNS servers are down, you might even be able to continue to use the Web.

Blocking Adware
Perhaps the biggest use of the hosts file is to block sites that are regarded as undesirable or to block ads. This done by assigning the loopback IP 127.0.0.1 to an URL that you wish blocked. Thus an entry might be: “127.0.0.1 www.unwanted.com” (without quotes). Any request for such an IP address just gets sent right back to your own computer.

Useful Run and DOS commands

Run commad to view services
services.msc

To view system information
msinfo32

To view Remote Desktop Client
mstsc

For IIS
inetmgr - To view IIS Management Console
iisreset - To restart (stop and start) IIS
iisreset /start - to start IIS
iisreset /stop - to stop IIS

To view command prompt
cmd

To start Event Viewer
eventvwr

To view control panel
control.exe / control

Keyboard Properties
control keyboard

Mouse properties
control mouse

Internet Explorer
iexplore

More available here
http://mypchell.com/guides/34-guides/69-156-useful-run-commands

DOS command for viewing system information
systeminfo

Test / send information to another network computer or network device.
ping

More available here

http://www.computerhope.com/overview.htm
http://www.easydos.com/dosindex.html

focus on an input field using jQuery

To focus on a login input box with id 'login' on page startup, try:

$("#login").focus();

To following script can be used to trigger some action on focus of each input field in the form.

$("input").focus(function () {
$(this).next("span").css('display','inline').fadeOut(1000);
});