Crystal Reports in VB.net with datatable - crystal-reports

!!
I'm using crystal report in my project of vb.net. I'm using visual studio 2010,sql server2008 and SAP crystal report.there is an error that i can not overcome/solve.here is my code:
Dim database As New Database
Dim dr As DataRow
Dim dt As Data.DataTable
Dim report As New CrystalReportChalan
Dim query1 As String
Try
query1 = "select per_name,f_name,per_address,rem_amount from person_info "
database.creatConn()
dt = database.getDS(query1, "person_info")
'dr = dt.Rows(0)
MessageBox.Show(dt.Rows.Count) // Here the no of rows displayed
report.SetDataSource(dt) //Error 'Report has no table'
CrystalReportViewer1.ReportSource = report
CrystalReportViewer1.Refresh()
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
there is a separated class for Database connection
that class has function to get data from database.

Related

Sub report text fields right alignment not working on jasper master report at run time

I set right alignment in sub report text fields. Its showing in preview report but when I run this report from java then sub report text fields right alignment are not working.
Here is my java code
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://XYZ-BD-BBI01;user=abc;password=1234;database=myDataTables");
String report="C:\\Users/mainReport.jrxml";
JasperDesign jd = JRXmlLoader.load(report);
String sql = "select * from Report where ReportID = " + reportID;
JRDesignQuery query = new JRDesignQuery();
query.setText(sql);
jd.setQuery(query);
JasperReport jr = JasperCompileManager.compileReport(jd);
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("ReportID",1211);
JasperPrint jp1 = JasperFillManager.fillReport(jr, parameters, conn);
JasperViewer.viewReport(jp1, false);
}catch (Exception ex) {
ex.printStackTrace();
}
This is the current output
And I want this output

Crystal Reports - how to pass parameter?

I would like to pass a parameter to my report and put the report into my project.
I want to pass a user id as parameter to the report.
The id will be different each time.
Can someone help me on this topic how to pass the parameter.
Here is my code so far:
CrystalReportViewer1.Visible = True
Dim rDoc As New ReportDocument()
' Crystal Report Name
rDoc.Load(Server.MapPath("Sucess.rpt"))
' Your .rpt file path
'set dataset to the report viewer.
CrystalReportViewer1.ReportSource = rDoc
You can try something like this:
string ponumber;
string receiptno;
ponumber = TextBox1.Text;
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);

I can't display any result in crystal report

i'm developing a web app and I can't display any result to my crystal report. i don't know what is wrong in my code. please help.
Dim cnn As SqlConnection
Dim connectionString As String
Dim sql As String
connectionString = "data source=SERVERNAME; _
initial catalog=crystaldb;user id=sa;password=PASSWORD;"
cnn = New SqlConnection(connectionString)
cnn.Open()
sql = "SELECT * FROM Grades
Dim dscmd As New SqlDataAdapter(sql, cnn)
Dim ds As New DataSet1
dscmd.Fill(ds, "Grades")
objRpt.SetDataSource(ds.Tables(1))
CrystalReportViewer1.ReportSource = objRpt
CrystalReportViewer1.RefreshReport()

Populating unbound field in crystal report

