Tuesday, March 11, 2008

Export Data to Excel in VB

Hi,


the folowing code helps you to create Excelsheet in Vb..

Check it out

Dim oExcel As New Excel.Application()
Dim oBooks As Excel.Workbooks, oBook As Excel.Workbook
Dim oSheets As Excel.Sheets, oSheet As Excel.Worksheet
Dim oCells As Excel.Range
Dim sFile As String, sTemplate As String
Dim dt As DataTable = _
CType(Application.Item("MyDataTable"), DataTable)

sFile = Server.MapPath(Request.ApplicationPath) & _
"\MyExcel.xls"

sTemplate = Server.MapPath(Request.ApplicationPath) & _
"\MyTemplate.xls"

oExcel.Visible = False : oExcel.DisplayAlerts = False

'Start a new workbook
oBooks = oExcel.Workbooks
oBooks.Open(Server.MapPath(Request.ApplicationPath) & _
"\MyTemplate.xls") 'Load colorful template with chart
oBook = oBooks.Item(1)
oSheets = oBook.Worksheets
oSheet = CType(oSheets.Item(1), Excel.Worksheet)
oSheet.Name = "First Sheet"
oCells = oSheet.Cells

DumpData(dt, oCells) 'Fill in the data

oSheet.SaveAs(sFile) 'Save in a temporary file
oBook.Close()

'Quit Excel and thoroughly deallocate everything
oExcel.Quit()
ReleaseComObject(oCells) : ReleaseComObject(oSheet)
ReleaseComObject(oSheets) : ReleaseComObject(oBook)
ReleaseComObject(oBooks) : ReleaseComObject(oExcel)
oExcel = Nothing : oBooks = Nothing : oBook = Nothing
oSheets = Nothing : oSheet = Nothing : oCells = Nothing
System.GC.Collect()
Response.Redirect(sFile) 'Send the user to the file


Happy Programming

Thursday, March 6, 2008

Time Zone Conversions in SQL

Hi..

While surfing net i found a beautiful article..

The followiing function convert the Time zones based on the input given.. check the functions and also a table called time zone should be created that consists of OfSet values


CREATE FUNCTION udf_Timezone_Conversion(
@Source_Timezone varchar(25),
@Destination_Timezone varchar(25),
@Source_datetime datetime,
@Display_Timezone bit = 0)
RETURNS varchar(50)AS
BEGIN
--------------------------------------------------------------------------------------------------------------- Declarations-------------------------------------------------------------------------------------------------------------
DECLARE @Source_DST bit
DECLARE @Destination_DST bit
DECLARE @converted_date datetime
DECLARE @converted_timezone varchar(50)
DECLARE @year int
DECLARE @AprilDate datetime
DECLARE @OctDate datetime
DECLARE @DST_Start datetime
DECLARE @DST_End datetime
DECLARE @GMT_Offset_Source int
DECLARE @GMT_Offset_Destination int
DECLARE @converted_datetime varchar(50)
--------------------------------------------------------------------------------------------------------------- Initializations-------------------------------------------------------------------------------------------------------------
SELECT @year = DATEPART(year, @Source_datetime)
SELECT @AprilDate = 'Apr 15 ' + CONVERT(char(4), @year)
SELECT @OctDate = 'Oct 15 ' + CONVERT(char(4), @year)
SELECT @DST_Start = DATEADD(hour, 2, (dbo.udf_FirstSundayOfTheMonth(@AprilDate)))SELECT @DST_End = DATEADD(hour, 2, (dbo.udf_LastSundayOfTheMonth(@OctDate)))SELECT @DST_End = DATEADD(second, -1, @DST_End)

SELECT @GMT_Offset_Source = GMT_Offset FROM TIMEZONE WHERE Timezone_Name = @Source_Timezone
SELECT @GMT_Offset_Destination = GMT_Offset FROM TIMEZONE WHERE Timezone_Name = @Destination_Timezone
SELECT @Source_DST = DST_bit FROM TIMEZONE WHERE Timezone_Name = @Source_TimezoneSELECT @Destination_DST = DST_bit FROM TIMEZONEWHERE Timezone_Name = @Destination_Timezone
--------------------------------------------------------------------------------------------------------------- Check for valid inputs-------------------------------------------------------------------------------------------------------------
IF @Source_Timezone NOT IN (SELECT Timezone_Name FROM TIMEZONE) OR @Destination_Timezone NOT IN (SELECT Timezone_Name FROM TIMEZONE) RETURN 'You have entered an invalid time zone.'
--------------------------------------------------------------------------------------------------------------- Source date and time are during DST and both time zones observe DST-- Convert source time zone to GMT, then convert GMT to destination time zone-- Check if destination date and time are not in DST after the conversion-------------------------------------------------------------------------------------------------------------

