Crystal Reports Display String Formula Error - vb.net

I have an application which is running over hundred of system , I am facing an error in formula statement and only the best and optimal solution to change in Stored Procedure rather change in Report DLL .
Below i m attaching the screen shots , If any body suggest the best solution.
When Gross < 0 then this error occures.
any body suggest the solution

You are getting the error because the string you are converting to number doesn't have any numeric value as string but it contains string characters...
You are again converting Number to String after converting to number... why this multiple conversions.. instead use string value directly..
If you still want to do the same way then suggested approach is to check first with IsNumeric and then convert to number.
Main thing to consider is what does the variable String contains if Gross<0

Related

Issue summing doubles in DataTable with VB?

I am having an issue summing a column of dollar amounts in a DataTable with VB. I have tried two different ways resulting in two different errors which I am not sure how to resolve. Before I go into the two ways I have tried to solve the problem here is the setup:
I am importing a tab delimited file into a DataTable. The headers are automatically populated with data from the first row in the file.
The DataGridView that displays the Datatable is called DGV_detail.
The column in DGV_detail I am trying to sum occurs at column 3 and is called 'Value-to-date'. This column is full of dollar amount values similar to: $10.00 With the dollar sign and everything.
I am also declaring a view variables and doing some calculations when a button is clicked.
Here is my first approach:
For i As Integer = 0 To DGV_detail.Rows.Count() - 1 Step +1
interestPaidToAccounts = interestPaidToAccounts + DGV_detail.Rows(i).Cells(3).Value
Next
When I try this approach I get the following error:
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Conversion from string "" to type 'Double' is not valid.
The other methods I was trying to use to solve the problem were (For reference: In these examples I created a variable local to the button_click function that assigned the BGV_detail.DataSource to equal dataTable1):
Dim sumObject = dataTable1.Compute("Sum(Convert(Value-to-date, 'System.Decimal'))", "[Value-to-date] IS NOT NULL")
and
Dim sumObject As Decimal = dataTable1.Compute("Sum(Value-to-date)", "")
Which both produced the following error:
Expecting a single column argument with possible 'Child' qualifier.
I am pretty lost at this point so whatever the community thinks the best method to resolves these problems is, I am open to.
I can provide additional information if needed! :)
Thanks in advance for your help!
The dollar sign is the issue, use mid to remove it and val to convert to number:
val(mid(DGV_detail.Rows(i).Cells(3).Value, 2,DGV_detail.Rows(i).Cells(3).Value.length -1))
Untested, but should get you started ... there are any number of ways to remove the $.

VB.NET - Can't get Integer to work

I'm working with Windows Forms Application in Visual Studio 2015, using .NET Framework 4.5.2. So I'm making a simple program which includes a label to display a value. The label (called lblMoney) displays the currency (in this case, $) and then the value (e.g 350). Together it looks like $350.
Now, I made another form with a textbox called txtCash and a button called bApply. You enter any integer (e.g 350) into txtCash. When you press bApply, the number in txtCash will add to the number in lblMoney. So, if you had 5 in lblMoney, and you entered 350 in txtCash, lblMoney would display 355.
Here's my code to add to the number:
My.Forms.VeilSideCash.lblMoney.Text = cstrx + txtCash.Text.ToString
The form VeilSideCash is the form that holds lblMoney.
Here's the code for cstrx:
Dim cstrx = "$" & Val(My.Settings.Money.ToString)
The problem here is that, instead of overwriting to lblMoney, the new number is just added after the original number. So if lblMoney has 5 and you enter 350 into txtCash, lblMoney looks like $5350.
How would I go about overwriting with the new number (adding to) instead of replacing?
Any help would be appreciated. Feel free to edit incase I messed something up while explaining.
The problem with your code is the Option Strict setting for your project. You have it set to Off and this allows your code to freely treat strings as they were numbers and try to use them in mathematical operations.
But, when you use the + operator between two strings, it doesn't matter if, for a human being, the two strings represent a number, the compiler see them as strings (cstrx, txtCash.Text) and thinks that you want to use the concatenation operator defined for strings (the +). Yielding wrong (for you) results .
I really suggest you to not use the automatic conversions made by the compiler on your code, instead, when you need to do math operations, always convert your strings to numbers and do the math with variables of numeric type (You could also change the Option Strict to On, but you should be prepared to solve a lot of problems in your current code)
Instead, with a proper numeric approach, you get the text inside txtCash and try to check if your user has correctly typed a decimal value.
Dim cash As Decimal
if Not decimal.TryParse(txtCash.Text, cash) Then
MessageBox.Show("Invalid cash value")
Return
End if
Now get the text of lblMoney and convert it back to a decimal number alerting the compiler that there is a currency symbol in the text to convert
Dim current As Decimal
current = decimal.Parse(lblMoney.Text, NumberStyles.Currency, CultureInfo.CurrentCulture)
Now you have two numbers and the + operator does what you expect. It adds the two numbers together. Finally you could write back the result with a proper currency formatting
Dim result as Decimal
result = current + cash
lblMoney.Text = result.ToString("C")
You need to remove the "$" and convert to a number. I used a decimal so you can include cents if you want:
Dim sum As Decimal
sum = Val(cstrx.Replace("$","")) + Val(txtCash.Text)
My.Forms.VeilSideCash.lblMoney.Text = sum.ToString()
Dim cstrx = sum.ToString("C")
Note that I used the "C" to format the sum as currency. That automatically puts the $ on for you, or uses other currency symbols for other countries.
I'm not a big VB.net user, so my syntax may be slightly off.
When you use the + operator with string unexpected results can occur. In this case the string with a $ cannot be implicitly converted so you should explicit convert it
Once you convert your strings to a number type you can then use the + operator and they can be implicitly converted back to a string.
My.Forms.VeilSideCash.lblMoney.Text = decimal.Parse(cstrx, NumberStyles.Currency)
+ decimal.Parse(txtCash.Text ,NumberStyles.Currency)

