Wednesday, April 29, 2009

Enabled/Disable HTML Elements


function EnableDisabled(elName)
{
var el = document.getElementById(elName);
if(el){
try {el.disabled = el.disabled ? false : true;}
catch(ex){}

if (el.childNodes && el.childNodes.length > 0)
{
for (var x = 0; x < el.childNodes.length; x++)
{
EnableDisabled(el.childNodes[x]);
}
}
}
}

Monday, April 27, 2009

VB.Net - Export DataTable to CSV

Public Sub ExportDataTableToCSV(ByRef dtSource As DataTable, _
ByVal filename As String)

Dim context As HttpContext = HttpContext.Current
context.Response.Clear()
For Each column As DataColumn In dtSource.Columns
context.Response.Write(column.ColumnName + ",")
Next
context.Response.Write(Environment.NewLine)

For Each row As DataRow In dtSource.Rows
For i As Integer = 0 To dtSource.Columns.Count - 1
context.Response.Write(row(i).ToString().Replace(",", String.Empty) + ",")
Next
context.Response.Write(Environment.NewLine)
Next

context.Response.ContentType = "text/csv"
context.Response.AppendHeader("Content-Disposition", _
"attachment; filename=" + filename + ".csv")
context.Response.End()

End Sub

Saturday, April 25, 2009

VB.Net - Get Week Number of a date

Public Function GetWeekNumber(ByVal inDate As DateTime) As Integer
Const JAN As Integer = 1
Const DEC As Integer = 12
Const LASTDAYOFDEC As Integer = 31
Const FIRSTDAYOFJAN As Integer = 1
Const THURSDAY As Integer = 4
Dim ThursdayFlag As Boolean = False

' Get the day number since the beginning of the year
Dim DayOfYear As Integer = inDate.DayOfYear

' Get the numeric weekday of the first day of the
' year (using sunday as FirstDay)
Dim StartWeekDayOfYear As Integer = _
DirectCast(New DateTime(inDate.Year, JAN, FIRSTDAYOFJAN).DayOfWeek, Integer)
Dim EndWeekDayOfYear As Integer = _
DirectCast(New DateTime(inDate.Year, DEC, LASTDAYOFDEC).DayOfWeek, Integer)

' Compensate for the fact that we are using monday
' as the first day of the week
If StartWeekDayOfYear = 0 Then
StartWeekDayOfYear = 7
End If
If EndWeekDayOfYear = 0 Then
EndWeekDayOfYear = 7
End If

' Calculate the number of days in the first and last week
Dim DaysInFirstWeek As Integer = 8 - StartWeekDayOfYear
Dim DaysInLastWeek As Integer = 8 - EndWeekDayOfYear

' If the year either starts or ends on a thursday it will have a 53rd week
If StartWeekDayOfYear = THURSDAY OrElse EndWeekDayOfYear = THURSDAY Then
ThursdayFlag = True
End If

' We begin by calculating the number of FULL weeks
' between the start of the year and
' our date. The number is rounded up, so the smallest possible value is 0.
Dim FullWeeks As Integer = _
CType(Math.Ceiling((DayOfYear - DaysInFirstWeek) / 7), Integer)

Dim WeekNumber As Integer = FullWeeks

' If the first week of the year has at least four days,
' then the actual week number for our date
' can be incremented by one.
If DaysInFirstWeek >= THURSDAY Then
WeekNumber = WeekNumber + 1
End If

' If week number is larger than week 52
' (and the year doesn't either start or end on a thursday)
' then the correct week number is 1.
If WeekNumber > 52 AndAlso Not ThursdayFlag Then
WeekNumber = 1
End If

' If week number is still 0,
' it means that we are trying to evaluate the week number for a
' week that belongs in the previous year
' (since that week has 3 days or less in our date's year).
' We therefore make a recursive call using the last day of the previous year.
If WeekNumber = 0 Then
WeekNumber = GetWeekNumber( _
New DateTime(inDate.Year - 1, DEC, LASTDAYOFDEC))
End If

Return WeekNumber
End Function

Monday, April 20, 2009

VB.Net - Get Weeks In a Year


Public Function GetWeeksInYear(ByVal dayofsunday As DateTime) As Integer

Dim dayofthursday As DateTime = dayofsunday.AddDays(-3)
Dim yearno As Integer = dayofthursday.Year
Dim firstday As DateTime = "01-Jan-" & yearno.ToString

Dim totalweeks As Integer = 0