IF (@Source_datetime BETWEEN @DST_Start AND @DST_End) AND (@Destination_DST = 1) AND (@Source_DST = 1)
BEGIN
SELECT @converted_date = DATEADD(MINUTE, - @GMT_Offset_Source - 60, @Source_datetime)
SELECT @converted_date = DATEADD(MINUTE, @GMT_Offset_Destination + 60, @converted_date)
IF @converted_date NOT BETWEEN @DST_Start AND @DST_End
BEGIN
SELECT @converted_timezone = @Destination_Timezone
SELECT @converted_date = DATEADD(MINUTE, -60, @converted_date) END ELSE SELECT @converted_timezone = DST_Abbrv FROM TIMEZONE WHERE Timezone_Name = @Destination_TimezoneEND
--------------------------------------------------------------------------------------------------------------- Source data and time are not during DST-- Convert source time zone to GMT, then convert GMT to destination time zone-- Check if destination data and time are in DST after the conversion-- If destination date and time are in DST, check if it observes DST-------------------------------------------------------------------------------------------------------------ELSE IF
(@Source_datetime NOT BETWEEN @DST_Start AND @DST_End)
OR ((@Source_datetime BETWEEN @DST_Start AND @DST_End)
AND (@Destination_DST = 0) AND (@Source_DST = 0))

BEGIN
SELECT @converted_date = DATEADD(MINUTE, - @GMT_Offset_Source, @Source_datetime) SELECT @converted_date = DATEADD(MINUTE, @GMT_Offset_Destination, @converted_date)
IF (@converted_date BETWEEN @DST_Start AND @DST_End) AND (@Destination_DST = 1)
BEGIN
SELECT @converted_date = DATEADD(MINUTE, 60, @converted_date)
SELECT @converted_timezone = DST_Abbrv FROM TIMEZONE WHERE Timezone_Name = @Destination_Timezone
END
ELSE

SELECT @converted_timezone = @Destination_TimezoneEND
--------------------------------------------------------------------------------------------------------------- Source date and time are during DST and only source time zone observes DST-- Convert source time zone to GMT, then convert GMT to destination time zone-- Check if destination date and time are not in DST after the conversion-------------------------------------------------------------------------------------------------------------
ELSE IF (@Source_datetime BETWEEN @DST_Start AND @DST_End) AND (@Destination_DST = 0) AND (@Source_DST = 1)
BEGIN
SELECT @converted_date = DATEADD(MINUTE, - @GMT_Offset_Source - 60, @Source_datetime)
SELECT @converted_date = DATEADD(MINUTE, @GMT_Offset_Destination, @converted_date) SELECT @converted_timezone = @Destination_Timezone
END
--------------------------------------------------------------------------------------------------------------- Source date and time are during DST and only destination time zone observes DST-- Convert source time zone to GMT, then convert GMT to destination time zone-- Check if destination date and time are not in DST after the conversion-------------------------------------------------------------------------------------------------------------
ELSE IF (@Source_datetime BETWEEN @DST_Start AND @DST_End) AND (@Destination_DST = 1) AND (@Source_DST = 0)
BEGIN
SELECT @converted_date = DATEADD(MINUTE, - @GMT_Offset_Source, @Source_datetime)
SELECT @converted_date = DATEADD(MINUTE, @GMT_Offset_Destination + 60, @converted_date)
IF @converted_date NOT BETWEEN @DST_Start AND @DST_End BEGIN SELECT @converted_timezone = @Destination_Timezone
SELECT @converted_date = DATEADD(MINUTE, -60, @converted_date) END ELSE SELECT @converted_timezone = DST_Abbrv FROM TIMEZONE WHERE Timezone_Name = @Destination_Timezone
END
--------------------------------------------------------------------------------------------------------------- Format the output using style 21-------------------------------------------------------------------------------------------------------------
IF @Display_Timezone = 0 SELECT @converted_datetime = CONVERT(varchar(50), @converted_date, 21)ELSE SELECT @converted_datetime = CONVERT(varchar(50), @converted_date, 21) + '; ' + UPPER(@converted_timezone)--------------------------------------------------------------------------------------------------------------- Return the output-------------------------------------------------------------------------------------------------------------
RETURN
@converted_datetime
END


