Problem returning field in ms sql 2005 - System.InvalidCastException: - sql

Using the code below, I am returning an nvarchar field from MS SQL 2005 and keep getting a System.InvalidCastException.
vo.PlacementID = dr.IsDBNull(0) ? null : dr.GetString(0);
The vo.PlacementID variable is of type String so there shouldn't be a problem.
The values I am trying to return are like this (number, number, letter): 00F, 02A, 01F, etc
System.InvalidCastException: Unable to cast object of type 'System.Int32' to
type 'System.String'.
at System.Data.SqlClient.SqlBuffer.get_String()
at System.Data.SqlClient.SqlDataReader.GetString(Int32 i)
Any help much appreciated.

If you read the exception again it gives you a clue as to the problem:
System.InvalidCastException:
Unable to cast object of type 'System.Int32' to type
'System.String'. at
System.Data.SqlClient.SqlBuffer.get_String()
at
System.Data.SqlClient.SqlDataReader.GetString(Int32
i)
Basically the underlying data type being returned in column 0 of your SqlDataReader isn't a string compatible type, hence the cast exception.
I'd suggest setting a breakpoint on the offending line and then execute the following line in the immediate window:
? dr.GetValue(0).GetType()
This will tell what type being returned.

the cast exception is not raised in the assignment, but in the datareader's GetString().
try dr.GetValue(0).ToString()

The InvalidCastException isn't raised because of the type incompatibility between the PlacementID property and string. If that was the case, you'd get a compile-time error. The problem is the first field in the result set is not a string, it's something else.

Related

error in LinkingSetRelation - Invalid cast ... as object

I have the following error when I try to save a form with linkingsetrelation field:
Invalid cast exception while trying to set the value of ProductId field on ProductRow as object.
What can cause this error? any idea?
Thx
found it.... fields must be Int32, cannot use Int64!

Change a setting using a combobox in vb.net

I'm working on a program, and I need to be able to change a setting using a combobox. I'm getting
CS0266 Cannot implicitly convert type 'object' to 'FastColoredTextBoxNS.Language'. An explict conversion exists (are you missing a cast?)
Using the code:
{
fastColoredTextBox1.Language = comboBox1.SelectedItem
}
Does anyone know a simple way to fix this? If any more info is needed, I will gladly edit it in.
You must cast to the desired type, since the SelectedItem property returns the unspecific type Object.
fastColoredTextBox1.Language = DirectCast(comboBox1.SelectedItem, FastColoredTextBoxNS.Language)
Note there is also the CType function. In addition to performing type casts it also performs also type conversions. We do not need a conversion here, if the items in the ComboBox are of the required type. DirectCast just means, okay I know that the item with the runtime type Object is of the required type, just take it as such.
See also:
Casting DataTypes with DirectCast, CType, TryCast
Difference between DirectCast() and CType() in VB.NET

Assign null value to data row

I am trying to write one line IF condition when assign value to property.
I've tried these syntax in VB.NET, type_of_documents is nullable integer:
1) vehicle.type_of_documents = If(vehicle.Istype_of_documentsNull, SqlTypes.SqlInt32.Null, vehicle.type_of_documents)
2) vehicle.type_of_documents = If(vehicle.Istype_of_documentsNull, DBNull.Value, vehicle.type_of_documents)
3) vehicle.type_of_documents = If(vehicle.Istype_of_documentsNull, Nothing, vehicle.type_of_documents)
Well, I am a little pushy to do this in one line. Somehow, all these syntax have failed to assign null value to my database. Syntax 1 & 2 have thrown cast integer exception. Syntax 3 is no error but no change/update value in database (same as previous).
Can anyone show me better syntax? Since I am really not into VB.NET.
Thanks in advance
The If operator is basically generic, i.e. the type it returns is inferred from the type of the second and third arguments. That means that those two arguments have to be the same type or one must assignable from the type of the other. A simple cast is all you need:
vehicle.type_of_documents = If(vehicle.Istype_of_documentsNull,
CObj(DBNull.Value),
vehicle.type_of_documents)
Now that the first argument is specified to be type Object, the two arguments share a type.
Return type of If method will be inferred from result parameters (as #jmcilhinney already answered).
In 1 and 2 samples you get cast exception because Integer cannot be casted to SqlTypes.SqlInt32.Null and DbNull.Value types.
Third sample compile because Nothing can be introduced as Integer, it simply default value of Integer which equals 0.
If you want return DbNull.Value in case when type_of_documents has no value - you need cast results in their common type, which is Object.
Instead of If method you can create extension method which return common type(object) required by SqlParameters
Public Module Extensions
<Extension>
Public Function GetValueOrDbNull(this As Integer?) As Object
If this.HasValue Then Return this
Return DBNull.Value
End Function
End Module
And use it
Dim sqlValue = vehicle.type_of_documents.GetValueOrDbNull()

VB Compiler Conversion from string to number

This is more of a curiosity question on what the VB compiler is doing. Basically the following code generates an error,
If "String" = CInt(1) Then
End If
As it should. What makes me curious is the error reported is
Conversion from string "String" to type 'Double' is not valid.
So, I guess my question is, why is the compiler attempting to convert to a Double when i would assume it should be converting to Integer?
Following can give some hint.
For following
If "String" = CInt(1) Then
End If
The innerexception stacktrace shows
at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value)
Even if you change the statement as
If "String" = CDbl(1) Then
or
If "String" = CDec(1) Then
It still shows the innerexception stacktrace as given above.
It means it has to do nothing with the right hand side value. It is behavior of compiler while doing implicit conversion to convert the string to more accommodating data type which is double(long would be too long-pun intended ).
This behavior can be proved by changing the statement to :
If CInt("String") = CLng(1) Then
End If
For this the innerexception stacktrace shows
at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value)
Which means even for explicit type conversion it first tries to convert the string to double(most accommodating) and then converts it to integer.

Why am I getting this Objective-C error message: invalid conversion from 'objc_object*'

This error message had me stumped for a while:
invalid conversion from 'objc_object* to 'int'
The line in question was something like this:
int iResult = [MyUtils utilsMemberFunc:param1,param2];
It doesn't matter what the "to" type is, what is important is that you recognize that this message, in this context, is reporting that the utilsMemberFunc declaration was not found and due to Objective-C's dynamic binding it is assuming it returns an objc_object* rather than the type that utilsMemberFunc was declared to return.
So why isn't it finding the declaration? Because ',' is being used rather than ':' to separate the parameters.