How to combine Database fields label together in crystal report - c#

Hi
I am working in .Net crystal reports with asp2.0 and VS2010, now what i want is
i have four fields, drag and dropped, AddressName, AddressCity, AddressZip etc, now i have placed their labels and they repeat, what i want is, one label should have AddressName,AddressCity,AddressZip combined, like 507 Eden Towers Main Boulevard Gulberg, Lahore 54000. And also, i dont want separate fields to be combined as they overlap on lengthy record, i want one label with everything related to address binded.
Please help

You can just create a new formula field, and in that formula concatenate your fields together:
{YourTableName.AddressName} + " " + {YourTableName.AddressCity} + ", " + {YourTableName.AddressZip}
Then, instead of the AddressName, AddressCity and AddressZip fields, put your new formula field on the report instead.

You can also create a text label and drag any report field in the label. That way, the text is actually concatenated and you won't have any spacing problems.

Related

How to generate product receipt in winform application?

I am working on a winform application for the first time and I have a gridview which contains a list of products users have bought.
I have a Print button on click which allows the user to generate a receipt like the one below:
So here I am confused whether I should use "winform default RDLC or Crystal Report" or whether I should generate PDF and then let it print out as receipt, but I am not sure if PDF is a good option for receipt generation or not.
For Crystal Report, I have read that I need to install it and client (who will use this desktop application) had to install Crystal Report and also there is some licensing involve with Crystal Report which I don't want.
Also if I use Crystal Report then I am not sure if it would be possible to generate exactly above receipt (with table formatting) and will it be complicated?
Receipt is bit complicated so is there a better tool or way, or how should I generate receipt I have shown in above image?
Update : Printing paper total size is : 7.50 centimeter and user wants to print all the content in center.
Discount = FinalAmount - MRP;
Customer Name, Mobile No, Bill No, Payment Mode values are entered on the form by user itself.
I am having a Excel file which contains list of products and with each products I have information like ProductId,ProductName,MRP,Tax information like CGST,SGST.
Code to fill gridview from excel file based on Product Id:
using (OleDbConnection cnnxls = new OleDbConnection(strConn))
using (OleDbDataAdapter oda = new OleDbDataAdapter(query, cnnxls))
{
oda.Fill(dtProductList);
DataColumnCollection columns = dtProductList.Columns;
if (!columns.Contains("FinalAmount"))
{
dtProductList.Columns.Add(new DataColumn() { ColumnName = "FinalAmount", DataType = typeof(decimal) });
}
if (!columns.Contains("Quantity"))
{
dtProductList.Columns.Add(new DataColumn() { ColumnName = "Quantity", DataType = typeof(int) });
}
DataRow lastRow = dtProductList.Rows[dtProductList.Rows.Count - 1];
lastRow["FinalAmount"] = Convert.ToDecimal(lastRow["MRP"]);
lastRow["Quantity"] = 1;
}
Generate and print the receipts
You can use any report designer tool like RDLC Reports or Crystal Reports to generate a report. RDLC reports are good enough. You can print the RDLC report with or without showing the print dialog. You can also easily export the RDLC report manually or using the code.
If for any reason you don't want to use a reporting tool, as another option you can consider generating HTML report easily using Run-time T4 templates.
Using an RDLC report, how to show multiple fields in a single cell
You can easily use an expression to show multiple values in a single cell. Also as another option, you can use rows in a single row group and show different fields in a single column.
Example 1 - RDLC - Show multiple fields in a single column using expression
The following steps show you how you can display multiple fields in a single column using expression. I assume you have set up the data source and have ProductName, UnitPrice and Quantity fields. Then, follow these steps:
Drop a Table from toolbox on the report design surface.
In first column, first data row (not the header row), right click and choose ProductName (image)
Select the header of the second column and type UnitPrice/Quantity (image)
In second column, first data row, right click and choose Expression. (image)
In the expression window, enter the desired expression, for example:
= "UnitPrice: " & Fields!UnitPrice.Value.ToString() & System.Environment.NewLine & "Quantitye: " & Fields!Quantity.Value.ToString()
Example 2 - RDLC - Show multiple fields in a single column using row group
The following steps show you how you can display multiple fields in a single column. I assume you have set up the data source and have ProductName, UnitPrice and Quantity fields. Then, follow these steps:
Drop a Table from toolbox on the report design surface.
In first column, first data row (not the header row), right click and choose ProductName (image)
Select the header of the second column and type UnitPrice/Quantity (image)
Right click on row header of the first data row and choose Insert Row → Inside Group - Below (image)
In second column, first data row, right click and choose UnitPrice. (image)
Click on the [UnitPrice], and then press Home and type UnitPrice: (image)
Do the same for Quantity, in the next row in the group.
If you need another row in the group, repeat step 3.
You can setup borders of the cells by selecting them and setting BorderStyle individually for top, left, bottom and right.
Download
You can clone or download an example using expression here:
repository
zip file
A quick and easy way I used before was to generate a html page, and then use the html2pdf library to convert it to a pdf file.
You may also consider this approach since the RDLC reports/Crystal reports may be a overkill for your case.
The RDLC is powerful as well as Crystal reports. You may choose the rdlc which comes close in eliminating licensing costs.
Using RDLC
Data
You need to add datasets Here or data sources to the report which you will manipulate to meet the design and data you want.
Design
On design you just drag and drop controls to your taste. There is a challenge that sometimes what you see on the design may not what be you see on final output so you need to test much.
Printing
You can put a print preview or send directly to a pdf viewer using rdlc. Here is an example.
Conclusion
I think If you have your data generated well on the report, the design and layout won't be much of a problem using both rdlc and crystal reports.
UPDATE
Based on further information provided I have tried to do something that may come close to what you want to achieve.
I have used crystal reports as well as database table to simulate because of time. Otherwise the same can be achieved using rdlc.
The sample table i created
Here is the sample query and results from the database. I have made groups that can be accomodated by the crystal reports. You can do calculated text values using the same to put distinction between the Tax information as well as Transaction Memo.
Here is the final look after tweaking the design. The page layout may also be tweaked with regards to your taste.
Update.
For RDLC I think you need to add datasets for memo data and tax information. Take a look at the below if it comes close. I failed to make a preview there were components I hadn't installed.
for adding 3 columns in one cell
you have two options:
1- Use new line expression
=Fields!MyField1.Value + System.Environment.NewLine + Fields!MyField2.Value
2- Use something like subreport or grouping in rdlc.
the first option seams easier