************TimeZoneTable**********************


CREATE TABLE [dbo].[TIMEZONE]
( [Timezone_Name] [varchar] (25) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [GMT_Offset] [float] NOT NULL ,
[DST_Abbrv] [varchar] (6) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[DST_bit] [bit] NOT NULL ) ON [PRIMARY]GOALTER TABLE [dbo].[TIMEZONE] ADD CONSTRAINT [PK_TIMEZONE] PRIMARY KEY CLUSTERED ( [Timezone_Name] ) WITH FILLFACTOR = 90 ON [PRIMARY]

GO

*******************************Time Zone Values ************************

+0200 120.0 0
+0300 180.0 0
+0400 240.0 0
+0500 300.0 0
+0530 330.0 0
+0600 360.0 0
+0700 420.0 0
+0800 480.0 0
+0930 570.0 0
+1000 600.0 0
+1100 660.0 0
+1200 720.0 0
-0300 -180.0 0
-0400 -240.0 0
-0500 -300.0 0
-1100 -660.0 0
-1200 -720.0 0
AHST -600.0 HDT 1
ALASKA -540.0 ALASKA 1
ARIZ -420.0 0
AST -240.0 ADT 1
CST -360.0 CDT 1
EST -300.0 EDT 1
GMT 0.0 0
HKT 480.0 0
HST -600.0 0
INDANA -300.0 0
IST 330 0
*****************************************************************************
CREATE FUNCTION udf_LastSundayOfTheMonth
( @Date datetime )
RETURNS datetime
AS
BEGIN
DECLARE @weekday int
DECLARE @Lastday datetime
DECLARE @number int
DECLARE @day datetime
SELECT @weekday = 0SELECT @Lastday = (DATEADD(day, -1, CAST(STR(MONTH(@Date)+1) + '/' + STR(01) + '/' + STR(YEAR(@Date)) AS DateTime)))
SELECT @number = DATEPART(day, @Lastday)WHILE @weekday <> 1BEGINSELECT @day = (CAST(STR(MONTH(@Date)) + '/' + STR(@number) + '/' + STR(YEAR(@Date)) AS DateTime))
SELECT @weekday = DATEPART(weekday, @day)
SELECT @number = @number - 1ENDRETURN @day
END
GO

**************************************************************

CREATE FUNCTION udf_FirstSundayOfTheMont
h( @Date datetime )
RETURNS datetime
AS
BEGIN
DECLARE @weekday int
DECLARE @day datetime
DECLARE @number int
SELECT @number = 1
SELECT @weekday = 0
WHILE @weekday <> 1
BEGIN
SELECT @day = (CAST(STR(MONTH(@Date)) + '/' + STR(@number) + '/' + STR(YEAR (@Date)) AS DateTime))

SELECT @weekday = DATEPART(weekday, @day)
SELECT @number = @number + 1

END
RETURN
@day


ENDGO



And Finally how to use the function



select dbo.udf_Timezone_Conversion('EST','IST',getdate(),0)


This completes the function ..

Hope this will be helpful for u all

Happppppppy Programming

Tuesday, March 4, 2008

Date Time Functions in SQL

Get Year
Select datepart(Year,getdate())
or
Select Year(getdate())


Get Month
Select datepart(Month,getdate())
or
Select Month(getdate())
Get Day

Select datepart(day,getdate())
or
Select Day(getdate())
Get Week Number
Select Datepart(Wk,getdate())
Get WeekDayName
Select DateName(dw,getdate())
(to get weeknumber DateName(Week,getdate()))
Get hours
select Datepart(hh ,getdate())
Get Minutes
Select Datepart(n ,getdate())
Get Seconds
Select Datepart(ss ,getdate())

