Tuesday, April 21, 2009

The administrative limit for this request was Exceeded - LDAP

Last week I was writing an ASP page that fetched information from Active Directory using LDAP queries.

It was a straight forward page - the manager from the client side wanted to check some attributes. I wrote the asp code and began to test it. That’s when this error popped up, after processing the page halfway through:

“The administrative limit for this request was
Exceeded”

The error was self-explanatory and also the error popped up exactly after fetching 499 records. I was quite certain of the issue, but I just wanted to know if I had to access 500 records at a time by modifying the query or could the ADODB Command object (which I was using) do the trick.

Googled it quickly and got the answer – setting the page size on the Command object was enough:

comm.Properties("Page Size") = 100

The Code snippet for reference purpose:

Dim rs
Dim conn, strSQL
Dim dictDeptList, szUserID, szUserName, intDeptID
Dim comm

szCountry = Request.Form("CountrySelCtl")

strSQL = "select cn, " & CONST_TEL_KEY & _
" from '" & GetMemberContainerPath & "/ou=" & szCountry & "' where objectClass='Member' "

Set conn = Server.CreateObject("ADODB.Connection")conn.Provider = "ADsDSOObject"conn.Open "ADs Provider", Application("AD_UserName"), Application("AD_Password")

Set comm = Server.CreateObject("ADODB.Command")
Set comm.ActiveConnection = conncomm.CommandText = strSql
' Removes the Max Allowed Restriction
comm.Properties("Page Size") = 100

Set rs = comm.Execute()

Monday, April 20, 2009

Sorting in Classic ASP

For the past 2 weeks I have been supporting and maintaining a client website, which is huge, but mostly in ASP.

There was one emergency client work order – requesting an ASP page that should provide options to download some error log files located in a virtual directory.
The request was very clear in mentioning that the error log files should be shown in a dropdown, and the files should be sorted chronologically, the recently modified to be on top.

Getting the list of files is easy, using the FileSystemObject. Also getting the modified date is simple, we have the DateLastModified property of the file object. The only additional thing pending was to sort them by this date.

This has to be developed in classic ASP. .Net was not an option – How I miss coding in .Net! This whole thing could have been done in just a couple minutes. Still building this in ASP page only took a few minutes extra. Wanted to share the Sort method for reference to others (and myself too).

Fetch the list of files into an array:

Dim fso
Dim errorDir
Dim file
Dim filesArr()
Dim sortedFiles
Dim cnt
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set errorDir = fso.GetFolder(Server.MapPath(ErrorLog_Dir))
cnt = 0
For Each file in errorDir.Files
cnt = cnt + 1
Next
Redim filesArr(cnt,1)
cnt = 0
For Each file in errorDir.Files
filesArr(cnt,0) = file.Name
filesArr(cnt,1) = file.DateLastModified
cnt = cnt + 1
Next

Call the Sort function

SortByDate filesArr, 1

Definition of the SortByDate function:


Function SortByDate(arrToSort, intAsc)
Dim temp1
Dim temp2
Dim i, j
For i = 0 To UBound(arrToSort) - 1
For j = i To UBound(arrToSort)-1
'Sort Ascending
If intAsc = 1 Then
If datediff("n",arrToSort(i,1) , arrToSort(j,1)) >= 0 Then
temp1 = arrToSort(i,0)
temp2 = arrToSort(i,1)
arrToSort(i,0) = arrToSort(j,0)
arrToSort(i,1) = arrToSort(j,1)
arrToSort(j,0) = temp1
arrToSort(j,1) = temp2
End If
'Sort Descending
Else
If datediff("n",arrToSort(i,1) , arrToSort(j,1)) <= 0 Then
temp1 = arrToSort(i,0)
temp2 = arrToSort(i,1)
arrToSort(i,0) = arrToSort(j,0)
arrToSort(i,1) = arrToSort(j,1)
arrToSort(j,0) = temp1
arrToSort(j,1) = temp2
End If
End If
Next
Next
fnSort = arrToSort
End Function