If firstday.DayOfWeek = DayOfWeek.Thursday Then
totalweeks = 53
ElseIf Date.IsLeapYear(yearno) = True _
And firstday.DayOfWeek = DayOfWeek.Wednesday Then
totalweeks = 53
Else
totalweeks = 52
End If

Return totalweeks
End Function

Tuesday, April 7, 2009

How to recover a database marked suspect?


SQL Server 2000/2005/2008
Recover database suspect sql server
The following script resets the status of the database and checks the database for integrity.

USE master;

GO

EXEC sp_resetstatus 'MYDATABASE';

GO

USE MYDATABASE;

DBCC CHECKDB WITH NO_INFOMSGS;

GO

SQL Server 2005/2008
Recover database marked suspect sql serve

USE master;

GO

ALTER DATABASE MYDATABASE SET EMERGENCY

GO

ALTER DATABASE MYDATABASE SET SINGLE_USER

GO

DBCC CHECKDB (MYDATABASE, REPAIR_ALLOW_DATA_LOSS) WITH NO_INFOMSGS;

GO

USE MYDATABASE;

DBCC CHECKDB WITH NO_INFOMSGS;

GO

Friday, April 3, 2009

SQL Server 2005 - Get Size of Database and Individual Tables

-- Get size of Database
EXEC DATABASENAME.dbo.sp_spaceused

-- Get size of a Table in database
EXEC DATABASENAME.dbo.sp_spaceused 'TABLENAME'

-- Get size of All tables in a database
EXEC DATABASENAME.dbo.sp_spaceused '?'

-- Sql Query
USE DATABASENAME
GO
DECLARE @database_size DECIMAL(15,2)
SELECT @database_size = SUM(((convert (dec (15,2),[size]) * 8192 / 1048576)))
FROM dbo.sysfiles
PRINT @database_size

Thursday, April 2, 2009

Software Configuration Management (SCM)

Software Configuration Management (SCM) is part of configuration management (CM). Roger Pressman, in his book Software Engineering: A Practitioner's Approach, says that software configuration management (SCM) is a "set of activities designed to control change by identifying the work products that are likely to change, establishing relationships among them, defining mechanisms for managing different versions of these work products, controlling the changes imposed, and auditing and reporting on the changes made." In other words, SCM is a methodology to control and manage a software development project.

SCM concerns itself with answering the question: somebody did something, how can one reproduce it? Often the problem involves not reproducing "it" identically, but with controlled, incremental changes. Answering the question will thus become a matter of comparing different results and of analysing their differences. Traditional CM typically focused on controlled creation of relatively simple products. Nowadays, implementers of SCM face the challenge of dealing with relatively minor increments under their own control, in the context of the complex system being developed.

Variety of artifacts may be managed and versioned, including software code, documents, design models,and even the directory structure itself

The goals of SCM are generally:
Configuration Identification- What code are we working with?
Configuration Control- Controlling the release of a product and its changes.
Status Accounting- Recording and reporting the status of components.
Review- Ensuring completeness and consistency among components.
Build Management- Managing the process and tools used for builds.
Process Management- Ensuring adherence to the organization's development process.
Environment Management- Managing the software and hardware that host our system.
Teamwork- Facilitate team interactions related to the process.
Defect Tracking- making sure every defect has traceability back to the source

Wednesday, April 1, 2009

SQL - Update table A from table B

-- SIMPLE UPDATE QUERY FROM TABLE A TO B
UPDATE TABLEA
SET TABLEA.[NAME] = TABLEB.[NAME]
FROM TABLEB
WHERE TABLEB.ID = TABLEA.ID

-- USING JOINS
UPDATE TABLEA
SET TABLEA.[NAME] = TABLEB.[NAME]
FROM TABLEB
INNER JOIN TABLEC
ON TABLEC.FIELD1 = TABLEB.FIELD1
WHERE TABLEB.ID = TABLEA.ID

VB.Net - Get Directory / Folder Size


Public Function GetFolderSize(ByVal path As String, _
ByVal includeSubFolders As Boolean) As Long

GetFolderSize = 0

Dim main_dir As New System.IO.DirectoryInfo(path)
Dim dir_files() As FileInfo

If includeSubFolders Then
dir_files = main_dir.GetFiles("*", SearchOption.AllDirectories)
Else
dir_files = main_dir.GetFiles("*", SearchOption.TopDirectoryOnly)
End If

For Each ofile As IO.FileInfo In dir_files
GetFolderSize = GetFolderSize + ofile.Length
Next

End Function