Tuesday, May 31, 2005

No keydown event fires in a datagrid

I wanted to handle the key down event of a datagrid. But unfortunately havent succeeded in it. Found out this

No keydown event fires in a datagrid

If any of you out there know the solution, then do let me know ASAP.

Try IT and enjoy!!!

Sunday, May 29, 2005

Useful Scripts for SQL Server 2000

/*
I work mostly on SQL Server 2000, and try to be as dynamic as possible. For this reason i created some SCRIPTS.
*/

/*
1. This SCRIPTS will return all the tables in a database.
It is useful if you want a select or delete query for every table.
*/
DECLARE @TABLENAME VARCHAR(100)

DECLARE ALLTABLES CURSOR FOR
SELECT NAME FROM SYSOBJECTS WHERE XTYPE='U' AND SYSSTAT <> 8275
ORDER BY NAME

OPEN ALLTABLES

FETCH NEXT FROM ALLTABLES INTO @TABLENAME

WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'SELECT top 100 * FROM [' + @TABLENAME + ']'
--PRINT 'DELETE FROM [' + @TABLENAME + ']'
FETCH NEXT FROM ALLTABLES INTO @TABLENAME
END

CLOSE ALLTABLES
DEALLOCATE ALLTABLES


/*
2. This SCRIPTS will return all the Column names for every user table in a database.

*/

DECLARE @TABLENAME VARCHAR(100)
DECLARE @TABLENAME2 VARCHAR(100)
DECLARE @TABLEID INT
DECLARE @COLNAME VARCHAR(100)
/*
--COLUMN COUNT IN TABLES
SELECT SO.NAME [Table], COUNT(SC.NAME) [Columns] FROM SYSOBJECTS SO, SYSCOLUMNS SC
WHERE SO.ID = SC.ID AND SO.XTYPE='U' AND SYSSTAT <> 8275
GROUP BY SO.NAME
ORDER BY SO.NAME

*/

DECLARE ALLTABLES CURSOR FOR
SELECT SO.[NAME], SC.[NAME] FROM
SYSOBJECTS SO INNER JOIN SYSCOLUMNS SC ON SO.ID = SC.ID
WHERE SO.XTYPE='U' AND SYSSTAT <> 8275
ORDER BY SO.[NAME], SC.[NAME]

OPEN ALLTABLES

FETCH NEXT FROM ALLTABLES INTO @TABLENAME, @COLNAME
DECLARE @COLS VARCHAR(1000)
SET @COLS = ''
WHILE @@FETCH_STATUS = 0
BEGIN

IF @TABLENAME = @TABLENAME2
BEGIN
PRINT @COLNAME
END
ELSE
BEGIN
PRINT ''
PRINT 'TABLE: [' + @TABLENAME + ']'
PRINT @COLNAME
SET @TABLENAME2 = @TABLENAME
END

FETCH NEXT FROM ALLTABLES INTO @TABLENAME, @COLNAME
END

CLOSE ALLTABLES
DEALLOCATE ALLTABLES


/*
3. Now to put the above knowledge into something important (atleast for me)
If you change the SELECT query to the one shown below, then it will only the first columns of each table, a good way to return the primary keys of tables, usually the first column of each table is the primary key. It will also be helpful if you want to make a fucntion, that return the next primary key value to help you inserts records.

SELECT SO.[NAME], SC.[NAME], SC.* FROM
SYSOBJECTS SO INNER JOIN SYSCOLUMNS SC ON SO.ID = SC.ID
WHERE SO.XTYPE='U' AND SYSSTAT <> 8275
AND SC.COLORDER = 1
ORDER BY SO.[NAME], SC.[NAME]

This script will return a script that will create a function, which takes as input a tablename and returns the next primary key value if the primary key is a numeric field.
*/

--USE DATABSENAME
DECLARE @TABLENAME VARCHAR(100)
DECLARE @COLNAME VARCHAR(100)

DECLARE ALLTABLES CURSOR FOR
SELECT UPPER(SO.[NAME]), UPPER(SC.[NAME]) FROM
SYSOBJECTS SO INNER JOIN SYSCOLUMNS SC ON SO.ID = SC.ID
WHERE SO.XTYPE='U' AND SYSSTAT <> 8275
AND SC.COLORDER = 1
ORDER BY SO.[NAME], SC.[NAME]