Adjusting number format using vb.net

I have this statement nested in an IF-Statement under a scenario. I would like it to format column 2 into a Accounting number without decimal places. How can this be done using VB.net? The current code gives me ex: 1,000,000.00 on an input of 1000000. I noticed excel has buttons for adding or subtracting decimal places. Could these be called within VB.net? Thanks!
Lo.ListColumns("Column2").DataBodyRange(CurrentRow).NumberFormat = "ACCOUNTING"
Try the following
Lo.ListColumns("Column2").DataBodyRange(CurrentRow).NumberFormat = "#0,000.00"
You may find help in this Article
From what i understand you want, you can do:
math.floor(*insert the value or variable you want here*)
What that does is it changes the number in the parameter to the biggest integer lower than the parameter.
Hope this works :)

Handling ⅝ (five eights) Vulgar fraction as part of a string in VBA

As part of an access application, i retrieve data from sql server. I have a string field that contains ⅝ in one of the rows, and when i attempt to insert that value into an MsAccess recordset, i get an error
Multiple-step operation generated errors. Check each status value. Here is my code
sFieldValue = getValue() ' when i add a watch, the '⅝' is replaced by a ? e.g. "The result is ⅝" will be shown as "The result is ?"
Rs(sFieldName) = sFieldValue ' error is thrown
I then attempted to hard code the value in VBA
sFieldValue ="⅝"
And the moment i type '⅝' it automatically changes to a question mark
sFieldValue ="?"
I would like to know how i can support characters such as "⅝". The other fractions work just fine, e.g. '½'. I do not want to do any calculations, the fractions are part of a string that comes from a SQL Server, and the problem is, i get a runtime error when i try to add a string value that contains a fraction to a recordset in access.
from this page, it shows that some fractions are supported in utf-8
The problem was that the field type of Rs(sFieldName) was adVarChar. So to fix the problem, i used a adVariant field type in my recordset, and it now accepts the ⅝ vulgar character.
I am creating the recordset on the fly.
Edit: adVarWChar is more appropriate, see comment below

Detecting NULL dates and showing empty string in SSRS

I'm trying to format some cells in a Reporting Services report that will contain DateTime? values - or not.
If the underlying data has a NULL for that DateTime?, I'd like to show nothing (empty cell) - and if that data source contains a value, I'd like to show the date in short date format (dd.MM.yyyy in my locale).
So I tried to put this formula into the relevant SSRS cells
=FormatDateTime(Fields!DatumBSE.Value, 2)
but now I'm getting 01.01.0001 for all NULL dates....
I can't seem to wrap my head around how to do this in a SSRS (VB) formula.... I tried using IsNothing() but that doesn't seems to really help - I can detect a NULL, but how to I tell the cell to show an empty string in that case?
Solution:
I ended up using this function:
=IIF(IsNothing(Fields!DatumBSE.Value), "", FormatDateTime(Fields!DatumBSE.Value, 2))
Seems to work just fine for me now.
I just tested the following expression and it replaced the null date with an empty string:
=IIF(Fields!DatumBSE.Value is nothing, nothing, FormatDateTime(Fields!DatumBSE.Value, 2))
The other suggestion that I would make is that you could format the date to the correct format in the report dataset by placing a CASE expression around the date value.
use a code like this:
If(isNull([date field]),Null, elsequote)