How to pass parameters to crystal report from vb.net code - vb.net

I have created a crystal report (cross tab). I'm not using any dataset, instead I used the wizard in crystal report to call an procedure from my Database schema
(Provider given is Microsoft OLEDB provider for oracle, after which I gave my DB credentials(i.e. schema, username, password) and selected the procedure and selected the columns I wanted to display in the report).
There are 5 parameters that I need to pass it from the front end to generate the report. While viewing the crystal report preview, by giving parameters, the report works fine.
Now i want to pass these 5 parameters from the front end(vb.net) to show the report in the CrystalReportViewer. Please suggest the code to write in aspx.vb file.
(PS:- I did go through other forums and found out some code, but all of them were giving some or the other error, so am posting one so that i can get the code specific to my requirement).
Thanks in advance..

I have gotten the report to work...
I wrote the code below:
Dim RptDocument As New ReportDocument
RptDocument.Load(Server.MapPath("rpt\Report.rpt"))
RptDocument.SetParameterValue("param1", Session("param1"))
RptDocument.SetParameterValue("param2", ddlparam2.SelectedValue)
RptDocument.SetParameterValue("param3", param3.text)
RptDocument.SetParameterValue("param4", param4.text)
RptDocument.SetParameterValue("param5", param5.text)
'Set login info
Dim myLogin As CrystalDecisions.Shared.TableLogOnInfo
Dim myTable As Table
For Each myTable In RptDocument.Database.Tables
myLogin = myTable.LogOnInfo
myLogin.ConnectionInfo.ServerName = "server name"
myLogin.ConnectionInfo.DatabaseName = ""
myLogin.ConnectionInfo.UserID = "userid"
myLogin.ConnectionInfo.Password = "pwd"
myTable.ApplyLogOnInfo(myLogin)
myTable.Location = myTable.Location
CrystalReportViewer1.ReportSource = RptDocument
Created a System DNS and had to add Oracle.DataAccess.dll to reference and a class file (with functions same as that in connectooracle.vb class file but with different name), also set up a connection in global.asax to refer to that class connection and using
Imports Oracle.DataAccess.Client instead of Imports System.Data.OracleClient (to avoid ambiguity)...
This somehow made it work and there might be some other solution for that..:)
(For ref:- Adding myLogin.ConnectionInfo.IntegratedSecurity = True gave me this error--
Logon failed. Error in File C:\DOCUME~1\Username\LOCALS~1\Temp\Report {1AG3DD86-141D-43YA-B6A2-AEDF3459AE49}.rpt: Unable to connect: incorrect log on parameters.)

