May 8, 2012

SQL SERVER place unique contraint on multiple column

Problem:

The problem is that we want to create two columns of a table combined unique.
Ex: consider the table below.

EmpID DeptID1     1
1     2
2     1
3     2
4     5
5     2
1     1 
1     2 
 Now we have to maintain the integrity of  these two columns combined, as you can see that last two rows are exact duplicate of 1st and 2nd rows which must not exists where as 2nd and 3rd rows are permissible.

Solution :

If you have already created a table then first remove these duplicate rows. Then execute the following SQL query in the query editor window or SQL command prompt.


ALTER TABLE table ADD CONSTRAINT AnyConstraintName UNIQUE (EmpID,DeptID)
Or at time of creating table : 
CREATE TABLE T1 (
    EmpID int not null,
    DeptID int not null,
    /* Other Columns */
    constraint PK_T1 PRIMARY KEY (EmpID,DeptID)
)
Hope this will help...


May 7, 2012

Get the request complete event of ASP.NET Update Panel

Problem : 

This problem is related to the use of jquery with ASP.NET Update panel, I encountered it when i was trying to make a event scheduler using the update panel.

The problem was that when i load the new data in update panel, or when i fire an post-back asynchronously the event handlers of jquery attached to the previous dom inside the update panel gets lost. and i need to attach the events to newly updated dom elements.


Solution :

I begin my thought process and a ♫♪Tding♥♥♥  idea came into my mind, that when ever an ajax request is completed a request complete event is fired. Thus i need to capture this event and reinitialize my jquery code at the request completion. 


After reading a lot of documentations over Microsoft AJAX i discovered the code below.

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(  yourCallBackFunction );

And yes this line of code calls your function when ever any ajax request completes on the page. and of course this is the extension to Microsoft AJAX Framework.

Below is a complete demo of code.


  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(
     function () {  

        $('#Calender li').each(function (index) {
            var Prev = $(this);
            $(this).find('p').click(function () {
                Prev.find('p').css('background-color', '');
                $(this).css('background-color', '#111');
            });
        })

    });

simply you can test as: (or i must say cup cake steps)

1. put a scriptmanager on the page
2. place an update panel on your page
3. put some buttons inside update panel
4. copy the following code and paste somewhere inside your page

<script type="text/javascript">


  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(
     function () { 
                        alert('Request completed');
                  });

</script>

Thanks for reading hope this will help you some where..