OPEN ALLTABLES

FETCH NEXT FROM ALLTABLES INTO @TABLENAME, @COLNAME

PRINT 'CREATE FUNCTION fnGetIDByName (@TABLENAME VARCHAR(100)) '
PRINT 'RETURNS INT AS '
PRINT 'BEGIN'
PRINT ' DECLARE @ID INT'

PRINT ' IF UPPER(@TABLENAME) = ''' + @TABLENAME + ''''
PRINT ' SET @ID = (SELECT ISNULL(MAX([' + @COLNAME + ']),0)+1 FROM [' + @TABLENAME + '])'

FETCH NEXT FROM ALLTABLES INTO @TABLENAME, @COLNAME

WHILE @@FETCH_STATUS = 0
BEGIN
BEGIN
PRINT ' ELSE IF UPPER(@TABLENAME) = ''' + @TABLENAME + ''''
PRINT ' SET @ID = (SELECT MAX([' + @COLNAME + ']) FROM [' + @TABLENAME + '])'
END
FETCH NEXT FROM ALLTABLES INTO @TABLENAME, @COLNAME
END

PRINT ' ELSE '
PRINT ' SET @ID = 0'
PRINT ' RETURN(@ID)'
PRINT 'END'

CLOSE ALLTABLES
DEALLOCATE ALLTABLES

/*
How to execute this procedure
SELECT [dbo].[fnGetIDByName]('TABLENAME')

ANOTHER USE
INSERT INTO TABLENAME(PRIMARYKEY, ANYFIELD, ...)
VALUES ([dbo].[fnGetIDByName]('TABLENAME'), @ANYFIELD, ....)

There could be better use of this kind of scripts.
And there could be better ways of writing them as well. This is to my knowledge.

The important thing is the SYS.... tables in a SQL Server databases. They are tables that keep all the information about user tables, views, procedures etc. If you look closely in them, the primary key in them is the [id] field, make joins with it and you can get every bit of information about the database that you are working on.

Try IT and enjoy!!

*/

Saturday, May 28, 2005

BEDatePicker for Arabic

Click here to download the control, its an ActiveX control dll, made in .NET.

Try it.

Or want to see some other applications.

Goto TheBukharis.com.

Have a nice time.

.Net developer site from MS Arabia

Its new, so it will take time to fill it up with informations.
Keep checking it if you are in programing for Arabic language.

See IT

Wednesday, May 25, 2005

Hijri Calendar


VS.NET is cool, and you can do cool things with it as well.
I had a problem of saving Hijri dates in an application. To solve it i made a DateTimePicker for Hijri calendar. But in hijri calendar the main problem is of not knowing the number of days in a month and also not knowing on which date, which day will fall. As hijri calendar is based on Moon. If you see the moon then the months starts, and so no one can predict the dates.

The other problem, is showing arabic dates in Arabic numerals or characters.

So in my calendar, i dont have options like Day names or the number of days in a month. It contantly shows 30 days.

had to work hard to make it, but after completeion it looks cool.

Here is some things that are needed to complete this task in Visual Basic.Net

Import these classes.

Imports System.Threading
Imports System.Globalization

Convert the current date to Hijri date.
Dim myCal As New HijriCalendar
Year = myCal.GetYear(Now)
Month = myCal.GetMonth(Now)
Day = myCal.GetDayOfMonth(Now)

But if you want to show Arabic numerals as well.

"٠١٢٣٤٥٦٧٨٩" Numerics
"محرم", "صفر", "ربيع الأول", "ربيع الثاني", "جمادىالأولى", "جمادىالأخرة", "رجـــب", "شعبان", "رمضان", "شوال", "زوالقعدة", "زوالحجة" Months

All you need now is some functions to return the arabic numerals for english and the month name for specific month.

In SQL Server.

you can easily convert a varchar date to hijri date through

select CONVERT(datetime, '16/04/1426 5:51:59:967PM', 130)
Return '2005-05-25 17:51:59.967'

select CONVERT(nvarchar, getdate(), 130)
Return '16 ربيع الثاني 1426 5:53:00:5'
select CONVERT(nvarchar, getdate(), 131)
Return '16/04/1426 5:53:00:500PM'

The values 130 and 131 are called the Quati Algorithm to convert dates


Rest is all logic that you use to make this happen.

:D