Challenging Crystal Reports / .NET Windows application question
-
I have a VB.NET VS2008 windows form application where I have a crystalviewer form setup to print a few reports out from the menu off of the main data entry form. The crystal reports are already written and stored in a folder on one of our servers (For this example lets call it \\MyServer\ReportFolder\). The crystal report uses a DSN to know where to find the data (For this example lets call it MyCrystalDSN). The code on the menu fires off the report as shown below: Dim frmCV As New frmCrystalViewer frmCV.CrystalReportViewer1.ReportSource = "\\MyServer\ReportFolder\Reportname.rpt" frmCV.WindowState = FormWindowState.Maximized frmCV.ShowDialog() Ok, everything works great for now, but of course it never fails that when you develop at your desk with all the rights available as a developer it all works and when you deploy the app, everything never works as planned. When I publish the project for use by the client, I have to setup a DSN so the Crystal Reports will work. Can I do this on the fly with code? For example, my crystal reports are using the MyCrystalDSN, and I don't want to have to remember to setup a DSN for everyone's computer I deploy the application on. Can I code the DSN information to use in the code prior to firing off the report? A coworker gave me the following code from an article that I believe was written in 2001 for VB6. Is anyone doing this in .NET? Here is that code: Public Sub RegisterDatabaseSample() 'Create a DSN called TEST on a server called ThatServer DBEngine.RegisterDatabase "TEST", "SQL Server", True, _ "Database=pubs" & _ vbCr & "Description=Sample DSN" & _ vbCr & "Server=ThatServer" End Sub Any help is appreciated :)
Lost in the vast sea of .NET
-
I have a VB.NET VS2008 windows form application where I have a crystalviewer form setup to print a few reports out from the menu off of the main data entry form. The crystal reports are already written and stored in a folder on one of our servers (For this example lets call it \\MyServer\ReportFolder\). The crystal report uses a DSN to know where to find the data (For this example lets call it MyCrystalDSN). The code on the menu fires off the report as shown below: Dim frmCV As New frmCrystalViewer frmCV.CrystalReportViewer1.ReportSource = "\\MyServer\ReportFolder\Reportname.rpt" frmCV.WindowState = FormWindowState.Maximized frmCV.ShowDialog() Ok, everything works great for now, but of course it never fails that when you develop at your desk with all the rights available as a developer it all works and when you deploy the app, everything never works as planned. When I publish the project for use by the client, I have to setup a DSN so the Crystal Reports will work. Can I do this on the fly with code? For example, my crystal reports are using the MyCrystalDSN, and I don't want to have to remember to setup a DSN for everyone's computer I deploy the application on. Can I code the DSN information to use in the code prior to firing off the report? A coworker gave me the following code from an article that I believe was written in 2001 for VB6. Is anyone doing this in .NET? Here is that code: Public Sub RegisterDatabaseSample() 'Create a DSN called TEST on a server called ThatServer DBEngine.RegisterDatabase "TEST", "SQL Server", True, _ "Database=pubs" & _ vbCr & "Description=Sample DSN" & _ vbCr & "Server=ThatServer" End Sub Any help is appreciated :)
Lost in the vast sea of .NET
I've been searching for the solution on the web and found a few different suggestions which I combined and came up with the answer to my question. Here is the code:
Public Declare Auto Function SQLConfigDataSource Lib "ODBCCP32.DLL" _ (ByVal hwndParent As Integer, ByVal fRequest As Integer, _ ByVal lpszDriver As String, ByVal lpszAttributes As String) As Integer Private Const ODBC_ADD_SYS_DSN As Integer = 4 Private Sub Create_DSN_For_Crystal_Reports() Try Dim attributes As New System.Text.StringBuilder() Dim returnCode As Integer attributes.Append("DSN=MyCrystalDSN") attributes.Append(Chr(0)) attributes.Append("Server=MyDBServer") attributes.Append(Chr(0)) attributes.Append("Description=DSN added via code from My VB App") attributes.Append(Chr(0)) attributes.Append("Database=MyDatabase") attributes.Append(Chr(0)) attributes.Append("AnsiNPW=Yes") attributes.Append(Chr(0)) attributes.Append("QuotedId=Yes") attributes.Append(Chr(0)) attributes.Append("Trusted_Connection=Yes") attributes.Append(Chr(0)) attributes.Append(Chr(0)) returnCode = SQLConfigDataSource(0&;, ODBC_ADD_SYS_DSN, "SQL Server", attributes.ToString) If returnCode <> 1 Then Throw New Exception("DSN could not be setup to allow Crystal Reports access") End If Catch ex as Exception msgbox(ex.message) End Try End Sub
Hopefully posting the answer will help someone else later who has the same problem. :-DLost in the vast sea of .NET
modified on Thursday, March 6, 2008 8:45 AM
-
I have a VB.NET VS2008 windows form application where I have a crystalviewer form setup to print a few reports out from the menu off of the main data entry form. The crystal reports are already written and stored in a folder on one of our servers (For this example lets call it \\MyServer\ReportFolder\). The crystal report uses a DSN to know where to find the data (For this example lets call it MyCrystalDSN). The code on the menu fires off the report as shown below: Dim frmCV As New frmCrystalViewer frmCV.CrystalReportViewer1.ReportSource = "\\MyServer\ReportFolder\Reportname.rpt" frmCV.WindowState = FormWindowState.Maximized frmCV.ShowDialog() Ok, everything works great for now, but of course it never fails that when you develop at your desk with all the rights available as a developer it all works and when you deploy the app, everything never works as planned. When I publish the project for use by the client, I have to setup a DSN so the Crystal Reports will work. Can I do this on the fly with code? For example, my crystal reports are using the MyCrystalDSN, and I don't want to have to remember to setup a DSN for everyone's computer I deploy the application on. Can I code the DSN information to use in the code prior to firing off the report? A coworker gave me the following code from an article that I believe was written in 2001 for VB6. Is anyone doing this in .NET? Here is that code: Public Sub RegisterDatabaseSample() 'Create a DSN called TEST on a server called ThatServer DBEngine.RegisterDatabase "TEST", "SQL Server", True, _ "Database=pubs" & _ vbCr & "Description=Sample DSN" & _ vbCr & "Server=ThatServer" End Sub Any help is appreciated :)
Lost in the vast sea of .NET
You can create a typed dataset, get the data yourself via ADO.net and then supply the report with the loaded type dataset and then you don't need a DSN for Crystal. A developer I work with has used this technique in the past. I haven't tried it yet. http://support.microsoft.com/kb/320714 http://www.gridviewguy.com/ArticleDetails.aspx?articleID=201\_Creating\_Crystal\_Reports\_Using\_Typed\_DataSet
-
You can create a typed dataset, get the data yourself via ADO.net and then supply the report with the loaded type dataset and then you don't need a DSN for Crystal. A developer I work with has used this technique in the past. I haven't tried it yet. http://support.microsoft.com/kb/320714 http://www.gridviewguy.com/ArticleDetails.aspx?articleID=201\_Creating\_Crystal\_Reports\_Using\_Typed\_DataSet
Thanks for the feedback. I'm actually using the technique you suggested in several other projects and yes it works great. With the project I'm currently working on, they want me to fire off already created Crystal Reports from my VB application which uses a preset DSN. Thanks again for you response! :-D
Lost in the vast sea of .NET
-
Thanks for the feedback. I'm actually using the technique you suggested in several other projects and yes it works great. With the project I'm currently working on, they want me to fire off already created Crystal Reports from my VB application which uses a preset DSN. Thanks again for you response! :-D
Lost in the vast sea of .NET
Oh I see - yeah, that's too bad - that's our current situation too - forced to use the DSN way due to the reports being developed that way. I'm too lazy to look into it, but perhaps with the Developer edition of CR someone could modify the data source part on the fly and convert it to accept data through a typed set instead of DSN's.