I have a crystal report with these fields:
-Name
-Other
The field 'Other' is not a database field, so I added it as an unbound field.
Then I have this code in my controller:
DataSet ds = new DataSet();
DataTable dt = new DataTable("Product");
dt.Columns.Add("Name");
dt.Columns.Add("Other");
When I have populated the above datatable I add it to the dataset, anf then set the dataset as report's datasource:
ReportDocument report = new ReportDocument();
string reportFile = Server.MapPath("~/") + "\\bin\\Reports\\MyReport.rpt";
report.Load(reportFile);
report.SetDataSource(ds);
Then I set the export options (it's a excel report).
In my report I'm getting the text for the 'Name' field correctly, but the 'Other' field is empty. How can I associate the column from the datatable to this field?
Thanks in advance.
This is aspx File :
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" EnableParameterPrompt="false"
ReuseParameterValuesOnRefresh="true" EnableDatabaseLogonPrompt="False" ToolPanelView="None"
HasToggleParameterPanelButton="false" HasCrystalLogo="False" />
This is Cs File ......
protected void btnSubmit_Click(object sender, EventArgs e)
{
LoadData();
}
protected void LoadData()
{
DataSet ds= null;
ds = new DataSet();
DataTable dt = new DataTable("Product");
dt.Columns.Add("Name");
dt.Columns.Add("Other");
rptDoc.Load(Server.MapPath("~/bin/Reports/MyReport.rpt"));
rptDoc.SetDataSource(ds);
CrystalReportViewer1.ReportSource = rptDoc;
CrystalReportViewer1.DataBind();
CrystalReportViewer1.refreshreport();
}
Try this code.......
try this with VB
'pass texbox2 to unbounddate1
CrystalReportSource1.ReportDocument.DataDefinition.FormulaFields.Item("unbounddate1").Text = "'" & Me.TextBox2.Text & "'"
'pass texbox3 to unbounddate2 CrystalReportSource1.ReportDocument.DataDefinition.FormulaFields.Item("unbounddate2").Text = "'" & Me.TextBox3.Text & "'"

Crystal report create connection to OLE DB database

I am working with VS 2005 and using visual basic for coding.
How do I set up the connection object for my crystal report using vb code.
I have written some code
Dim strcon As String = ConfigurationManager.AppSettings("PhdConnectionString")
Dim getconn As SqlConnection = New SqlConnection(strcon)
Dim rpt As ReportDocument = New ReportDocument
rpt.Load(Server.MapPath("aspirantCrystalReport.rpt"))
// I want to set the connection properties here. How do I do that ?
CrystalReportViewer1.ReportSource = rpt
CrystalReportViewer1.DataBind()
I have not done this using the Crystal Reports .NET API, but I do have a working piece of code written in VB6 that calls the COM API. The API class and member names can't be that different.
Private Sub SetDataConnections(ByVal oReport As CRAXDRT.Report, ByVal oConnection As ADODB.Connection)
' Do all tables in this report.
Dim oTable As CRAXDRT.DatabaseTable
For Each oTable In oReport.Database.Tables
SetDataConnection oTable, oConnection
Next
' Find all subreports and do them too.
Dim oSection As CRAXDRT.Section
For Each oSection In oReport.Sections
Dim oObject As Object
For Each oObject In oSection.ReportObjects
If TypeOf oObject Is CRAXDRT.SubreportObject Then
Dim oSubreportObject As CRAXDRT.SubreportObject
Set oSubreportObject = oObject
SetDataConnections oSubreportObject.OpenSubreport()
Set oSubreportObject = Nothing
End If
Next
Next
End Sub
Private Sub SetDataConnection(ByVal oTable As CRAXDRT.DatabaseTable, ByVal oConnection As ADODB.Connection)
' Extract the relevant data from the ADO connection.
Dim sServer As String
Dim sDatabase As String
Dim bTrusted As String
Dim sUserName As String
Dim sPassword As String
Dim nTimeout As Long
With oConnection.Properties
sServer = .Item("Data Source").Value
sDatabase = .Item("Initial Catalog").Value
Select Case UCase(.Item("Integrated Security").Value)
Case "SSPI", "YES"
bTrusted = True
Case Else ' "NO", ""
bTrusted = False
End Select
sUserName = .Item("User ID").Value
sPassword = .Item("Password").Value
End With
nTimeout = oConnection.CommandTimeout
' Delete and re-create all connection information. This is the only way of getting it
' to work if the report contains subreports. Changing database drivers on the fly is
' not allowed, so we must re-create the connection using settings appropriate for the
' particular driver involved.
Select Case oTable.DllName
Case "crdb_ado.dll"
With oTable.ConnectionProperties
.DeleteAll
.Add "Database Type", "OLE DB (ADO)"
.Add "Provider", "SQLOLEDB"
.Add "Data Source", sServer
.Add "Initial Catalog", sDatabase
.Add "Integrated Security", bTrusted
.Add "User ID", sUserName
.Add "Password", sPassword
.Add "OLE DB Services", -1
.Add "General Timeout", nTimeout
End With
Case Else
' TODO: Handle other drivers appropriately.
End Select
End Sub