Tuesday, February 10, 2009

.Net Regular Expression for Numbers Range

At work, a colleague asked me for help with with a regular expression he was trying to get working. It was simple and quick and worth a post in my blog.

He wanted to validate for numbers between 1 and 50. After a couple of minutes of testing at http://www.regextester.com/, I sent him this:

Dim re As New Regex("^([0-9]|[0-4][0-9]|[5][0])$")

MsgBox(re.IsMatch(“123”))
MsgBox(re.IsMatch(“2”))
MsgBox(re.IsMatch(“50”))
MsgBox(re.IsMatch(“51”))

Tuesday, January 27, 2009

Accessing images from Google Sites

I recently wanted to modify my blog template a bit and had to create my own images and use them. I decided to upload the images to Google sites, where I have kept a bunch of my other files and preferred to maintain them all in one single location.

I uploaded the .gif images which had transparent areas in them and I copied the link and added to my harness blog and tested. I noticed that it displayed fine in Chrome, Firefox, Opera and Safari, but not really so in IE 6.

The transparent areas of the file were filled with a light blue color which was sufficient enough to mangle the appearance of the page.

The problem was with the URL I had used. The actual URL from the Google Sites was:
http://sites.google.com/site/codingpassion/Home/rails_main.gif?attredirects=0

For some reason, I found that query at the end of the URL to be unnecessary and removed it and hence faced this error.

This is what you see when accessing this link in IE6 (notice the blue/gray area? That is supposed to be transparent)
http://sites.google.com/site/codingpassion/Home/rails_main.gif


This is what you see when accessing this link in IE6 (notice the ?attredirects=0 query string)
http://sites.google.com/site/codingpassion/Home/rails_main.gif?attredirects=0


Fiddling with this further showed that when the attredirects=0 query string is passed along, Google appends the URL with an “auth” key value which in turn redirects to a more messy URL. Not sure what happens internally, probably they extract it from some location and authenticate if the file is available to all users, but adding that query string value back to the URL fixed things.

Update 1
Apparently there has been some issues with accessing images from Google sites:
http://groups.google.com/group/sites-help-somethingisbroken/browse_thread/thread/ec167ad76b4ea267/170c3709074f2bf6?lnk=gst&q=

Not sure what exactly Google does to fetch the image and why it would behave differently without the query string input.

Modifying Blogger Template – Resizing main Blog Area

The Google blogger has a decent bunch of templates to choose from and when I had to choose one I took a couple of minutes to think.

I would be posting code snippets and occasionally diagrams or images, maybe screen captures to explain something. But primarily the content will mostly be text with a lot of code.

I did not want a dark background with white fonts – I think no one will ever want to use that for writing technical content. Somehow I did not like the fully white templates as well. Most of the templates offered looked too simple and without many colors.

I finally chose the Rounders-3 (Green version) because it was simple and slick and the colors were fine and looked professional. However there was one drawback with it which I noticed as I started to post. The space reserved for the main blog was too little and any post that I did looked obtrusively lengthy. The code snippets that I posted had to be broken down to multiple lines despite using a smaller font. Making the fonts any smaller would be make it impossible to read.

So the only options I had were to either expand the main blog area or choose another template. But then, since I liked the template and also because did not want to perform a huge face-lift to a blog with steady traffic, I chose option A.

The change, I presumed, should not be difficult to make and it was not that difficult at all.

The default template was done to fit 800x600 resolutions and hence the max size of the title and footer was at 740 px. I am not sure if there are any more people using such low resolutions, but anyways I had my Google analytics stats to answer my question. There was less than 1% of the total visitors in that screen resolution.

Convinced that it would not affect the majority of the users, and even for those affected, they only have to scroll to see the profiles section, I proceeded to make the change.
Just wanted to jot down the exact changes made, as a ready reckoning for myself and others:
1. CSS properties had to be changed for the main blog area and the Title
2. Noticed that the rounded corners were not images for just the corners, but were a strip, hence had to modify the images and adjust it to the new width

That was all that needed to be done.