Repeat a RDLC ReportViewer subreport

I'm really new to RDLC so please forgive me if I'm missing something obvious.
I have some data that needs to get repeated, based on a 1:Many dataset that's linked to the main report. It's not something that can be displayed in a tabular fashion because the customers want it laid out in a specific manner, so using the Tablix control is out. I have a number of subreports based on 1:1 datasets in this main report, and everything is cool with those. I created a subreport for this new dataset and added it to the main RDLC report. Everything works fine, but it only includes the first record of the dataset.
Is it possible to repeat this subreport for each record in the dataset? I think the thing that confuses me is the main RDLC has no code that specifically loads the individual subreports, so I don't see anyplace obvious where I could include a loop or anything.
If you don't have particular needs I think it's important to know that you can put a Rectangle in Tablix cell and then use it as a container for simple controls as TextBox, Line, Image, etc. laied out as you want.
If you still need to use subreport you could put a SubReport in Tablix cell and solve any problem in the LocalReport.SubreportProcessing event that occurs when a subreport is processed.
If you have a lot of records you can use a single Dataset and filtered it in the SubreportProcessing event, using the subreport Parameters that you already set in Designer Mode.
Private Sub SubreportProcessingHandler(sender As Object, e As SubreportProcessingEventArgs)
Dim dvFiltered As New DataView(Me.YourDataSet.Tables(0))
dvFiltered.RowFilter = "Parameter1 = " & CInt(e.Parameters.Item("yourParameter1").Values(0)) & " AND " _
& "Parameter2 = '" & CStr(e.Parameters.Item("yourParameter2").Values(0)) & "'"
e.DataSources.Add(New ReportDataSource("YourDataSourceName", dvFiltered.ToTable("YourDataSourceName")))
End Sub
For example, using a DataSet that contains master and details data, you can build a main report grouped by IdMaster and put a subreport in details section.
This is the subreport: please note that the DataSet is the same as main report but we also need 2 Parameters (IdMaster and IdRow) to display the correct data.
In the main report you need to link subreport Parameters to actual values of DataSet.
Then, the most important part: the SubreportProcessingHandler event. This event is triggered for every instance of the subreport in the main report, so if you have 100 rows / 100 subreports this event is triggedered 100 times: every time you need to specify which data you want to display, so you have to filter the DataSet using the 2 Parameters (IdMaster and IdRow) defined in the previous step and filled with values coming from master report.
Private Sub SubreportProcessingHandler(sender As Object, e As SubreportProcessingEventArgs)
Dim dvTest As New DataView(Me.dsTest.Tables(0))
dvTest.RowFilter = "IdMaster = " & CInt(e.Parameters.Item("parIdMaster").Values(0)) & " AND " _
& "IdRow = " & CInt(e.Parameters.Item("parIdRow").Values(0))
e.DataSources.Add(New ReportDataSource("DataSet_TEST", dvTest.ToTable("DataSet_TEST")))
End Sub
This is the result:
As I stated at the beginning of the answer, if you don't have particular needs you can use a Rectangle instead of a SubReport. Regarding this example you can obtain the same result using the green Rectangle as container.

