Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trycast / Ctype 2

Status
Not open for further replies.

woyler

Programmer
Joined
Jun 20, 2001
Messages
678
Location
US
Can someone explain to me why I can convert.tostring and Ctype the ExecuteScalar result if a command object with no issues, but if I use the Trycast operator the value returned is always nothing?

Say my sql text returns the string value "Tek-Tips"

Convert.tostring(cmd.ExecuteScalar) returns "Tek-Tips"
ctype(cmd.ExecuteScalar,string) returns "Tek-Tips"
trycast(cmd.ExecuteScalar,string) returns nothing
 
Is there any reason why you would use TryCast here?

I think you are completely safe using cmd.ExecuteScalar.ToString() as the default value for Object.ToString is the fully qualified name of the object

I'm still on 2003 so I've no other clues about your TryCast problem.
 
Well, currently I have a function that returns a string. This function will query a datasource and return a lone string value(executescalar). The possible values to return are ""(empty string), NULL, or a valid string. I was trying to just assign my funtion value to a string variable
dim MyString as string = MyFunction
Problem is if the value is null an InvalidcastException is thrown. I know I can use an object declaration instead of string. But I thought that was the point of TryCast. I must have misunderstood the functionality of the operator
 
You are getting this error because TryCast depends on inheritance. The object you pass into TryCast MUST be of a type that is either the type that is passed in as the 2nd parameter, or a child class of that type.

Scalars do not inherit from String, so this will always fail. To use TryCast in this situation, you could do this:

Code:
TryCast(cmd.ExecuteScalar,object.type).tostring
That will always return the fully qualified name.

I'm pretty sure that cmd.ExecuteScalar.tostring will safely get you what you are looking for.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
FYI,
if I use cmd.ExecuteScalar.tostring and the value from the query is null, Referenced object has a value of 'Nothing' error is thrown.
 
Performance wise, the best bet would be:
Code:
dim objValue as object
objValue = cmd.ExecuteScalar.tostring
if objValue is nothing then objValue = string.emptystring

That should avoid the null value issue and the performance hit of an exception.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
That is exactly what I am doing now. I was not clear on how the trcast worked. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top