I used Google sites to upload my modified images. There was a snag in accessing the images from Google sites. Read more about it here.

Actual CSS Changes

#outer-wrapper
width:940px;
change image – used
http://sites.google.com/site/codingpassion/Home/corners_main_bot.gif?attredirects=0

#main-wrap2
change image – used
http://sites.google.com/site/codingpassion/Home/corners_main_top.gif?attredirects=0

#main
change image – used
http://sites.google.com/site/codingpassion/Home/rails_main.gif?attredirects=0

.main .widget
width: 668px;

.main .Blog
width: 684px;

#header-wrapper
change image – used
http://sites.google.com/site/codingpassion/Home/corners_cap_top.gif?attredirects=0

#header
change image – used
http://sites.google.com/site/codingpassion/Home/corners_cap_bot.gif?attredirects=0

#footer-wrap2
change image – used
http://sites.google.com/site/codingpassion/Home/corners_cap_top.gif?attredirects=0

#footer
change image – used
http://sites.google.com/site/codingpassion/Home/corners_cap_bot.gif?attredirects=0

Before


After

Tuesday, January 13, 2009

Fixing Corrupt Fonts Windows XP

Recently while I uninstalled a Game I was playing, it corrupted my Windows XP fonts. It had a copy of it taken from the Windows directory and when it prompted to delete some files, it showed the path of the game directory, and I thought it was safe to delete. But it totally messed up things and left my PC with bloated fonts.

A little bit of googling and quite a few options were suggested including reinstalling the OS (Nah, I was not planning to do that though). But this solution saved my day:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontSubstitutes]
"MS Shell Dlg 2"="Tahoma"
"MS Shell Dlg"="Tahoma"

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer]
"NoSaveSettings"=dword:00000000

Copy the above content into a text editor and save it with some name and make the extension as .reg - you can then double click this to set Tahoma back as the default font and get back the original Windows XP settings.

Or you can download the one I created from here.

Be careful with editing the registry, if you are not sure of what you are doing, i would advise you to take a backup of the registry before proceeding.

Sybase Stored procedure error Select permission denied on object

Yesterday we finally resolved a strange issue which was troubling us for a few hours.
We had a web page which was calling a newly created stored procedure. This stored procedure was returning results, but most fields of the result set were NULLS.

While we know that there is no chance for the data to go missing, we were left with the following options:
  1. Execute just the query used inside the stored procedure
  2. See the IO statistics to see if the correct tables are internally mapped (we had faced a similar issue a few weeks back, Check it here)

On running the IO statistics, we found that there was a warning - Server Error Number 10330, Severity 14. Select permission denied on object dbName..tableName.

We were logged into the database with very limited access and permissions and so after getting a login which had sufficient Select and Exec permissions, we proceeded to find out and fix the issue.

  1. There were two databases involved. Let us call it db1 and db2
  2. Stored procedure created in db1, selects from a table in db2 (db2..tablename)
  3. The db1 stored procedure was granted exec permissions to user group Group1
  4. The login used from the UI connection string belonged to the db1..group1 but did not belong to db2..group1
  5. The reason why the old stored procedure worked was because – that procedure was created by a user who had sa rights – and Sybase applies the rights of the owner to the queries inside it

Adding the login user (from the UI connection string) to db2..Group1 resolved this issue.

Thursday, December 18, 2008

SqlDbx - Really Cool

I was asked to work in a project migrating from classic ASP to .Net and the database used was Sybase ASE. The only option for DB coding, was to use the pre-installed SQLAdvantage.

The first thing I did was to search for a better tool and after getting SqlDbx I knew, I've got the best.

Guys in my team were thrilled at this product. I have recommended this one to my company's IT Tools department and I am sure each one of us would get the professional edition real soon.

Wednesday, December 17, 2008

Building a simple Dirty State Page in .Net with AJAX

I was building a small internal application for my company as a pilot application that tests the Business Object framework we had just built. This web application could well serve as a useful too.

