CHANGING DATA TYPE

  • Thread starter Thread starter Savas Ates
  • Start date Start date
S

Savas Ates

Dim ProductPropertyValues_Value as string



ProductPropertyValues_Value = xx("ProductPropertyValues_Value")

it returns error.. Because xx("ProductPropertyValues_Value") returns NULL
or "" records?

How can i convert it to string or which method should i use it ?
 
Savas Ates said:
Dim ProductPropertyValues_Value as string



ProductPropertyValues_Value = xx("ProductPropertyValues_Value")

it returns error.. Because xx("ProductPropertyValues_Value")
returns NULL or "" records?

How can i convert it to string or which method should i use it ?


Did you enable Option Strict?

What is xx("ProductPropertyValues_Value")?

What is the type of xx("ProductPropertyValues_Value")?

What is the instance type of the object returned by
xx("ProductPropertyValues_Value")?


Armin
 
Dim xx As SqlClient.SqlDataReader

I take values from my db by appliying that method. ?
 
Savas Ates said:
Dim xx As SqlClient.SqlDataReader

I take values from my db by appliying that method. ?


How do you intend to handle Null values coming from the database? You can
not store them in a string. Declare the variable As Object.

dim ProductPropertyValues_Value as object

ProductPropertyValues = xx("ProductPropertyValues_Value")

Later, if you want to distinguish between Null and String, you have to
write:


if ProductPropertyValues_Value is dbnull.value then
'handle null value
else
'handle string
end if



- OR -

Some people convert DBNull to Nothing in order to be able to declare the
variable As String:

Dim ProductPropertyValues_Value as string
dim o as object

o = xx("ProductPropertyValues_Value")

if o is dbnull.value then
ProductPropertyValues_Value = Nothing
else
ProductPropertyValues_Value = o.ToString
end if

Later usage:
if ProductPropertyValues_Value is nothing then
'...
else
'...
end if

Later, if you want to save the values back to the database, you have to
convert back from Nothing to DBNull.Value.


Typed datasets, or classes that you create on your own, can have typed
members (As String) that simplifies this (because the member is typed), and
they have additional "IsMemberNull" and "SetMemberNull" methods.


Armin
 
Savas Ates said:
Dim ProductPropertyValues_Value as string

ProductPropertyValues_Value = xx("ProductPropertyValues_Value")

it returns error.. Because xx("ProductPropertyValues_Value") returns NULL
or "" records?

How can i convert it to string or which method should i use it ?

\\\
Dim Value As Object = xx("...")

' 'If Value Is DBNull.Value Then...'.
If IsDBNull(Value) Then
s = ""
Else
s = DirectCast(Value, String)
End If
///
 
Herfried K. Wagner said:
Dim Value As Object = xx("...")

' 'If Value Is DBNull.Value Then...'.
If IsDBNull(Value) Then
s = ""

This means, you can not distinguish between Null and "" anymore, later. I
guess this is not intended because if both were equal, the database design
would be wrong (usually).


Armin
 
Armin Zingler said:
This means, you can not distinguish between Null and "" anymore, later. I
guess this is not intended because if both were equal, the database design
would be wrong (usually).

This depends on what you want to archive. If the intention is to display
the values in labels or similar, then my code makes sense, IMO.
 
Armin,

Do I see something wrong

ToString or Cstr does mostly everything in these cases.

However because all these answers I become confused.

Cor
 
Herfried K. Wagner said:
This depends on what you want to archive. If the intention is to
display the values in labels or similar, then my code makes sense,
IMO.


As from the OP's post I only see a String typed variable, I can not assume
that a label will be used. I can only answer based on the information I
have. That's why I also assume the correct database design. Consequently, it
does not make sense to convert Null to "". But let's wait for the OP's
answer.


Armin
 
Cor Ligthert said:
Armin,

Do I see something wrong

ToString or Cstr does mostly everything in these cases.

However because all these answers I become confused.

Cor


DBNull.Tostring probably gives not the result expected.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top