Convert string to number value - c#

From my C# code-behind I send a string value to my dataset. The data table also contains a field string type, which is dragged and dropped into Crystal Reports.
Now I need to divide that (string) value by some number, but strings can't be divided. So I need to convert the string to a value first - but I can't. No matter how I try to convert it always shows 0 on the report.
I created a formula called Euro:
NumericText({MYVALUE}) then ToNumber({MYVALUE})
And accessed it from another formula, EuroNum:
(If NumericText({Total}) then ToNumber ({Total}))
Then I created another field lets say TotalEuro where I did this in forumla, but it's always showing 0:
Euro/1.95

I used this formula:
If NumericText({VALUE}) Then ToNumber({VALUE}) / 1.955
Everything seems fine with Crystal Reports but I had problem in Visual Studio.
Even if this file is set up to copy everytime ,file somehow not copy changes to debug folder. So I manually moved this file to debug folder and everything was fine.

Related

SSRS report parameter

I have a problem with rendering report with correct parameter.
We have RDL report that has date parameter with default value which is an expression "=today()".
In project i have the following code in c#
for(int i = 0; i < 15; i++)
{
serverReport.SetParameters(new ReportParameter("dt1",date.ToString()));
File.WriteAllBytes(path, serverReport.Render("PDF"));
}
For first iteration sql stored procedure is called with the default parameter and following iterations are called with passed date(i checked it with sql profiler).
I want to mension that in loop i have many other reports with exact same default date parameter but the problem is with this one. I have compared all parameter properties in 2 repors(one that works and the other that is not working properly), but they are identical. I cant find any difference.
If i delete default value "=today()" then report is working fine.
Maybe sameone had similar problem and recommend me something about this. Thanks in advance.
A few things to check:
SetParameters takes IEnumerable<ReportParameter> not a single ReportParameter object. Call it like this instead:
serverReport.SetParameters(new ReportParameter[] { ReportParameter("dt1", date.ToString()) } );
Make sure that the parameter does not have the available values filled in as well as the default value. If the date passed is not one of the valid values (if applied) it won't work. For example, if the available values are set to =Today then the only report that will run properly is the first one that coincidentally uses that value.
Are you sure that date.ToString() is passing an appropriate and valid date?
Does the server's report parameter match the development environment? Parameter settings aren't automatically updated so that any modifications made on the server aren't overwritten by deploying the report again. Check the server's report parameters and update if necessary, or simply delete and redeploy the report.
Try to completely remove the not working report within the server (and to test,also one of the working) and re-deploy both to the server. you could also check in reporting manager for the parameter settings, because there the difference might be visible.
I had that kind of issues before with report parameters and know that the parameter settings are not overwritten correctly all the time you deploy the report.

Multi-language report in C#