Date Time Conversions in SQL

Hi..
Here are some of the Date time Conversion Functions in sql server

Date in MM/DD/YYYY
select convert(varchar,DateColumn,108)

108 represents the format Style of date

Style ID

Style Type

0 or 100 mon dd yyyy hh:miAM (or PM)
101 mm/dd/yy
102 yy.mm.dd
103 dd/mm/yy
104 dd.mm.yy
105 dd-mm-yy
106 dd mon yy
107 Mon dd, yy
108 hh:mm:ss
9 or 109 mon dd yyyy hh:mi:ss:mmmAM (or PM)
110 mm-dd-yy
111 yy/mm/dd
112 yymmdd
13 or 113 dd mon yyyy hh:mm:ss:mmm(24h)
114 hh:mi:ss:mmm(24h)
20 or 120 yyyy-mm-dd hh:mi:ss(24h)
21 or 121 yyyy-mm-dd hh:mi:ss.mmm(24h)
126 yyyy-mm-dd Thh:mm:ss.mmm(no spaces)
130 dd mon yyyy hh:mi:ss:mmmAM
131 dd/mm/yy hh:mi:ss:mmmAM

Thursday, February 28, 2008

VB String Functons

Function Description
InStr
Returns the position of the first occurrence of one string within another. The search begins at the first character of the string

InStrRev Returns the position of the first occurrence of one string within another. The search begins at the last character of the string

LCase Converts a specified string to lowercase

Left Returns a specified number of characters from the left side of a string

Len Returns the number of characters in a string

LTrim Removes spaces on the left side of a string

RTrim Removes spaces on the right side of a string
Trim Removes spaces on both the left and the right side of a string

Mid Returns a specified number of characters from a string Replace Replaces a specified part of a string with another string a specified number of times Right Returns a specified number of characters from the right side of a string
txt="This is a beautiful day!"

document.write(Right(txt,11))Output:utiful day!

Space Returns a string that consists of a specified number of spaces StrComp Compares two strings and returns a value that represents the result of the comparison String Returns a string that contains a repeating character of a specified length StrReverse Reverses a string UCase Converts a specified string to uppercase

Vexed with "Stack overflow at line: 0" Error

Hi ,

Stack overflow at line: 0 Error really troubing me lot form several days..
I am unable to debug it..even..
I dont know how but when i removed Smartnavigation=true from my web config file it started working..

Dont ask me the reasons why??but it worked.

Monday, February 18, 2008

Sending EMails using CDO's

CDO 's are command data objects used to send Emails from SMTP server.
Before using this objects ,we need to configure the SMTP server .
Using CDO objects
Simple EMail
Set myMail=CreateObject("CDO.Message") myMail.Subject=ReportName myMail.From="HarikaWorks" myMail.To="harika.04@Gmail.com" mymail.cc="OtherReceipints@abc.com" myMail.Bcc= "OtherReceipints1@abc.com" myMail.Replyto="admin@abc.com" mymail.send()



Email with attachments
Set myMail=CreateObject("CDO.Message") myMail.Subject=ReportName myMail.From="HarikaWorks" myMail.To="harika.04@Gmail.com" mymail.cc="OtherReceipints@abc.com" myMail.Bcc= "OtherReceipints1@abc.com" myMail.Replyto="admin@abc.com" myMail.AddAttachment "D://Images/image1.gif" mymail.send()


Zip attachments in Email
Set myMail=CreateObject("CDO.Message") myMail.Subject=ReportName myMail.From="HarikaWorks" myMail.To="harika.04@Gmail.com" mymail.cc="OtherReceipints@abc.com" myMail.Bcc= "OtherReceipints1@abc.com" myMail.Replyto="admin@abc.com"
Scurdir=oFs.GetAbsolutePathName("") strcommand="winzip32.exe" Set objShell=CreateObject("WScript.Shell") strpath= "D:\\images\charts\Reports.zip" strdestination= ScurDir & "\" & sFullFileName
strcommand= strcommand & " " & " -a" & " " & strpath & " " & strdestination objShell.Run strCommand, 0, True 'wait! myMail.AddAttachment "D://Images/image1.gif" mymail.send()