ask Logon info again - vb.net

I created a report by using crystal report. I used ado.net connection for this. I created a dataset and joined report.
In coding veiw
Dim Rpt As New ReportDocument
Dim ClientAdp As New ClientTableAdapters.ClientsTableAdapter
Dim ClientDt As New Client.ClientsDataTable
ClientAdp.Connection = New SqlConnection(ConfigurationManager.ConnectionStrings("CurrentConnection").ConnectionString)
ClientAdp.Fill(ClientDt)
Rpt.Load(Server.MapPath(ADONET_REPORT_FILE))
Rpt.SetDataSource(CType(ClientDt, DataTable))
Rpt.SetDatabaseLogon("123", "123")
ReportViewer.ReportSource = Rpt
This is OK in first load time. And when I clicks print button in report view, the report view ask logon information. How can I fit this

I think you need to set the TableLogOnInfo for each table within your report not just the Report.
Dim logOnInfo As TableLogOnInfo
For Each tbCurrent As CrystalDecisions.CrystalReports.Engine.Table In Rpt.Database.Tables
logOnInfo = tbCurrent.LogOnInfo
logOnInfo.ConnectionInfo.DatabaseName = "MyDatabaseName"
logOnInfo.ConnectionInfo.UserID = "UserId"
logOnInfo.ConnectionInfo.Password = "secretpassword"
logOnInfo.ConnectionInfo.ServerName = "SQLServer"
logOnInfo.ConnectionInfo.Type = ConnectionInfoType.SQL
tbCurrent.ApplyLogOnInfo(logOnInfo)
Next
I have a blog post here which details this in full (It's written in C# but the same principles apply for VB.NET)

Related

vb.net .VerifyDatabase for Crystal Reports using a password restricted Access DB

As the question title suggests, my vb.net project that uses Crystal Reports 9 and a password restricted MS-Access 97 database.
When loading reports, the Load method calls cReport.VerifyDatabase() which has previously been working fine without a password, but since adding one, it isn't working, and gives me the following error
Log on failed.
at CrystalDecisions.CrystalReports.Engine.ReportDocument.VerifyDatabase()
What do I need to add/change to this code for it to log on successfully?
Private Sub frmReportViewer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
setFormSizes(Me, con)
Me.Location = New Point((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2), 10)
Me.Text = "Report Viewer - (" & Replace(cReport.FileName, "rassdk://", "") & ")"
cReport.VerifyDatabase()
cReport.Refresh()
crViewer.ReportSource = cReport
crViewer.ToolPanelView = CrystalDecisions.Windows.Forms.ToolPanelViewType.None
crViewer.Zoom(87)
Catch ex As Exception
errorLog(ex)
End Try
End Sub
EDIT
The data can be viewed when opening the report file in Crystal Reports itself, but this is because I can manually enter the password.
According to this documentation : 1.4.4.1.2
Use this method to log database with your report before verify the database :
Private Function SubreportLogon_RD(ByVal TestReport As ReportDocument) As ReportDocument
Dim dataSourceConnections As DataSourceConnections = TestReport.DataSourceConnections
Dim connectInfo As IConnectionInfo = dataSourceConnections(0)
connectInfo.SetConnection("ServerName", "DatabaseName", "UserName", "Password")
Dim TestSubreport As ReportDocument = TestReport.Subreports("Subreport1")
Dim subreportDataSourceConnections As DataSourceConnections = TestSubreport.DataSourceConnections
Dim connectInfo As IConnectionInfo = subreportDataSourceConnections(0)
connectInfo.SetLogon("UserName", "Password")
End Function
As per MSDN
Dim logOnInfo As New TableLogOnInfo()
Dim i As Integer
For i = 0 To cReport.Database.Tables.Count - 1
logOnInfo.ConnectionInfo.DatabaseName = "Database_Name"
logOnInfo.ConnectionInfo.Password = "Password"
cReport.Database.Tables.Item(i).ApplyLogOnInfo(logOnInfo)
Next i

Prompting Database Login when loading crystal reports

I've got a problem every time i load my report on crystal. Always asking for database login but when i enter my password(which is the password of my connection) it always says that login failed.
This is my code:
Dim _ReportLogonInfos As New TableLogOnInfos
Dim _ReportLogonInfo As New TableLogOnInfo
Dim _ReportConInfo As New ConnectionInfo
Dim _Tables As Tables
Dim _Table As Table
With _ReportConInfo
.ServerName = "localhost"
.DatabaseName = "database"
.UserID = "root"
.Password = "Qwerty123"
End With
Dim _Report As New rptPrntIss
_Tables = _Report.Database.Tables
For Each _Table In _Tables
_ReportLogonInfo = _Table.LogOnInfo
_ReportLogonInfo.ConnectionInfo = _ReportConInfo
_Table.ApplyLogOnInfo(_ReportLogonInfo)
Next
CrystalReportViewer1.ReportSource = _Report
In addition to what you set, I also set the following (sorry this is in C# not VB.NET - hopefully you can translate):
1
//SET DATASOURCE FOR EACH SUBREPORT IN REPORT
foreach (CrystalDecisions.CrystalReports.Engine.Section section in CrystalReportSource2.ReportDocument.ReportDefinition.Sections)
{
// In each section we need to loop through all the reporting objects
foreach (CrystalDecisions.CrystalReports.Engine.ReportObject reportObject in section.ReportObjects)
{
if (reportObject.Kind == ReportObjectKind.SubreportObject)
{
SubreportObject subReport = (SubreportObject)reportObject;
ReportDocument subDocument = subReport.OpenSubreport(subReport.SubreportName);
foreach (CrystalDecisions.CrystalReports.Engine.Table table in subDocument.Database.Tables)
{
// Cache the logon info block
TableLogOnInfo logOnInfo = table.LogOnInfo;
// Set the connection
logOnInfo.ConnectionInfo = crConnectionInfo;
// Apply the connection to the table!
table.ApplyLogOnInfo(logOnInfo);
}
}
}
}
2
if (CrystalReportSource2.ReportDocument.DataSourceConnections.Count > 0)
CrystalReportSource2.ReportDocument.DataSourceConnections[0].SetConnection(server, db, crystalUser, pwd);
3
CrystalReportSource2.ReportDocument.SetDatabaseLogon(crystalUser, pwd, server, db);

change crystal reports ADO.NET(XML) dynamically

I am developing a project using VB2008 and Crystal Reports 11.5.
I have added new crystal report to the project and it is working fine with the folowing code.
Dim ds As New DataSet
mysqlDataAdapter1 = New MySqlDataAdapter("SELECT * from client_reg where c_qid='" & TextBox2.Text & "'", SQLConnection)
mysqlDataAdapter1.Fill(ds, "client_reg")
ds.WriteXml(CurDir() + "\work_order.xml", XmlWriteMode.WriteSchema)
Dim cryRpt As New work_order '// this is my rpt file in project
rpt_view.CrystalReportViewer1.ReportSource = cryRpt '// rpt_view is the form with empty report viewer
rpt_view.CrystalReportViewer1.Refresh()
rpt_view.CrystalReportViewer1.RefreshReport()
rpt_view.Show()
Now I want to change the XML file location dynamically as follows;
ds.WriteXml("C:\new\bin\work_order.xml", XmlWriteMode.WriteSchema)
How can I change the RPT datasource to new path???
Thank you,
Sameera
Try Something like :
Create Property called:
Public Property ds() As DataSet
Get
Return ViewState("ds")
End Get
Set(ByVal value As DataSet)
ViewState("ds") = value
End Set
End Property
Then set report datasource to be the dataset
Public Sub BindReport(ByVal Id As String)
ds.WriteXml("C:\new\bin\work_order.xml", XmlWriteMode.WriteSchema)
cryRpt.SetDataSource(ds)
rpt_view.ReportSource = cryRpt
If rblFormat.SelectedValue = 1 Then
cryRpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, True, "ExportedReport")
Else
cryRpt.ExportToHttpResponse(ExportFormatType.WordForWindows, Response, True, "ExportedReport")
End If
Call This method at the button event

pass parameters on crystal report , connect access with code , vb.net

I want to search clients that got to shop between two dates.
The code I use seems alright and it doesn't return any error but the CR returns everything - It does not use the date search function. The CR I use is tested with a simple program that open CR and works fine.
database is ms access and code is in visual studio 2005
The second issue I have is that despite i use the connection info part it still ask me for a code
PS. sorry for my bad formatting , i am new in stackoverflow and i find it hard to make a post
thank you!
-Agisilaos
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class Form1
Dim crConnectionInfo As New ConnectionInfo()
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim cryRpt As New ReportDocument
cryRpt = New CrystalDecisions.CrystalReports.Engine.ReportDocument
cryRpt.Load("C:\Users\agis\Documents\Visual Studio 2005\Projects\new_contracts _of_month\new_contracts _of_month\CrystalReport2.rpt", CrystalDecisions.Shared.OpenReportMethod.OpenReportByTempCopy)
With crConnectionInfo
.ServerName = "C:\Users\agis\Desktop\Insurance database\Insurancemanager.mdb"
.UserID = "Admin"
.Password = "kos"
.DatabaseName = "insurancemanager"
End With
'first parameter
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterValues As New ParameterValues
Dim crParameterDiscreteValue As New ParameterDiscreteValue
crParameterDiscreteValue.Value = Convert.ToDateTime(TextBox1.Text)
crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields
crParameterFieldDefinition = crParameterFieldDefinitions.Item("startdate")
crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterValues.Add(crParameterDiscreteValue)
crParameterValues.Clear()
' second parameter :
Dim crParameterFieldDefinitions2 As ParameterFieldDefinitions
Dim crParameterFieldDefinition2 As ParameterFieldDefinition
Dim crParameterValues2 As New ParameterValues
Dim crParameterDiscreteValue2 As New ParameterDiscreteValue
crParameterDiscreteValue2.Value = Convert.ToDateTime(TextBox2.Text)
TextBox3.Text += " " + crParameterDiscreteValue2.Value
crParameterFieldDefinitions2 = cryRpt.DataDefinition.ParameterFields
crParameterFieldDefinition2 = crParameterFieldDefinitions2.Item("enddate")
crParameterValues2 = crParameterFieldDefinition2.CurrentValues
crParameterValues2.Clear()
crParameterValues2.Add(crParameterDiscreteValue2)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
crParameterFieldDefinition2.ApplyCurrentValues(crParameterValues2)
CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
End Sub
End Class
Don’t understand what exactly your problem is but I assume that you want to pass parameter to report and filter report data on those parameters also want to set run-time connection settings of crystal report.
First create parameter field in your report (right click on parameter fields and click on add in crystal designer)
Now go to “crystal report->report->select expert” menu in visual studio crystal report designer and set data filter condition using these parameter fields.
For passing parameter to report use parameter name and value like this
cryRpt.ParameterFields("parametername").CurrentValues.AddValue(objVal)
cryRpt.ParameterFields("fromdatetime").CurrentValues.AddValue(Now)
you don’t have to set any server name for ms access database better way is create dsn for your database file and use that dsn in your report so your report will never ask for database file unless data file is missing you can even create dsn programmatically at run time if want that let us know
if you wat to set sql like server database password and login at runtime use
repDoc.DataSourceConnections(0).SetLogon("usrID","psw")
repDoc.DataSourceConnections(0).IntegratedSecurity = False

crystal report viewer The Parameter is incorrect

i tried to pass parameter to crystal report viewer. but it show The Parameter is incorrect.
Dim rpt As New ReportDocument
rpt.FileName = My.Application.Info.DirectoryPath & "\MAuditList.rpt"
Dim paramDV As New ParameterDiscreteValue()
paramDV.Value = frmMachine.machine
rpt.ParameterFields("#mchno").CurrentValues.Clear()
rpt.ParameterFields("#mchno").DefaultValues.Clear()
rpt.ParameterFields("#mchno").CurrentValues.Add(paramDV)
paramDV.Value = frmMachine.batch - 1
rpt.ParameterFields("#batch").CurrentValues.Clear()
rpt.ParameterFields("#batch").DefaultValues.Clear()
rpt.ParameterFields("#batch").CurrentValues.Add(paramDV)
paramDV.Value = InpStdPt
rpt.ParameterFields("#Points").CurrentValues.Clear()
rpt.ParameterFields("#Points").DefaultValues.Clear()
rpt.ParameterFields("#Points").CurrentValues.Add(paramDV)
paramDV.Value = OvrPoints
rpt.ParameterFields("#ovrpt").CurrentValues.Clear()
rpt.ParameterFields("#ovrpt").DefaultValues.Clear()
rpt.ParameterFields("#ovrpt").CurrentValues.Add(paramDV)
Form1.CrystalReportViewer1.ReportSource = rpt
Form1.Show()</code>
any error in these code??
You should assign the parameters to your Report object and then assign the report object to your CrystalReportViewer.
Try something like this:
'Create report object and load the report
Dim rpt As New ReportDocument()
rpt.FileName = My.Application.Info.DirectoryPath & "\MAuditList.rpt"
'Set the Parameters
Dim paramDV As New ParameterDiscreteValue()
paramDV.Value = "Parameter Value"
rpt.ParameterFields("#ParameterName").CurrentValues.Clear()
rpt.ParameterFields("#ParameterName").DefaultValues.Clear()
rpt.ParameterFields("#ParameterName").CurrentValues.Add(paramDV)
'Assign the report object to the Viewer Report Source
Form1.CrystalReportViewer1.ReportSource = rpt
Form1.Show()