Populating unbound field in crystal report - crystal-reports

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 & "'"

Related

How to fill subreport in crystal report aspx with dataset

I have made two queries in dataset, then I want to set that query to the report
the first query is for the main report,
and the second query is for the subreport which have show by one of attribute field in main report.
Like I will show data (with where : from attribute field from main report)
Please anyone help me...
^^ but this code is still wrong
ReportDocument RptDocument = new ReportDocument();
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = "Your Dataset";
CrystalReportViewer1.DisplayGroupTree = false;
ds.Tables[0].TableName = "Main Report";
RptDocument.Load(Server.MapPath("your rpt file path"));
RptDocument.SetDataSource(ds.Table[0]);
RptDocument.Subreports[0].SetDataSource(ds.Tables[1]);
CrystalReportViewer1.ReportSource = RptDocument;
CrystalReportViewer1.DataBind();
CrystalReportViewer1.Visible = true;
}
please refer this code

Pass value in Crystal report

I add formula field region,fromdate,todate in report
i.e.
UPDATED IMAGE
Also I try this another method:
protected void Report_Click(object sender, EventArgs e)
{
data crystalReport = new data();
crystalReport.DataDefinition.FormulaFields["region"].Text = regiondrop.SelectedValue;
crystalReport.DataDefinition.FormulaFields["fromdate"].Text = fromdate.Value;
crystalReport.DataDefinition.FormulaFields["todate"].Text = todate.Value;
BindReport(crystalReport,Convert.ToDateTime(fromdate.Value), Convert.ToDateTime(todate.Value), regiondrop.SelectedValue);
}
This show error on page:
Error
This field name is not known. Details: errorKind Error in File temp_2c6994eb-49ef-432f-bfd7-af0eb80dc7ec 4032_6896_{5E54477E-F078-41DF-BD52-AF042B96DA53}.rpt: Error in formula fromdate: '{DataTable1.StartDate}' This field name is not known. Details: errorKind
Now this is working finally with the help of #Furtiro
public void BindReport(ReportDocument crystalReport, DateTime fromdate, DateTime todate, string region)
{
T1 t = new T1();
DateTime fdate = new DateTime(fromdate.Year, fromdate.Month, fromdate.Day, 0, 0, 0);
DateTime tdate = new DateTime(todate.Year, todate.Month, todate.Day, 23, 59, 59);
List<griddataresult_Result> dsc = t.griddataresult(fdate, tdate, region).ToList();
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("OwnerName", typeof(string));
dt.Columns.Add("RegNo", typeof(string));
foreach (var c in dsc)
{
dt.Rows.Add(c.ID, c.OwnerName, c.RegNo, c.total_voilatio, c.MileageAccumlation, c.MaxSpeed);
}
crystalReport.DataDefinition.FormulaFields["region"].Text = "'" + region + "'";
crystalReport.DataDefinition.FormulaFields["fromdate"].Text = "'" + fromdate + "'";
crystalReport.DataDefinition.FormulaFields["todate"].Text = "'" + todate + "'";
crystalReport.SetDataSource(dt);
CrystalReportViewer1.ReportSource = crystalReport;
CrystalReportViewer1.DataBind();
}
You can instanciate your report like an object , replace your line :
ReportDocument crystalReport = new ReportDocument();
By :
data crystalReport = new data();
Here data is the name of you .rpt class.
*Update *
You don't understand that ReportDocument is an object, your object here will be your data class.
So all the lines when you set doc = crystalreport are useless because you don't work on your crystalReport object but on other objects.
Second update :
I didn't see your access to the formula, you're not supposed to write the "#' in the name of your formula. My bad sorry , here's your working code :
protected void Report_Click(object sender, EventArgs e)
{
data crystalReport = new data();
crystalReport.DataDefinition.FormulaFields["region"].Text = "'" + regiondrop.SelectedValue.ToString() + "'";
crystalReport.DataDefinition.FormulaFields["fromdate"].Text = "'" + fromdate.Value.ToString() +"'";
crystalReport.DataDefinition.FormulaFields["todate"].Text = "'"+ todate.Value.ToString() + "'";
BindReport(crystalReport,Convert.ToDateTime(fromdate.Value), Convert.ToDateTime(todate.Value), regiondrop.SelectedValue);
}
Be careful about the dataType and parse when you write a formula in c# code, for example if you want to display some text in a formula dynamically you'll need to cast your data with quotes
Like report.DataDefinition.FormulaFields["yourformula"].Text ="'Hello world'";
Dont forget the quotes, simple or double quotes will works.
Best regards,

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 in VB.net with datatable

!!
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.

Crystal report xi + c#.net document load problem

I have a crystal report which gives me undefined error when opening document, does any one come across this type of error, below is the coding:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
///create instance of class first
ReportDocument rpDoc = new ReportDocument();
///load the report
rpDoc.Load(#"TicketingBasic.rpt");////------->>>problem is here
///pass the report to method for dataInfo
getDBInfo(rpDoc);
/// et the source for report to be displayed
CrystalReportViewer1.ReportSource = rpDoc;
}
protected static void getDBInfo(ReportDocument rpDoc)
{
///Connection Onject
ConnectionInfo cn = new ConnectionInfo();
///DataBase,Table, and Table Logon Info
Database db;
Tables tbl;
TableLogOnInfo tblLOI;
///Connection Declaration
cn.ServerName = "???????????";
cn.DatabaseName = "??????????";
cn.UserID = "?????????";
cn.Password = "????????????";
//table info getting from report
db = rpDoc.Database;
tbl = db.Tables;
///for loop for all tables to be applied the connection info to
foreach (Table table in tbl)
{
tblLOI = table.LogOnInfo;
tblLOI.ConnectionInfo = cn;
table.ApplyLogOnInfo(tblLOI);
table.Location = "DBO." + table.Location.Substring(table.Location.LastIndexOf(".") + 1);
}
db.Dispose();
tbl.Dispose();
}
}
Final code snippet is:
rpDoc.Load(Server.MapPath(#"TicketingBasic.rpt"));
Thank you all for the help.
The problem I am having now is report not printing or exporting to other types like .pdf,.xsl, .doc etc, any clue
so the error is literally "undefined error"? never seen that one before.
First guess is that you need the full physical path to the report.
rpDoc.Load(Server.MapPath(#"TicketingBasic.rpt"));
HttpServerUtility.MapPath
How are you handling the report outout?
When we do reports we set the file name:
ReportSource.Report.FileName = FileName;
Where filename is a string that is the name of the file (obviously). Then we select the report tables and export in whatever format. Try this.