I'm writing reporting application in C# (local reports). I've made report patterns (rdlc files) and now I'm trying to make them autotranslating due to localization of user. I've managed to make a single translation (translation of one field, but there are many of them) with resource file, but this requires parameter to every textbox I use in report :
ReportParameter p = new ReportParameter("Report1Parameter3", GlobalStrings.FieldText);
this.reportViewer1.LocalReport.SetParameters(p);
Is there any way to make it more "direct" than using additional parameter for every text field?
This has been the solution in my desktop application.
Let's say you have a separate resource file stored in the same folder of your application and named Resources.dll created with a resx file named Reports.resx with Public access
Step 1
Set permissions for local report (this should be repeated after every reset)
<YourReportViewer>.LocalReport.SetBasePermissionsForSandboxAppDomain(new PermissionSet(PermissionState.Unrestricted))
Step 2
In your rdlc report add following code (under report properties or manually edit rdlc file adding the code under <Report>/<Code> tag)
Private Shared Resources As Type = Reflection.Assembly.LoadFile(IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources.dll")).GetType("Resources.Reports")
Default Public ReadOnly Property Item(name As String) As String
Get
Return Resources.GetProperty(name, Reflection.BindingFlags.Static Or Reflection.BindingFlags.Public).GetValue(Nothing).ToString()
End Get
End Property
Step 3
In your report you can reference strings in resources using an expression like this:
=Code!<NameOfYourResource>
I think you should put all field to your dataset, and process translate before bind data for every user.

CSV column is being return as null

I have parsing a CSV via the Microsoft.Jet.OLEDB.4.0 provider. Which has been working fine for most of our tasks, but recently I've noticed an issue.
I have a CSV which has a column called Rating, this is generally an integer but occasionally it will be "1-2" or a Date e.g "1/1/2010". The datatable I am importing it into has had its columns explicitly set to strings but when a non-integer field is read it is null instead.
Any ideas how I get round this??
Use a schema.ini file (in the folder that contains your .csv) and specify the columns data types correctly.
Likely what is happening is that the first few fields in the column are being sniffed to determine data type, and then when there are later columns of a different type, they're dropped.
I believe you can turn off this behavior by adding IMEX=1 to your Extended Properties in the connection string. This sets the reader to Intermixed Mode which will read the fields as text. Then you can go through in another pass and set the types yourself.

How do I Color a label in reporting services based on a value in the dataset?

I have an SQL Server 2005 Express Reporting Server and I'm trying update a report to show a coloured label based on a value stored in the database.
I currently store the colour as an aRGB value, but I can change this if required.
I've seen posts on how to use expression in the color property, but I can't embed c# there.
Thanks!
SSRS uses VB.NET, not C#, and most places will accept code, it is prefixed with a '=' to let the parser know that it is code. I don't know exactly where you are trying to set the label (in a table, in a floating textbox, etc), but it is very likely that it is doable.
After some digging I found out that u have to use VB code.
Here's the steps.
Add a reference to System.Drawing in the report properties -> reference tab
Add this custom code to the report properties -> code tab
Public Function GetMyColour(myColour as integer) as string
Dim colorObj As System.Drawing.Color = System.Drawing.Color.FromArgb(myColour)
return String.Format("#{0:X2}{1:X2}{2:X2}", colorObj.R, colorObj.G, colorObj.B)
End Function
Set the color property expression on the report object you want set the color of to this
=Code.GetMyColour(First(Fields!RecipeColour.Value, "StockControl"))

How to specify data types when dragging content to Excel

I am initiating drag and drop from my WinForms application using this simple
IDataObject data = new DataObject();
string textToExcel = "Hello\tWorld\t1\t2\nHello\tWorld\t1\t2\n"
data.SetData(DataFormats.Text, textToExcel);
I works fine when dropped on Excel, it ends up nicely in columns and rows.
Problem is that Excel does not know that the cells with values 1 and 2 are numerics
and it gets worse when dropping a date value.
How can I tell Excel the data types of the individual cells, is there some richer type that Excel accepts when content is being dropped into it.
All you are able to communicate with Drag-Drop to Excel are strings, which Excel will automatically convert to a date or numeric type if it can. If you don't want it to convert identifable types to a data type then you should begin the value with an appostrophe (') character, such as '100, instead of 100.
If you wish to have full control, you should subscribe to the Excel.Worksheet.Change event which will be triggered at the end of the Drag-Drop action. At that point you can do a custom conversion of your own data. This would be facilitated if you transmit your data as a custom format to begin with, one that would not be automatically converted by Excel. For example, instead of sending a 2x2 block of values such as:
100 Hello
$1.25 10/9/2010
You could send it through as:
<DragDrop:Double>100</Double> <DragDrop:String>Hello</String>
<DragDrop:Currency>1.25</Currency> <DragDrop:Date>2010.10.9</Date>
These values would be received by the cells as strings. But when the Worksheet.Change event fires, it will tell you which cells have been changed, and your routine could process the strings, looking for anything that begins with "<DragDrop:". You could then convert these to the required data types as specified in the strings themselves.
For a detailed example, see the article Adding Drag-and-Drop Functionality using the .NET Framework and Visual Studio 2005 Tools for Office Second Edition. In that article, they use an example that is much more unique than "<DragDrop:" -- they use a GUID at the front of the string to really make sure that the string is unique and identifiable. But the basic strategy is as I described, above.
Hope this helps,
Mike

Categories

Resources