Sunday, February 15, 2009

Intersystems Caché Column Mismatch

In one of my applications I deal with Caché, an object-oriented database systems from InterSystems. The application communicates with Caché through an ODBC driver. I added Caché as a linked server to my SQL Server 2005.

One of the queries invovled selecting all columns from a table. So I had no doubt that 

select A,B,C,D,AMT
from TBL 

query would work. Whenever I ran this query in SQL Manage Studio I got this message:

"OLE DB provider 'MSDASQL' for linked server 'LS' returned message 'Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.' Cannot get the current row value of column LS..SCH.TBL.AMT from from OLE DB provider 'MSDASQL' for linked server 'LS' Could not convert the data value due to reasons other than sign mismatch or overflow"

By process of elmination I found out that the culprit is AMT. After some reading through Cache documentations [http://vista.intersystems.com/csp/docbook/DocBook.UI.Page.cls?KEY=GIC_INTRO ; and other places ] I solved this problem. Basically although the ODBC presented the AMT column as Currency or [decimal in the linked server] that did not mean that Cache really stored only decimal in that column. And somehow that column contained alphabet characters which caused the query to fail every time it asked for the AMT column.

And to solve this problem through SQL Server I ran the following query instead

SELECT A,B,C,D,AMT
FROM 
OPENQUERY(LS,'SELECT A,B,C,D,convert(nvarchar(50),AMT) as AMT FROM TBL');

I ran into other problems with other columns and the cause is basically the discrepancy between the way the ODBC driver sees Cache columns and their datatypes and the actual layout of these columns in Cache. And openquery does a good job in helping to solve this problem.



Tuesday, February 10, 2009

Computing Science and Computers History

One of the things I like is computer science history, what was computing like a decade or two ago and was interesting and the such. Here is a talk given by Bill Gates in 1989 hosted by the University of Waterloo Computer Science Club [http://csclub.uwaterloo.ca/media/1989%20Bill%20Gates%20Talk%20on%20Microsoft
I listened to a good portion of the talk a while back and from a history persective I liked it specially the talk on OS/2.


Monday, January 26, 2009

Using Report Services 2005 Web Service

EDIT: Your problem may be easily solved if you read this first http://alsaydi.blogspot.com/2010/11/consuming-reporting-services-2005-web.html 

Today I wrote a very simple application that connects to Reporting Services 2005 web service.
The Web Services URL is:

http://[servername]/ReportServer/ReportService2005.asmx

Many sources give the following lines of code as a sample (assuming you have all the proper using statements)

ReportingService2005 rs = new ReportingService2005();
rs.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
CatalogItem [] items = rs.ListChildren("/Sales Reports", true);

I first tried this with Visual Studio 2008 and noticed that there is no ReportingService2005 class. The closes class ReportingService2005SoapClient. So I switched to Visual Studio 2005 and the VS IntelliSense showed ReportingService2005 and my code worked.

After a some time of playing around I found a way to get the code to work in VS2008.

The app.config file of t he VS2008 a couple of changes needs to be made:

the binding tage , the property allowCookies has to be set true (I think not sure)


<security mode="TransportCredentialOnly">                                          
<transport clientCredentialType="Windows" proxyCredentialType="Windows"
realm="" />
</security>



This simple will use Windows integrated authentication.
One more line of code is need to allow impersonation:
rs.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

assuming rs is an instance of ReportingService2005SoapClient.

So a sample code looks something like the following:

ReportingService2005SoapClient rs = new ReportingService2005SoapClient();
rs.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
CatalogItem[] items;
rs.ListChildren("/", true, out items);
dataGridView1.DataSource = items;

Cool things can be done through RS2005 Web Service!

Saturday, January 24, 2009

Windows Resource Toolkit

Windows resource kits, can be obtained from here (http://www.microsoft.com/downloads/details.aspx?familyid=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en), has some really valuable tools. I use cdburn often. Yesterday I was playing with remapkey.exe and I can see some good uses for it and some evil (annoying ) ones too.

A more important utility in the kit is a utility that lets you search multiple servers events logs. This can be really helpful when debugging a problem in a domain eventcombmt.exe.

oh.exe lists open hanldes is another useful utiltiy in the kit.


Thursday, January 22, 2009

كلام قيم لأبي حامد الغزالي



بلغني أن كتبا تؤلف من قبل مسلمين كيما في حد رأيهم ترد على نظريات أتى بها العلم الحديث . هذه الكتب و الله , تخلق
 تعارضا من حيث لا يوجد و يا ليت من يكتبها أو يدافع عنها قرأ شيئا من تهافت الفلاسفة للغزالي رحمه الله.الإمام أبو حامد الغزالي عاش في نهاية القرن الخامس الهجري أي بعد بداية فترة النشاط العلمي الإسلامي و بعد مرحلة الترجمة من كتب  اليونان و الإغريق إلى العربية.  وهذا إضافة إلى مقام هذا العالم الجليل يجعل رأيه قيما جدا في الرد على من يريد أن يعارض نظريات علمية ثابتة بقوانين عقلية رياضية و فيزيائية بناءاً على فهم معين من لديه لآيات من القرآن الكريم و الأحاديث النبوية الشريفة. فمن ذلك الكتاب أنقل ما يلي. حيث قال المؤلف في مقدمة الكتاب مبينا ما سيتعرض له الكتاب و ما ما لا يتعرض له الكتاب في رده على الفلاسفة 

"القسم الثاني: ما لا يصدم مذهبهم (أي الفلاسفة) فيه أصلاً من أصول الدين، وليس من ضرورة تصديق الأنبياء والرسل منازعتهم فيه، كقولهم: إن كسوف القمر، عبارة عن انمحاء ضوء القمر بتوسط الأرض بينه وبين الشمس، والأرض كرة والسماء محيطة بها من الجوانب، وإن كسوف الشمس، وقوف جرم القمر بين الناظر وبين الشمس عند اجتماعهما في العقيدتين على دقيقة واحدة.
وهذا الفن أيضاً لسنا نخوض في إبطاله إذ لا يتعلق به غرض ومن ظن أن المناظرة في ابطال هذا من الدين فقد جنى على الدين، وضَعَّف أمره، فإن هذه الأمور تقوم عليها براهين هندسية حسابيَّة لا يبقى معها ريبة. فمن تطلَّع عليها، ويتحقَّق أدلّتها، حتى يُخبر بسببها عن وقت الكسوفين وقدرهما ومدة بقائهما إلى الانجلاء، إذا قيل له إن هذا على خلاف الشرع، لم يسترب فيه، وإنما يستريب في الشرع، وضرر الشرع ممَّن ينصره لا بطريقه أكثر من ضرره ممّن يطعن فيه بطريقة. وهو كما قيل: عدوّ عاقل خير من صديق جاهل." 
فتأمل قوله أن هذه الأمور مبنة على مبادئ هندسية رياضية لا يبقى بعد النظر فيها ريبة لصاحب عقل. ثم انظر إلى تبيينه أن ضرر الشرع ممن ينصره لا بطريقه أكثر من ضرره ممن يطعن فيه بطريقة اه. فليت قومي يعلمون. 
ثم يقول الغزالي مستطردا :

وأعظم ما يفرح به المُلحدة، أن يصرح ناصر الشرع بأن هذا، وأمثاله ( اي ألامور الثابتة بالراهين الرياضية) على خلاف الشرع، فيسهل عليه طريق إبطال الشرع، ان كان شرطع امثال ذلك. وهذا: لأنّ البحث في العالم عن كونه حادثاً أو قديماً، ثم إذا ثبت حدوثه فسواء كان كرة، أو بسيطاً، أو مثمناً، أو مسدّساً، وسواء كانت السماوات، وما تحتها ثلاثة عشرة طبقة، كما قالوه، أو أقلّ، أو أكثر، فنسبة النظر فيه الى البحث الالهىّ كنسبة النظر الى طبقات البصل وعددها وعدد حبّ الرمان. فالمقصود: كونه من فعل الله سبحانه وتعالى فقط، كيف ما كانت." فالله المستعان.

من أجمل ما قرأته لأحمد شوقي رحمه الله ما قال 
رُدَّت الروحُ على المُضْنَى معكْ 
أحسن الأيام يوم أرجعك

تجد بقية الأبيات هنا 



Windows Console Program to Eject CD-ROM

I find the *nix command "eject" very convienent to eject cd/dvd. There is not builtin command in Windows but microsoft has a sample code here http://support.microsoft.com/kb/q165721/ that does just that. Here is the code with few modification i made. The syntax of the command is eject.exe {driver letter} [/c]. The /c switch is to close the drive bay.

/**
http://support.microsoft.com/kb/q165721/
*/
#include
#include
#include
#include
// Prototypes
BOOL EjectVolume(TCHAR cDriveLetter);
HANDLE OpenVolume(TCHAR cDriveLetter);
BOOL LockVolume(HANDLE hVolume);
BOOL DismountVolume(HANDLE hVolume);
BOOL PreventRemovalOfVolume(HANDLE hVolume, BOOL fPrevent);
BOOL AutoEjectVolume(HANDLE hVolume);
BOOL CloseVolume(HANDLE hVolume);
BOOL EJECT = TRUE;
//default is to eject device;
but a command switch can tell program to close cdrom
LPTSTR szVolumeFormat = TEXT("\\\\.\\%c:");
LPTSTR szRootFormat = TEXT("%c:\\");
LPTSTR szErrorFormat = TEXT("Error %d: %s\n");
void ReportError(LPTSTR szMsg)
{
    _tprintf(szErrorFormat, GetLastError(), szMsg);
}
HANDLE OpenVolume(TCHAR cDriveLetter)
{
    HANDLE hVolume;
    UINT uDriveType;
    TCHAR szVolumeName[8];
    TCHAR szRootName[5];
    DWORD dwAccessFlags;
    wsprintf(szRootName, szRootFormat, cDriveLetter);
    uDriveType = GetDriveType(szRootName);
    switch(uDriveType)
    {
        case DRIVE_REMOVABLE:
        dwAccessFlags = GENERIC_READ | GENERIC_WRITE;
        break;
        case DRIVE_CDROM:
        dwAccessFlags = GENERIC_READ;
        break;
        default:
        _tprintf(TEXT("Cannot eject.  Drive type is incorrect.\n"));
        return INVALID_HANDLE_VALUE;
    }
    wsprintf(szVolumeName, szVolumeFormat, cDriveLetter);
    hVolume = CreateFile(   szVolumeName,
    dwAccessFlags,
    FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL,
    OPEN_EXISTING,
    0,
    NULL );
    if (hVolume == INVALID_HANDLE_VALUE)
    ReportError(TEXT("CreateFile"));
    return hVolume;
}
BOOL CloseVolume(HANDLE hVolume)
{
    return CloseHandle(hVolume);
}
#define LOCK_TIMEOUT        10000       // 10 Seconds (1 s == 1000 ms)
#define LOCK_RETRIES        20
BOOL LockVolume(HANDLE hVolume)
{
    DWORD dwBytesReturned;
    DWORD dwSleepAmount;
    int nTryCount;
    dwSleepAmount = LOCK_TIMEOUT / LOCK_RETRIES;
    // Do this in a loop until a timeout period has expired
    for (nTryCount = 0;
    nTryCount &lt;
    LOCK_RETRIES;
    nTryCount++)
    {
        if (DeviceIoControl(hVolume,
        FSCTL_LOCK_VOLUME,
        NULL, 0,
        NULL, 0,
        &dwBytesReturned,
        NULL))
        return TRUE;
        Sleep(dwSleepAmount);
    }
    return FALSE;
}
BOOL DismountVolume(HANDLE hVolume)
{
    DWORD dwBytesReturned;
    return DeviceIoControl( hVolume,
    FSCTL_DISMOUNT_VOLUME,
    NULL, 0,
    NULL, 0,
    &dwBytesReturned,
    NULL);
}
BOOL PreventRemovalOfVolume(HANDLE hVolume, BOOL fPreventRemoval)
{
    DWORD dwBytesReturned;
    PREVENT_MEDIA_REMOVAL PMRBuffer;
    PMRBuffer.PreventMediaRemoval = fPreventRemoval;
    return DeviceIoControl( hVolume,
    IOCTL_STORAGE_MEDIA_REMOVAL,
    &PMRBuffer, sizeof(PREVENT_MEDIA_REMOVAL),
    NULL, 0,
    &dwBytesReturned,
    NULL);
}
AutoEjectVolume(HANDLE hVolume)
{
    DWORD dwBytesReturned;
    if(EJECT)
    return DeviceIoControl( hVolume,
    //        IOCTL_STORAGE_LOAD_MEDIA ,
    IOCTL_STORAGE_EJECT_MEDIA ,
    NULL, 0,
    NULL, 0,
    &dwBytesReturned,
    NULL);
    return DeviceIoControl( hVolume,
    IOCTL_STORAGE_LOAD_MEDIA ,
    //        IOCTL_STORAGE_EJECT_MEDIA ,
    NULL, 0,
    NULL, 0,
    &dwBytesReturned,
    NULL);
}
BOOL EjectVolume(TCHAR cDriveLetter)
{
    HANDLE hVolume;
    BOOL fRemoveSafely = FALSE;
    BOOL fAutoEject = FALSE;
    // Open the volume.
    hVolume = OpenVolume(cDriveLetter);
    if (hVolume == INVALID_HANDLE_VALUE)
    return FALSE;
    // Lock and dismount the volume.
    if (LockVolume(hVolume) && DismountVolume(hVolume))
    {
        fRemoveSafely = TRUE;
        // Set prevent removal to false and eject the volume.
        if (PreventRemovalOfVolume(hVolume, FALSE) &&
        AutoEjectVolume(hVolume))
        fAutoEject = TRUE;
    }
    // Close the volume so other processes can use the drive.
    if (!CloseVolume(hVolume))
    return FALSE;
    if (fAutoEject)
    if(EJECT)
    printf("Media in Drive %c has been ejected safely.\n",
    cDriveLetter);
    else
    {
        if (fRemoveSafely)
        {
            if(EJECT)
            printf("Media in Drive %c can be safely removed.\n",
            cDriveLetter);
        }
    }
    return TRUE;
}
void Usage()
{
    printf("Usage: Eject [/C] \n");
    printf("When /c is supplied, the program will close CD-ROM bay\n\n");
    return ;
}
void main(int argc, char * argv[])
{
    if (argc &lt;
    2)
    {
        Usage();
        return ;
    }
    if(argc == 3)
    {
        if(strlen(argv[2])&gt;
        1 && argv[2][1] == 'c' || argv[2][1]== 'C')
        {
            EJECT = FALSE;
        }
    }
    if (!EjectVolume(argv[1][0]))
    {
        printf("Failure ejecting drive %c.\n\n", argv[1][0]);
        Usage();
    }
    return ;
}