And now I wanted to try out a simple dirty state page – a page that tracks any changes made to it and notifies accordingly to the user. With AJAX extensions, I know it was possible, there are options to extend and create your own control - like a DirtyStateUpdatePanel or something. Anyways, I just wanted to try a basic version - so here is the simple version that I came up with. Consider this as a work done for a proof of concept.

DISPLAY
We need something to notify that the page has become dirty – even a div tag should be suffice
<div id="DirtyStateNotifyDivCtl" class="dirtyPanel" runat="server">div>

CONTROLS AND EVENTS
We need to notify if any of the bound controls have changed – we should be fine with each control’s changed event. Additionally we need them all to perform one common function – notify a dirty state change. So we can have our own event declared

Public Event DirtyStateChangedEvent(ByVal getDirtyStateMethod As IDirtyPage.GetDirtyState) Implements IDirtyPage.DirtyStateChangedEvent

And its handler:

    Protected Sub ValueChanged(ByVal getDirtySateMethod As IDirtyPage.GetDirtyState) Handles Me.DirtyStateChangedEvent

        With Me.DirtyStateNotifyDivCtl

            If getDirtySateMethod.Invoke() Then

                .InnerText = "*Unsaved Changes"

            Else

                .InnerText = " "

            End If

        End With

    End Sub

And a cheeky way to expose the raise event from Master Page:

    Public Sub RaiseDirtyStateEvent(ByVal getDirtyStateMethod As IDirtyPage.GetDirtyState)

        RaiseEvent DirtyStateChangedEvent(getDirtyStateMethod)

    End Sub

POSTBACK
We need to get all our controls wrapped into an UpdatePanel to “Ajaxify” things.

DETERMINE IF DIRTY
We can wire up the page controls to one common event handler (you can have as many event handlers). And pass in a delegate of a GetDirtyState method and let the Master page take care of the display of the Unsaved change text – or clear the unsaved change text.

    Protected Sub Page_DirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles EndDateTxtCtl.TextChanged, StartDateTxtCtl.TextChanged

        Me.MasterPage.RaiseDirtyStateEvent(AddressOf Me.GetDirtyState)

    End Sub

 

    Public Function GetDirtyState() As Boolean

        ' TODOK: loop through your controls and find out if the page has become dirty by any chance

        If Me.EndDateTxtCtl.Text <> Me.TeamProject.EndDate Then

            Return True

        Else

            Return False

        End If

 

    End Function

And the icing on the cake would be the following:
<script language="javascript" type="text/javascript">

window.onbeforeunload = confirmIfDirty;

function confirmIfDirty(){

    if(document.getElementById('ctl00_DirtyStateUpdatePanelCtl').innerText > "")    {

        event.returnValue = "There are some unsaved changes. Navigating away from this page would cause such data to be lost.";

    }

}

script>

The slick onbeforeunload event pops up every time the user tries to navigate away from the page leaving it dirty.

You can find the sample files here.

 

Questions… Projects… - my two cents

A few years back, I happened to hear a glorified programmer advising his self-proclaimed sidekick how important it is to ask lesser questions to technical stakeholders/business users about the project or technology that they are working on.

I couldn’t disagree more! He was arguing that one should ask “smart” questions and should never let the other person think we do not know anything about the business by asking "stupid" questions.

Let me put this straight - There is nothing called as a stupid question. People get annoyed only when they get repeat questions. It would make them nervous on whether you are really on top of things.

For the benefit of the developer and for the benefit of the project, one should ask as many questions as possible. I am sure that clients who want the project done successfully would love to give you the answers. Even if they don’t have it with them immediately, it would kick start the thinking process and will make them revisit the requirements.

It helps to clarify a lot of things and verifies your assumptions. I strongly believe in this – “Assumption is the mother of all mess-ups”. It is wise to put forward your assumptions and sort it out. Nothing wrong in asking something basic about the business, after all you are going to be held responsible for the project you are build, aren’t you? The client would also be expecting you to know what you are doing.

Asking a lot of questions make you smart.
Asking only smart questions could leave even you in a lot of doubt.