Crystal Report can't connect to database - vb.net

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"

Related

How to take selected data from VB.net drop down list

I am currently practicing VB.net in Visual Studio 2013 and i have been asked to create a drop down list with data from a table which i have down and is working. What i am stuck on is how to pull the selected option from the drop down list and use that to display in a Label Web Control to show on the screen when a button is pressed to confirm the selection, Can anyone Help?
My Code on the Default.aspx.vb file
Dim dsData1 As New DataSet
dsData1 = tableData()
DDList.DataSource = dsData1
DDList.DataValueField = "code"
DDList.DataTextField = "description"
DDList.DataBind()
DDList.Items.Insert(0, New ListItem(String.Empty, String.Empty))
DDList.SelectedIndex = 0
My code in my function.vb file;
Public Shared Function tableData() As DataSet
Dim oraConnect As New OracleConnection
oraConnect.ConnectionString = ConfigurationManager.ConnectionStrings("smart_dev").ConnectionString
Dim oraCommand As New OracleCommand
oraCommand.Connection = oraConnect
oraCommand.CommandType = Data.CommandType.Text
Dim lsSQL As String = ""
lsSQL = "SELECT code, description FROM ref_code WHERE domain = 'SPECIALTY'"
oraCommand.CommandText = lsSQL
Dim da As New OracleDataAdapter(oraCommand)
Dim ds As New DataSet
da.Fill(ds)
Return ds
End Function
So basiaclly i need help with which one of these pieces of code do i edit and with what code if i open the page in a browser select an option from the dropdown and it displays on the screen.
Thank you
If i have confused anyone i apologise:)
I think you are looking for
DropDownList.SelectedItem.Text
If it's a web form the .SelectedItem.Text will only be available after the view state has been loaded, If you need to get around that you can do the following:
dim selectedText as String = page.request(DDList.UniqueId)

Parameters are not working passing to a crystal reports from vb.net

I am using this code to pass parameters to my crystal report, but in run time crystal report is showing text boxes to input parameters. please help me to solve this
Dim rpt As New RPT_Maintenance
rpt.SetDataSource(maintenanceDetailsTable)
rpt.SetParameterValue("datefrom", dtpDateFrom.Text)
rpt.SetParameterValue("dateto", DtpDateTo.Text)
rpt.SetParameterValue("cat", "All Vehicles")
FRM_ReportViewer.CrystelReportViewer.ReportSource = rpt
FRM_ReportViewer.ShowDialog()
FRM_ReportViewer.Dispose()
If the parameter box keep popping up, what you can do is on the page load of your crystal report viewer form, paste this:
Dim param1Fields As New ParameterFields
Dim param1Field As New ParameterField
Dim param1Range As New ParameterDiscreteValue
param1Field.ParameterFieldName = "TeamRoster"
param1Range.Value = Roster.cmbTeams.Text
param1Field.CurrentValues.Add(param1Range)
param1Fields.Add(param1Field)
CrystalReportViewer1.ParameterFieldInfo = param1Fields
You have to set Datasource to report before settings parameter like.
reportClass.SetDataSource(source);
reportClass.SetParameterValue("txtCompanyName", companyName);
viewer.SetReportSource(reportClass);

Trigger Print of ReportViewer VB

I am using VB.net and Winforms reportviewer for showing the reports generated using SQL Server Reporting Service.
I want to trigger the print of the reportviewer from code behind. Please help me in doing the same.
Dim usern = WindowsIdentity.GetCurrent().Name.ToString()
rptViewer.ProcessingMode = ProcessingMode.Remote
rptViewer.ServerReport.ReportServerUrl = New Uri("http://myserver/Reportserver")
rptViewer.ServerReport.ReportPath = "/Management Reports/InvoicReport"
Dim parm As ReportParameter
parm = New ReportParameter("parInvoiceID", InvoiceID)
rptViewer.ServerReport.SetParameters(parm)
rptViewer.ServerReport.Refresh()
Me.rptViewer.RefreshReport()
Thanks in advance
on RenderingComplete event of rptViewer write below code
rptViewer.PrintDialog()

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

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

Crystal Reports RecordSelectionFormula does not work

It's my code:
CrystalReportViewer1.Zoom(75)
Dim rpt As New CrystalReport1
rpt.RecordSelectionFormula = "{members.id} ='3232'"
CrystalReportViewer1.ReportSource = rpt
CrystalReportViewer1.Refresh()
It shows all records, I don't know why
Dim CrReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
CrReport = New CrystalDecisions.CrystalReports.Engine.ReportDocument()
CrReport.Load(Application.StartupPath & "\CrystalReport1.rpt")
CrReport.SetDataSource("HERE YOUR DATASET USED IN DA DESIGN OF CRYSTALREPORT1.rpt")
CrystalReportViewer1.ReportSource = CrReport
CrReport.RecordSelectionFormula = "{members.id} ='3232'"
Your CRYSTALREPORT1.rpt must be located at \\BIN\DEBUG OF your appath and it must be almost created before. Like an object by the designer from Visual Studio... .. > ADD NEW ITEM > CR
You must create a CrystalReportDocument in Form report (desing view), and after that, the Visual Studio will display a window where you can choose a Class for CrystalReportDocument. Choose the Class related with the report.rpt, and then in the print event add this:
crystalReportDocument.Load(#"reports\report.rpt");
crystalReportDocument.RecordSelectionFormula = "{viewTable.IdTable}=1";
crystalReportDocument.PrintToPrinter(1, false, 0, 0);
viewTable is a view element in Data Base.
Many people are used to DATASET for all, but in many cases it's ridiculous.