Option Strict On disallows implicit conversions from 'String' to 'Integer' - asp.net

txtAddress.Text = DB.ProfileDataset.Tables("tblCustomers").Rows.Item("Address").toString
The above code generated Option Strict On disallows implicit conversions from 'String' to 'Integer' error under the Item("Address")
I don't know what i did wrong...

The DataRowCollection.Item property requires an integer for the row index.
I think you are after the following syntax:
txtAddress.Text = DB.ProfileDataset.Tables("tblCustomers").Rows(0)("Address").ToString()
EDIT
Something to keep in mind:
original code
= DB.ProfileDataset.Tables("tblCustomers").Rows.Item("Address").toString
compiler sees
= DB.ProfileDataset.Tables.Item("tblCustomers").Rows.Item("Address").toString
fixed code
= DB.ProfileDataset.Tables("tblCustomers").Rows(0)("Address").ToString()
compiler sees
= DB.ProfileDataset.Tables.Item("tblCustomers").Rows.Item(0).Item("Address").ToString()

Related

Conversion from type 'TimeSpan' to type 'String' is not valid

' >
System.InvalidCastException: Conversion from type 'TimeSpan' to type 'String' is not valid
' >
I have a gridview , i want that the column"duréeCalculée" take like this value "text='<%# (TimeSpan.Parse(Eval("heure_retour") - Eval("heure_depart"))).ToString()%>'"
but when my page generate I got this problem(Conversion from type 'TimeSpan' to type 'String' is not valid)
Can U help Me?
I'm uncertain with this little information that anyone will be able to help you. It would seem that TimeSpan.Spare() is returning a structure of type TimeSpan. When you try to use .ToString(), your compiler isn't figuring out what to do because .ToString() isn't recognised as a conversion method for types TimeSpan.
Most likely what you need to do is get the element TimeSpan that you want and then parse it. Something along the lines of
double Element1 = TimeSpan.Element1;
char str[64];
sprintf(str,"%f",Element1);
Where Element1 is the name of the element you wish to retrieve.

Conversion from string "0.##" to type 'Integer' is not valid

i am doing a simple conversion from decimal to string and stripping trailing zeros like so:
argCat.ToString("0.##")
however, i keep getting the following error:
Conversion from string "0.##" to type 'Integer' is not valid.
am i missing something?
This would happen if argCat is of a type that does not have a ToString() overload that accepts a parameter.
In such a case, your code is parsed as ToString()("0.##"); the "0.##" becomes an argument to the indexer in the String returned by ToString().
You then get this misleading error because that indexer takes an int, not a string.
string str = String.Format("{0:C}", argCat);

How to really check if something is null in VB.NET?

I have an entity with a NrPeso decimal property that can be saved in the database as 0 or null.
Here is what I'm doing to assign the value to the property:
entity.NrPeso = Convert.ToDecimal(object.value("value"))
The problem is: if I don't fill the object value, it's set to Nothing. When I do the cast it turns into 0. But I don't want 0, I want Nothing. If I compare the object value with Nothing it will return me Nothing if it is Nothing or 0.
I tought in a few alternatives but they don't seem good.
So, what is the right way to do this?
Decimal is a structure - is cannot be Nothing by definition.
You need a nullable Decimal.
If NrPeso is defined as Nullable(Of Decimal) (aka Decimal?), things should work as you expect.
If you want to distinguish between 0 and Nothing when using Decimal (or any other value type), you should use a nullable type in the first place.
So use Decimal? instead of Decimal
Try this:
Dim obj As Object = object.value("value")
entity.NrPeso = If (obj Is Nothing, Nothing, Convert.ToDecimal (obj))
Instead of using Convert.ToDecimal, consider using Decimal.TryParse and then in a failure condition explicitly set the Nullable type to Null:
Dim outVal As Decimal?
If Decimal.TryParse(object.value("value"), outVal)
entity.NrPeso = outVal
Else
entity.NrPeso = Nothing
End If
Additionally, consider setting Option Strict On in your project to avoid type issues like this in the future.

What's wrong with groovy math?

This seems quite bizarre to me and totally putting me on the side of people willing to use plain java. While writing a groovy based app I encountered such thing:
int filesDaily1 = (item.filesDaily ==~ /^[0-9]+$/) ?
Integer.parseInt(item.filesDaily) : item.filesDaily.substring(0, item.filesDaily.indexOf('.'))
def filesDaily = (item.filesDaily ==~ /^[0-9]+$/) ?
Integer.parseInt(item.filesDaily) : item.filesDaily.substring(0, item.filesDaily.indexOf('.'))
So, knowing that item.filesDaily is a String with value '1..*' how can it possibly be, that filesDaily1 is equal to 49 and filesDaily is equal to 1?
What's more is that when trying to do something like
int numOfExpectedEntries = filesDaily * item.daysToCheck
an exception is thrown saying that
Cannot cast object '111' with class 'java.lang.String' to class 'int'
pointing to that exact line of code with multiplication. How can that happen?
You're assigning this value to an int:
item.filesDaily.substring(0, item.filesDaily.indexOf('.'))
I'm guessing that Groovy is converting the single-character string "1" into the char '1' and then taking the Unicode value in the normal char-to-int conversion... so you end up with the value 49.
If you want to parse a string as a decimal number, use Integer.parseInt instead of a built-in conversion.
The difference between filesDaily1 and filesDaily here is that you've told Groovy that filesDaily1 is meant to be an int, so it's applying a conversion to int. I suspect that filesDaily is actually the string "1" in your test case.
I suspect you really just want to change the code to something like:
String text = (item.filesDaily ==~ /^[0-9]+$/) ? items.filesDaily :
item.filesDaily.substring(0, item.filesDaily.indexOf('.'))
Integer filesDaily = text.toInteger()
This is a bug in the groovy type conversion code.
int a = '1'
int b = '11'
return different results because different converters are used. In the example a will be 49 while b will be 11. Why?
The single-character-to-int conversion (using String.charAt(0)) has a higher precedence than the integer parser.
The bad news is that this happens for all single character strings. You can even do int a = 'A' which gives you 65.
As long as you have no way of knowing how long the string is, you must use Integer.parseInt() instead of relying on the automatic type conversion.

C# - Datetime convertion and GetDayOfYear usage fails

There's the code:
HebrewCalendar Heb = new HebrewCalendar();
DateTime tmp = new DateTime(1964,2,3);
MessageBox.Show(Heb.GetDayOfYear(tmp));
it's very basic and simple, but yet - i get an errors:
Error 1 The best overloaded method match for System.Windows.Forms.MessageBox.Show(string)' has some invalid arguments..
Error 2 Argument 1: cannot convert from 'int' to 'string'
what is the problem?
I'm not familiar with HebrewCalendar, but given the error message, I'd say that GetDayOfYear is returning an integer.
Try this:
MessageBox.Show(Heb.GetDayOfYear(tmp).ToString());
MessageBox.Show doesn't know how to deal with integers. If you convert it to a string first, it will show you the string representation.

Resources