How Print single field multiple times as per Quantity on Crystal Reports

I am working on crystal reports. I want to print same field multiple times as per Quantity.
if I enter Quantity 6 then it must print 6 times same filed..
How I do this?
i will explain you some steps just try it
Take a formula field
Use for loop in the formula area in which
append the text to same variable so it you will get a string with
multiple times repeated
Place that formula field in your report
and adjust the width to come to the next line
In properties window
edit the CanGrow field and give max number of lines as per your
requirement.
hope this will help you,

How to hide Data in Crystal Report if data does not meet

I am using an if-then conditional in a Crystal Reports formula. I want to display just one record in report based on the results of the conditional. For example:
if(....)="H" then "Habbits" or if(.....)="I" then "Indoor"
How can I achieve this?
I think you need to show data like Habbit, Indoor etc based on condition.
Follow these steps If I am correct in guessing the question:
Create a new formula field, and add the condition in the same.
stringvar nameofvar:="";
if(....)="H" then nameofvar:="Habbits"
else if(.....)="I" then nameofvar:="Indoor"
//If there is a problem in this formula then specify
Drag the formula field from field explorer to the area on the report where you want it to show.

Crystal Report Making on the basis of Number of Selected Attributes from check list, how can we do so?

I am developing application in C# Windows Forms, i make crystal reports on the basis of data collection list in c#, for example i have a table having EmpID, Name, Sponsor, Job Title, Nationality etc. I bring them in collection in c# and pass it to crystal report, where i see my attributes in Database fields, if i drag and drop those fields on the report, for example Name, Job Title, Nationality, then i can see their columns coming in Details section, but the issue is, i have about 25 attributes and i have made a check list of 25 attributes in c#, if i check 13 attributes, it should make a report of 13 columns, the problem is, we make report in crystal report on the basis of drag and dropping fields, how can i dynamically do this, means if there are 13 fields selected in c# check list, there should be just 13 columns in report. Please find the image attached, "How currently i am doing" Please zoom it by right click on it, and open in new window.
I think that the easiest way to approach this, considering data type representations and all of the Crystal complexity under the covers, is to layout your report with all 25 fields sized appropriately.
Then, at runtime, you will need to reposition the report objects, which could be a little tricky due to the relatively unstructured way in which crystal provides the information.
The way I would approach this is to loop through the report objects and generate one SortedList for the data fields and one SortedList for the header fields. They should be sorted on their Left position so that you can process them in appearance order.
Once you have the sorted lists of objects, you can cycle through each one and, if it was not selected by the user, set the Width to 0.
As you are moving through the fields, you will keep track of the current left position. The first field that you process will set your starting point, even if it is not visible. Then, for all subsequent fields, if the field is visible, you will set its left value to the current left position, then add its width plus some separator space to the current left position for the next field.
Hopefully this will help you solve your problem.
It sounds like this would be a good place for a cross-tab report. In this case you'd need to add a cross tab to your report header or footer and pass your data into the report with a column for each attribute descriptions and then group on the attribute description. See below for details:
Data:
RowID ColDesc ColValue
1 Attr1 Value1
1 Attr2 Value2
2 Attr1 Value3
Then you can add your crosstab where your row field is RowID, your column field is ColDesc and the field to summarize is ColValue. You can use a Max of summary on the summarized field since it is different.
This is untested, but I believe that the output for this should be:
CrossTab:
Attr1 Attr2
1 Value1 Value2
2 Value3
As you can see that as you add a new attribute it will show up as a new column in the crosstab. As I said previously, this is untested so I apologize for any errors, but I hope it is enough to help you out. Thanks
Give a look at these links
http://www.c-sharpcorner.com/UploadFile/uditsingh/CR1111022006055359AM/CR11.aspx
http://www.crystalkeen.com/articles/crystalreports/dynamiccrosstab.htm
Using this Google Search
Crystal Reports will not automatically add columns & headers to a report given a list of fields.
My recommendation is to use the Report Application Server .NET SDK to dynamically alter a report. The link includes the API reference, as well as samples.

Categories