This works for me and I'm using Visual Studio 2008 for this one since VS2010 doesn't have crystal engine for the reference.
First, make sure you have imported these two:
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Now, on my part I was using the odbc because as what I have noticed crystal report works fine with this and since we are working with odbc. So I did not include the login property on the report in my code. On the report just choose odbc connection.
Private Sub ReportViewer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cryRpt As New ReportDocument
Dim str1 As String
Try
str1 = Title.str1
str2 =Title.str2
cryRpt.Load("c:\Program Files\Report\" & str2 & "")
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterValues As New ParameterValues
Dim crParameterDiscreteValue As New ParameterDiscreteValue
crParameterDiscreteValue.Value = strStore
crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields
crParameterFieldDefinition = crParameterFieldDefinitions.Item("Store")
crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterValues.Clear()
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
rptviewer.Cursor = Cursors.AppStarting
rptviewer.ReportSource = cryRpt
rptviewer.Refresh()
rptviewer.Cursor = Cursors.Default
Catch ex As Exception
MsgBox(ex.Message)
Me.Close()
ReportInterface.Show()
End Try
End Sub
I have a class that will get the data inputted by the user. But this will not work using stored procedure parameters.
please mark as accepted if it works for you
Thank You

Related

Crystal Reports 2013 Doesn't apply filter

I have a project developed in VS2008 and i'm migrating to vs2013. Everythings ok but i've noticed that all of my crystal reports doesn't apply the filters using the parameters passed.
For instance, this is the selection in the crystal report:
And i'm passing parameters this way:
Cursor = Cursors.WaitCursor
Dim CR As New ReportDocument
Dim CRParams As New ParameterFields
Dim CRParam As New ParameterField
CR.Load("infVolumenVenta2.rpt", OpenReportMethod.OpenReportByDefault)
' Creamos la conexion
Dim CRTableLogoInfos As New TableLogOnInfos
Dim CRTableLogoInfo As New TableLogOnInfo
Dim CRConnectionInfo As New ConnectionInfo
CR.SetDatabaseLogon("<user>", "<password>")
CR.ParameterFields("desde").CurrentValues.AddValue(CDate(txtDesde.Text))
CR.ParameterFields("hasta").CurrentValues.AddValue(CDate(txtHasta.Text))
CR.ParameterFields("idTienda").CurrentValues.AddValue(cmbTiendas.SelectedValue)
The Report shows:
The three fields at top are the value of the parameters i've passed before. But doesn't matter the dates i pass. Always shows the same report, the filter is not applying.
The same code and report in VS2008 works fine.
Do i have to change something to work with CR in VS2013?

Opening pdf from Crystal Reports Viewer

Thank you for viewing my question. I am building a project in Visual Studio 2010 using vb and .net. I have a Crystal Reports report that I'm trying to have auto export and open in PDF with a button click. Right now I am using Crystal Reports Viewer in my project which opens the report fine; However, I would like to have it only open in a pdf format. Is there a way to do this?
Note: I'm not here hunting for code. I'm wanting to learn, so if you could just guide me in the right direction, that will be great (if you don't want to provide code)!
Thank you for the help.
Josh
I'm using code from http://www.codeproject.com/Articles/14549/Crystal-Reports-To-PDF-converter-Without-Crystal-R
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class clsCrystalToPDFConverter
Dim ConInfo As New CrystalDecisions.Shared.TableLogOnInfo
Dim oRDoc As New ReportDocument
Dim expo As New ExportOptions
Dim sRecSelFormula As String
Dim oDfDopt As New DiskFileDestinationOptions
Dim strCrystalReportFilePath As String
Dim strPdfFileDestinationPath As String
Public Function SetCrystalReportFilePath(ByVal CrystalReportFileNameFullPath As String)
strCrystalReportFilePath = CrystalReportFileNameFullPath
End Function
Public Function SetPdfDestinationFilePath(ByVal pdfFileNameFullPath As String)
strPdfFileDestinationPath = pdfFileNameFullPath
End Function
Public Function SetRecordSelectionFormula(ByVal recSelFormula As String)
sRecSelFormula = recSelFormula
End Function
Public Function Transfer()
oRDoc.Load(strCrystalReportFilePath) 'loads the crystalreports in to the memory
oRDoc.RecordSelectionFormula = sRecSelFormula 'used if u want pass the query to u r crystal form
oDfDopt.DiskFileName = strPdfFileDestinationPath 'path of file where u want to locate ur PDF
expo = oRDoc.ExportOptions
expo.ExportDestinationType = ExportDestinationType.DiskFile
expo.ExportFormatType = ExportFormatType.PortableDocFormat
expo.DestinationOptions = oDfDopt
oRDoc.SetDatabaseLogon("PaySquare", "paysquare") 'login for your DataBase
oRDoc.Export()
End Function
End Class
You'll need to set the variables to the specifics of your project obviously. These are more than likely the classes and methods you'll be wanting to use however. This should allow you to take your crystal reports viewer file and turn it into something opened by PDF
This works for me.
Dim orpt As CrystalDecisions.CrystalReports.Engine.ReportDocument
orpt = DirectCast(crvInvoice.ReportSource, CrystalDecisions.CrystalReports.Engine.ReportDocument)
orpt.ExportToDisk(ExportFormatType.PortableDocFormat, "PdfFileName.pdf")

VB.net 2005 Type mismatch with datatable to crystal report

on my .xsd file where I create the datatable for use in my Crystal Report (buit in with Visual Studio 2005), I have included a column (which is datatatype = 'Byte') that is supposed to hold the image for my crystal report.
However, there is always an error thrown at where I've typed COMMENT1, saying that the file is being used by another process. The file I am accessing is a mapped drive to our server.
when I try to ignore the error and press on continue, the report does show up..but without the image it was supposed to be with.
below is my complete syntax
Dim x As New Reporter
x.MdiParent = Magulang
Dim dt As New DataTable
dt.Columns.Add("EmpNo")
dt.Columns.Add("FullName")
dt.Columns.Add("Project")
dt.Columns.Add("SSS")
dt.Columns.Add("TIN")
dt.Columns.Add("EmergencyPerson")
dt.Columns.Add("EmergencyAddress")
dt.Columns.Add("EmergencyContact")
dt.Columns.Add("PicNya", System.Type.[GetType]("System.Byte[]"))
Dim middleinitial As String = MiddleName.Text.Substring(0, 1)
Dim fs As FileStream
Dim br As BinaryReader
If File.Exists(PicPath.Text) Then
'COMMENT1 : there is error here, saying that file is used by another process
fs = New FileStream(PicPath.Text, FileMode.Open)
Else
fs = New FileStream(AppDomain.CurrentDomain.BaseDirectory + "NoPhoto.jpg", FileMode.Open)
End If
br = New BinaryReader(fs)
Dim imgbyte As Byte() = New Byte(fs.Length) {}
imgbyte = br.ReadBytes(Convert.ToInt32((fs.Length)))
MsgBox(imgbyte.ToString)
dt.Rows.Add(EmpID.Text, FirstName.Text & " " & middleinitial & ". " & LastName.Text, Project.Text, SSS.Text, TIN.Text, EmergencyContact.Text, "", EmergencyContactNo.Text, imgbyte)
br.Close()
fs.Close()
Dim rptdoc As CrystalDecisions.CrystalReports.Engine.ReportDocument
rptdoc = New HRISID1
rptdoc.SetDataSource(dt)
x.CrystalReportViewer1.ReportSource = rptdoc
ProgressForm.Hide()
x.Show()
What can I do to resolve COMMENT1? if resolved, will the image finally output in my crystal report? Am I on the right track, if I wanted to output images on my computer?
For now, we have resolved to instead pass each Data into a seperate form, then use the PRINTFORM object included with the VB Powerpacks. This is to make sure the image is also printed.
I can't help but feel this isn't the proper solution though, so if anyone can still contribute it's very much welcome.

Crystal Report can't connect to database

I'm creating a program in VB.net with Visual Studio and some forms use Crystal Reports to show PDF reports, but i'm having problems with database connection. VB.net code can access the databse without problems, but when a form shows a report it asks me for username and password and if i write them it fails to connect. The application and the reports share the same database and i use the same data to connect, but Crystal Reports fails. Can you help me?
Here's a snippet that I have that works (in C#, but should give you an idea of how I did it):
CrystalReportSource CrystalReportSource1 = new CrystalReportSource();
CrystalReportViewer CrystalReportViewer1 = new CrystalReportViewer();
CrystalReportViewer1.ReportSource = CrystalReportSource1;
CrystalReportViewer1.EnableParameterPrompt = false;
CrystalReportSource1.Report.FileName = "Report3.rpt";
CrystalReportSource1.EnableCaching = false;
CrystalReportSource1.ReportDocument.SetParameterValue(0, ponumber);
CrystalReportSource1.ReportDocument.SetParameterValue(1, receiptno);
TableLogOnInfo logOnInfo = new TableLogOnInfo();
logOnInfo.ConnectionInfo.ServerName = ConfigurationManager.AppSettings["WarehouseReportServerName"];
logOnInfo.ConnectionInfo.DatabaseName = ConfigurationManager.AppSettings["WarehouseReportDatabaseName"];
logOnInfo.ConnectionInfo.UserID = ConfigurationManager.AppSettings["WarehouseReportUserID"];
logOnInfo.ConnectionInfo.Password = ConfigurationManager.AppSettings["WarehouseReportPassword"];
TableLogOnInfos infos = new TableLogOnInfos();
infos.Add(logOnInfo);
CrystalReportViewer1.LogOnInfo = infos;
maindiv.Controls.Add(CrystalReportSource1);
maindiv.Controls.Add(CrystalReportViewer1);
CrystalReportViewer1.DataBind();
Although you said that you are using the designers, I 'll post some code that is working for me, maybe it will help you:
Dim cryRpt As New ReportDocument
Dim strReportPath As String = 'The Path of the rpt file
cryRpt.Load(strReportPath)
cryRpt.SetDataSource(Me.crData) 'crData is a datatable with data for the report
crvReport.ReportSource = cryRpt 'crvReport is the CrystalReportViewer in my form
Check if all fields exist in the data when you set in "SetDataSource"

SharePoint: GetListItems soapserverException being thrown because of Query

I am using designing a Windows Form application using VB.net. I trying to have the application return the number of rows in a specific SharePoint List. Everything works perfectly when I I remove the ndQuery.InnerXml code; however, I want to filter the list before I get the count. The two columns I want to filter are "Assigned Employee" and "status." I looked at many different posts here on Stack(SharePoint SoapServerException calling GetListItems web service), but my Exception is relating to the Query. The detail of the soapserverException is: "One or more field types are not installed properly. Go to the list settings page to delete these fields: 0x81020014."
I tried going to the relationship page, but I could not browse to it:
(url)/Relationships%20List/allitems.aspx
Can any one see a problem with the Query code?
Imports System
Imports System.IO
Imports System.Net
Imports System.Xml
Imports <xmlns="rs">
Public Class Form1
Dim i As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim listService As New getListItems.Lists
listService.Credentials = CredentialCache.DefaultCredentials
listService.Url = "http://(servername)/_vti_bin/Lists.asmx"
Dim xmlDoc = New System.Xml.XmlDocument()
Dim ndQuery As XmlNode =
xmlDoc.CreateNode(XmlNodeType.Element, "Query", "")
Dim ndViewFields As XmlNode =
xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "")
Dim ndQueryOptions As XmlNode =
xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "")
ndQueryOptions.InnerXml =
"<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>"
ndViewFields.InnerXml = "<FieldRef Name='Assigned Employee'/><FieldRef Name='Status'/>"
ndQuery.InnerXml = "<Where><And><Contains><FieldRef Name ='Assigned Employee'/><Value Type='Text'>Engineer</Value></Contains><Contains><FieldRef Name='Status'/><Value Type='Text'>New</Value></Contains></And></Where>"
Try
Dim ndListItems As XmlNode =
listService.GetListItems("Requests", Nothing, ndQuery, _
ndViewFields, Nothing, ndQueryOptions, Nothing)
Dim n1 As XmlNode = ndListItems.Item("rs:data")
Dim a As String = n1.Attributes("ItemCount").InnerText
'Attempted For each loop, but not needed:
'Dim listItemCount As String
'Dim innerXML = New System.Xml.XmlDocument
'innerXML.LoadXml(ndListItems.InnerXml)
'Dim rows As XmlNodeList = innerXML.GetElementsByTagName("rs:data")
'For Each (XmlNode Attribute In rows)
'Next
Label1.Text = a
Catch ex As System.Web.Services.Protocols.SoapException
Label1.Text = ("Message:" + ControlChars.Lf + ex.Message +
ControlChars.Lf +
"Detail:" + ControlChars.Lf + ex.Detail.InnerText +
ControlChars.Lf +
"StackTrace:" + ControlChars.Lf + ex.StackTrace)
End Try
End Sub
End Class
You may need to replace the spaces in the field names with _x0020_ e.g.
<FieldRef Name='Assigned_x0020_Employee'/>
The Name attribute takes the field's internal name so double check that's what you're using.
When I have encountered the fun "one or more field types are not installed properly" error in the past it has usually been due to my CAML WHERE criteria indicating a field is of a certain type when it is not (e.g. I indicate Value type="Text" when it is actually a lookup).
If you go and get CAML Query Builder (free) from U2U, you can point it at your site (using the built in web services of SharePoint) and build your CAML query using their drag and drop designer. Once you have the query working there just click on the Edit tab and it will show you the exact CAML that SharePoint expects. My guess is you will find a field type incorrectly set. Whole process should take about 10 minutes after you install it.
I discovered the problem when breaking apart the query statement into two parts:
'ndQuery.InnerXml = "<Where><Eq><FieldRef Name ='Assigned_x0020_Employee'/><Value Type='Text'>Engineer</Value></Eq></Where>"
'ndQuery.InnerXml = "<Where><Eq><FieldRef Name ='Status'/><Value Type='Text'>New</Value></Eq></Where>"
I figured out that although one of the Columns in the SP list was named "Assigned Employee," the actual FieldRef Name was just employee. When I modified the code to include that, the error went away. I was spending all my time changing the Value Type, instead of looking at the FieldRef Name
Final Conclusion:
The "one or more field types are not installed properly" error not only gets returned if the "Value Type" is incorrect, but also when the "FieldRef Name" contains the wrong label.
Final working code line:
ndQuery.InnerXml = "<Where><And><Eq><FieldRef Name ='Employee'/><Value Type='Text'>Engineer</Value></Eq><Eq><FieldRef Name='Request_x0020_Status'/><Value Type ='Text'>New</Value></Eq></And></Where>"