Friday, August 09, 2013

WHERE clause with multiple AND operators for the same column-[changed]

This relates to the earlier post. The developer wanted to eliminate those records if there are more than one MailGroup_FKey which satisfy the condition. So the SQL changed,

SELECT * 
FROM MailAddress_MailGroup
WHERE  MailGroup_FKey IN (
SELECT MailGroup_FKey FROM MailAddress_MailGroup
WHERE MailAddress_FKey IN (6,7) AND 
(SELECT COUNT(*) FROM (SELECT MailGroup_FKey  FROM MailAddress_MailGroup
WHERE MailAddress_FKey IN (6,7)
GROUP BY MailGroup_FKey
HAVING COUNT(*) = 2))=1
GROUP BY MailGroup_FKey
HAVING COUNT(*) = 2 AND COUNT(DISTINCT MailGroup_FKey)=1
) AND MailAddress_FKey IN (6,7)

Wednesday, August 07, 2013

WHERE clause with multiple AND operators for the same column


Here the developer wanted to pass n number of values for the MailAddress_Key column and return all the records which has all the values for the same MailGroup_FKey.

If values 6,7,1 passed, it should return all the records which satisfy that criteria if it's satisfied by individual MailGroup_Key(s). In the example MailGroup_FKey 2 and 3 both satisfy the criteria and it should return the relevant records of MailGroup_FKey 2 and 3

Here's the SQL

SELECT *
FROM MailAddress_MailGroup
WHERE  MailGroup_FKey IN (
SELECT MailGroup_FKey FROM MailAddress_MailGroup
WHERE MailAddress_FKey IN (6,7,1)
GROUP BY MailGroup_FKey
HAVING COUNT(*) = 3
) AND MailAddress_FKey IN (6,7,1)

Wednesday, December 19, 2012

Android: ListView custom layout with ImageButton of android:focusable="false"

Here I have customized the layout of a ListView. A ListItem is constructed by a TextView and a ImageButton. By default the ImageButton is invisible. I have OnItemLongClickListener implemented in the activity and make the button visible on onItemLongClick. Under the normal scenario everything works fine until you set the visibility to visible of the ImageButton. When you set the visibility to visible you won't be able to click on that particular ListItem anymore. 


This particular behavior is caused by the ImageButton. Reason for this is one particular property of the ImageButton which is android:focusable. By default this is true and you have to set this to false. But the tricky part is it doesn't work when you set the property in the xml layout as android:focusable="false". The Reason behind this is android:focusable gets overwritten and set to true by the ImageButton's constructor. So if you want to set android:focusable="false", do it in the java file instead of doing it in the xml layout file and you have to set android:focusable="false" in these kinds of situations otherwise you'll face the above mentioned (mis)behavior.

Friday, July 06, 2012

AJAX

AJAX - Asynchronous Javascript + XML

AJAX is not a one new technology or a language. It's just a way of using a set existing standards and technologies. It's all about updating a parts of the web pages by exchanging data with the server without reloadng(postback) the whole page. Rather than reloading the whole page parts of the web page does callbacks to the server. There are no any prerequisites (additional tools or software) to use AJAX in one of your web pages you just have to know how to write some code in javascript.

XMLHttpRequest(XHR) - API in web scripting languages such as JavaScript to send direct HTTP/HTTPS requests to the server and return the response back to the script.

The example below shows how to update a part of a web page without reloding the whle page(postback). The AJAX script or the code to implement the scenario 

is written in Javascript. 


Creating a XMLHttpRequest Object

xmlhttp = new XMLHttpRequest();

This is the basic syntax for creating a XMLHttpRequest object. This syntax might differ for older versions of some of the browsers.

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); - for IE 5 and 6


Request

Request can be sent to the server by using open and send methods of the XMlHttpRequest object. 

xmlhttp.open("GET", "movie_catalog.xml", true);
xmlhttp.send();

There are 3 parameters passed to the open method.

method - Request type. This can be either GET or POST
url - Location of the file which is in the server where the response is returned from
async - asynchronous or not (true - asynchronous)


Response

There are 2 properties which can be used to get the response from the server.

responseText - when the response is in string format.
responseXML - when the response is in XML format.


onreadystatechange

A function can be stored in this property and it is called everytime when the readyState property of the XMLHttpRequest object changes.


readyState

This holds the status of the request object which ranges from 0 to 4
0: request is not initialized 
1: connection established to the server
2: request received 
3: request processing
4: response is ready


status

This holds standard HTTP status codes returned by the request
E.g:-
200: OK
403: Forbidden
404: Not Found


Example 1 : Get the content from a text file which is located in the server and display in a part of the web page.






































Example 2 : Get the content from a XML file which is located in the server and disply in a part of the web page




 


Example 3 : Get the response another web page which is located in the server.










Thursday, July 05, 2012

ScriptManager.RegisterDataItem Method (Control, String)

How can you update a control which resides outside the update panel in an asynchronous post back?


You can use ScriptManager.RegisterDataItem to send data from the server to client whether the response control is inside the update panel or not. RegisterDataItem method can be called only during an asynchronous postbacks. The data item which is registered with the RegisterDataItem method can be accessed through the client script in  pageLoading, pageLoaded, and endRequest events of the PageRequestManager object. When you handle the event, custom data is passed in an event argument object. If you have a handler for pageLoaded event, customer data is passed in the PageLoadedEventArgs class which exposed dataitems property.





Wednesday, July 04, 2012

Named and Optional Parameters

Named and optional parameters have been there for Visual Basic for sometime but haven't been available in C# until the version 4 release. Name parameters allow you to pass parameters by the parameter names disregarding the order of parameters in the method signature. Optional parameters allow you to provide default values for some of the parameters in your method.


Named Parameters
















Optional Parameters

Optional parameters allow you to provide default values for some of the parameters in your method and acts as a type of overloading. If you want to add one or more new parameter to one of your existing methods and you still need the existing functionality to behave as it is and the new parameters doesn't effect the existing functionality, the solutions is to repeat the same method with the additional parameters and a different method body. This is called overloading. 

If you use optional parameters for this purpose you can add the extra parameters as default parameters and handle those in the existing method body. You don't have to add a new method or modify the code where the existing method was called.



Monday, July 02, 2012

.Net: Instantiating



When instantiating classes, the class which is on the left side of the equation is called the declarative class and the class which is on the right side is called the instantiating class.

































Base class called "Animal" contains 2 methods as

public void Eat()
public void Sleep()



Sub class called "Lion" which derived from "Animal" contains 2 new methods as
public void Hunt()
public void Walk()
















When instantiating with base class as the declarative class and the derived class as the instantiating class, what will be structure of the instance? You'll see it carried 2 public methods of the base class. Actually this instance will carry all the methods, properties and variables from base class (according to encapsulation and other object oriented principles). This will not relevant if you override or shadow the base class methods.