Tuesday, January 31, 2012

T-SQL: Number of working days between 2 dates

Number of working days between 2 dates in SQL

Both days are inclusive
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = '2008/10/01'
SET @EndDate = '2008/10/31'

SELECT
   (DATEDIFF(dd, @StartDate, @EndDate) + 1)
  -(DATEDIFF(wk, @StartDate, @EndDate) * 2)
  -(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END)
  -(CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END)


Friday, January 27, 2012

AJAX Control toolkit: Get/Set ActiveTabIndex in client side

Set ActiveTabIndex in Javascript

Code behind:
btnMyButton.OnClientClick = "$find('" + tcDocumentLines.ClientID + "').set_activeTabIndex(0);"

HTML:
OnClientClick="$find('<%=tcDocumentLines.ClientID %>').set_activeTabIndex(0);"

Get ActiveTabIndex in Javascript

You will first need to register to the OnClientActiveTabChanged event by using the code below. tcTabChanged is the client-side function which will be run when the tab is changed by the user
 OnClientActiveTabChanged="tcTabChanged"

Then create a function which will be called and do your custom logic in it. The sender object will reference the tabContainer and therefore we can call the get_activeTabIndex() method by using sender.get_activeTabIndex()
 function tcTabChanged(sender, args) {
        tabIndex = sender.get_activeTabIndex();
        switch (tabIndex) {
            case 1: { alert(tabIndex); } break; }
                break;
        }
    }

Tuesday, January 17, 2012

Check if control exists by ClientInstanceName

In order to check whether a specific Devexpress control exists using javascript, you can use the ASPxClientUtils.IsExists method. If the control exists, the method words as expected. The problem rises when the control you are looking for doesn't exist as this will cause the error 'ReferenceError: myControl is not defined' in javascript. In order to solve this, first use the eval function to check whether the object exists and put it inside a try catch.
var obj = null;
try { obj = eval('myClientASPxGridview'); } catch (e) { }
if (ASPxClientUtils.IsExists(obj))
    myClientASPxGridview.PerformCallback();
Link to ASPxClientUtils.IsExists documentation

Tuesday, January 3, 2012

ASPxGridview Error: LinqDataSource '' does not support the Update operation unless EnableUpdate is true

When using ASPxGridview binded to a LinqServerModeDataSource, make sure that EnableUpdate is set to false when you are handling the update in the rowupdating event. I was getting the error

LinqDataSource '' does not support the Update operation unless EnableUpdate is true

because I forgot to add the following lines in the rowupdating event, which tells the grid that the event has been handled. Omitting these 2 lines causes the code to think that the update needs to be handled by the linqdatasource, which results in the above error

void MyGrid_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
{
   //Code to update, example:
   //int pb_pk; int.TryParse(Convert.ToString(e.Keys[0]), out pb_pk);
   //int pb_w3_Budget_fk; int.TryParse(Convert.ToString(e.NewValues["pb_w3_Budget_fk"]), out pb_w3_Budget_fk);
   //SqlDataProvider.PurchasingBudgets_Update(pb_pk, pb_w3_Budget_fk);
   e.Cancel = true;
   (sender as ASPxGridView).CancelEdit();
}