Monday, January 30, 2012

tsql function to convert a column value to csv

Option 1:

alter function udf_get_csv_faults (@unit varchar(4),@jobno int)
returns varchar(500)
declare @csv varchar(2048)
(SELECT ',' + s.detail
FROM vw_job_sheet_fault s
where s.js_unitcode=@unit and s.js_jobno=@jobno
FOR XML PATH('')),2,500)
return @csv

Option 2:

create FUNCTION udf_get_csv(@unit varchar(4),@jobno int)
RETURNS varchar(500)
declare @csv varchar(500)
Select @csv = null
SELECT @csv = Coalesce(@csv + ', ', '') +
FROM vw_job_sheet_fault s
where s.js_unitcode=@unit and s.js_jobno=@jobno


Friday, January 20, 2012

code for crystal report

Visual Studio 2005 and Crystal Report XI.

1. Create a Crystal Report as usual in Visual studio 2005 .net
using ODBC connection. In this example "CrystalReport.rpt"

2. Create a aspx web form to display the report. This form will
be a place holder for crystal report.

3. In design mode, drag and drop Crystal Report viewer control and
don't configure anything.

4. Open code behind .aspx.vb

'Declare public variable

Private myReportDocument As ReportDocument

'Note: This is Page Init event not Page Load event

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Dim myConnectionInfo As ConnectionInfo = New ConnectionInfo()

' Note: This is your local ODBC Name NOT SQL SERVER NAME

myConnectionInfo.ServerName = "PUBSODBCNAME"

' If your ODBC connection defaulted to your application database,
' you can remove the following line.

myConnectionInfo.DatabaseName = "Pubs"
myConnectionInfo.UserID = "userid"
myConnectionInfo.Password = "password"

myReportDocument = New ReportDocument()
'If your crystal report is in some other folder, give proper path
' In this example, .rpt file in the same folder as .aspx file
Dim reportPath As String = Server.MapPath("CrystalReport.rpt")
SetDBLogonForReport(myConnectionInfo, myReportDocument)
CrystalReportViewer1.ReportSource = myReportDocument
' You can customize the Crystal Reports toolbar.
' like the following. I have disabled the business object logo
' in the tool bar.

CrystalReportViewer1.HasCrystalLogo = False

End Sub

'This Sub is necessary for Crystal report database connection

Private Sub SetDBLogonForReport(ByVal myConnectionInfo As ConnectionInfo, ByVal myReportDocument As ReportDocument)
Dim myTables As Tables = myReportDocument.Database.Tables
For Each myTable As CrystalDecisions.CrystalReports.Engine.Table In myTables
Dim myTableLogonInfo As TableLogOnInfo = myTable.LogOnInfo
myTableLogonInfo.ConnectionInfo = myConnectionInfo
5. Now run the .aspx page. You will see the report or the crystal report parameter
form. It will not ask you DB login information.

6. dont forget to Imports dll

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports CrystalDecisions.ReportSource