By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,485 Members | 1,061 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,485 IT Pros & Developers. It's quick & easy.

Newbie question: Cstr vs toString

P: n/a
Hi

I have a WinForm app with a bound form. When user enters a value in field
rateid I lookup the respective rate amount from a table and assign it to
field rate.I am using the DLookup function to achieve this (full code given
below). This function returns a value of type object so I need to convert it
to string. If I use the function as below (using ToString) it works fine;

Me.txtRate.Text = DLookup(<parameters here>).ToString

but if I use it with a CStr

Me.txtRate.Text = CStr(DLookup(<parameters here>))

and then browse through 2-3 record the form stops showing data in the bound
fields. This brings me to question; what is the difference between CStr and
tostring that could be causing the above problem?

Thanks

Regards

=======

Function DLookup(ByVal SearchFld As String, ByVal SearchTbl As String, ByVal
SearchCriteria As String) As Object
Dim Cmd As OleDb.OleDbCommand
Dim Reader As OleDb.OleDbDataReader

Cmd = New OleDb.OleDbCommand("SELECT " & SearchFld & " FROM " & SearchTbl
& " WHERE " & SearchCriteria, DBConnection())
Reader = Cmd.ExecuteReader()

If (Reader.Read()) Then
DLookup = Reader.GetValue(0)
Else
DLookup = DBNull.Value
End If

Reader.Close()
Reader = Nothing
Cmd = Nothing
End Function
Jul 3 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
CStr can't convert DBNull.Value to a string. It's weird you don't have a
more explicit error...

--
Patrice

"John" <in**@nospam.infovis.co.uka écrit dans le message de groupe de
discussion : #F**************@TK2MSFTNGP03.phx.gbl...
Hi

I have a WinForm app with a bound form. When user enters a value in field
rateid I lookup the respective rate amount from a table and assign it to
field rate.I am using the DLookup function to achieve this (full code
given below). This function returns a value of type object so I need to
convert it to string. If I use the function as below (using ToString) it
works fine;

Me.txtRate.Text = DLookup(<parameters here>).ToString

but if I use it with a CStr

Me.txtRate.Text = CStr(DLookup(<parameters here>))

and then browse through 2-3 record the form stops showing data in the
bound fields. This brings me to question; what is the difference between
CStr and tostring that could be causing the above problem?

Thanks

Regards

=======

Function DLookup(ByVal SearchFld As String, ByVal SearchTbl As String,
ByVal SearchCriteria As String) As Object
Dim Cmd As OleDb.OleDbCommand
Dim Reader As OleDb.OleDbDataReader

Cmd = New OleDb.OleDbCommand("SELECT " & SearchFld & " FROM " & SearchTbl
& " WHERE " & SearchCriteria, DBConnection())
Reader = Cmd.ExecuteReader()

If (Reader.Read()) Then
DLookup = Reader.GetValue(0)
Else
DLookup = DBNull.Value
End If

Reader.Close()
Reader = Nothing
Cmd = Nothing
End Function
Jul 3 '08 #2

P: n/a
Here is sample project; http://www.infovis.biz/TestEMS.zip

Thanks

Regards

"John" <in**@nospam.infovis.co.ukwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi

I have a WinForm app with a bound form. When user enters a value in field
rateid I lookup the respective rate amount from a table and assign it to
field rate.I am using the DLookup function to achieve this (full code
given below). This function returns a value of type object so I need to
convert it to string. If I use the function as below (using ToString) it
works fine;

Me.txtRate.Text = DLookup(<parameters here>).ToString

but if I use it with a CStr

Me.txtRate.Text = CStr(DLookup(<parameters here>))

and then browse through 2-3 record the form stops showing data in the
bound fields. This brings me to question; what is the difference between
CStr and tostring that could be causing the above problem?

Thanks

Regards

=======

Function DLookup(ByVal SearchFld As String, ByVal SearchTbl As String,
ByVal SearchCriteria As String) As Object
Dim Cmd As OleDb.OleDbCommand
Dim Reader As OleDb.OleDbDataReader

Cmd = New OleDb.OleDbCommand("SELECT " & SearchFld & " FROM " & SearchTbl
& " WHERE " & SearchCriteria, DBConnection())
Reader = Cmd.ExecuteReader()

If (Reader.Read()) Then
DLookup = Reader.GetValue(0)
Else
DLookup = DBNull.Value
End If

Reader.Close()
Reader = Nothing
Cmd = Nothing
End Function

Jul 3 '08 #3

P: n/a

"John" <in**@nospam.infovis.co.ukwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi

I have a WinForm app with a bound form. When user enters a value in field
rateid I lookup the respective rate amount from a table and assign it to
field rate.I am using the DLookup function to achieve this (full code
given below). This function returns a value of type object so I need to
convert it to string. If I use the function as below (using ToString) it
works fine;

Me.txtRate.Text = DLookup(<parameters here>).ToString

but if I use it with a CStr

Me.txtRate.Text = CStr(DLookup(<parameters here>))

and then browse through 2-3 record the form stops showing data in the
bound fields. This brings me to question; what is the difference between
CStr and tostring that could be causing the above problem?

Thanks

Regards

=======

Function DLookup(ByVal SearchFld As String, ByVal SearchTbl As String,
ByVal SearchCriteria As String) As Object
Dim Cmd As OleDb.OleDbCommand
Dim Reader As OleDb.OleDbDataReader

Cmd = New OleDb.OleDbCommand("SELECT " & SearchFld & " FROM " & SearchTbl
& " WHERE " & SearchCriteria, DBConnection())
Reader = Cmd.ExecuteReader()

If (Reader.Read()) Then
DLookup = Reader.GetValue(0)
Else
DLookup = DBNull.Value
End If

Reader.Close()
Reader = Nothing
Cmd = Nothing
End Function
The main difference is that cstr is a function which is part of the VB
language which depending on the overload will convert an object to a string
while .tostring is a function which any object can override with their own
implementation. The Object implementation will simply return the object
type name so it will never fail while using cstr on objects which cannot be
converted to a string easily will fail.

LS

Jul 3 '08 #4

P: n/a
Lloyd Sheen wrote:
The Object implementation will simply
return the object type name so it will never fail while using cstr on
objects which cannot be converted to a string easily will fail.
....The exception to that being when the object is Nothing, at which point
object.ToString fails with a Null Reference Exception, whereas CStr(object)
simply returns Nothing.

--

(O)enone
Jul 3 '08 #5

P: n/a
John,

To make the confusion even more complicated for you.

ToString is one of the standard members from Object where all classes derive
from in Net. It is an overridable method.

http://msdn.microsoft.com/en-us/libr....tostring.aspx

In the Object.Values (that is where all Values derive from including a
struct) is this method overriden to convert a Value to a string.

Cstr is a function from VB as it is in the Cxx group of functions, which
convert values to other values. The String is a special reference type
threathen as Value, probably that is the reason why the Cstr exist.

In VB I use whenever I need to convert forever the Cxx functions, however
never the Cstr as it is so common in dotNet to use the overridden base
functions like ToString.

Cor
"John" <in**@nospam.infovis.co.ukschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi

I have a WinForm app with a bound form. When user enters a value in field
rateid I lookup the respective rate amount from a table and assign it to
field rate.I am using the DLookup function to achieve this (full code
given below). This function returns a value of type object so I need to
convert it to string. If I use the function as below (using ToString) it
works fine;

Me.txtRate.Text = DLookup(<parameters here>).ToString

but if I use it with a CStr

Me.txtRate.Text = CStr(DLookup(<parameters here>))

and then browse through 2-3 record the form stops showing data in the
bound fields. This brings me to question; what is the difference between
CStr and tostring that could be causing the above problem?

Thanks

Regards

=======

Function DLookup(ByVal SearchFld As String, ByVal SearchTbl As String,
ByVal SearchCriteria As String) As Object
Dim Cmd As OleDb.OleDbCommand
Dim Reader As OleDb.OleDbDataReader

Cmd = New OleDb.OleDbCommand("SELECT " & SearchFld & " FROM " & SearchTbl
& " WHERE " & SearchCriteria, DBConnection())
Reader = Cmd.ExecuteReader()

If (Reader.Read()) Then
DLookup = Reader.GetValue(0)
Else
DLookup = DBNull.Value
End If

Reader.Close()
Reader = Nothing
Cmd = Nothing
End Function
Jul 4 '08 #6

P: n/a

While the VB compiler may recognize the CStr command,
it is eventually comiled to a call to something in
Microsoft.VisualBasic.CompilerServices.Conversions -
and quite often (but not always) the end result comes from a call
to the object's ToString method.

Regards,

Joergen Bech

On Fri, 4 Jul 2008 05:23:37 +0200, "Cor Ligthert[MVP]"
<no************@planet.nlwrote:
>John,

To make the confusion even more complicated for you.

ToString is one of the standard members from Object where all classes derive
from in Net. It is an overridable method.

http://msdn.microsoft.com/en-us/libr....tostring.aspx

In the Object.Values (that is where all Values derive from including a
struct) is this method overriden to convert a Value to a string.

Cstr is a function from VB as it is in the Cxx group of functions, which
convert values to other values. The String is a special reference type
threathen as Value, probably that is the reason why the Cstr exist.

In VB I use whenever I need to convert forever the Cxx functions, however
never the Cstr as it is so common in dotNet to use the overridden base
functions like ToString.

Cor
"John" <in**@nospam.infovis.co.ukschreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
>Hi

I have a WinForm app with a bound form. When user enters a value in field
rateid I lookup the respective rate amount from a table and assign it to
field rate.I am using the DLookup function to achieve this (full code
given below). This function returns a value of type object so I need to
convert it to string. If I use the function as below (using ToString) it
works fine;

Me.txtRate.Text = DLookup(<parameters here>).ToString

but if I use it with a CStr

Me.txtRate.Text = CStr(DLookup(<parameters here>))

and then browse through 2-3 record the form stops showing data in the
bound fields. This brings me to question; what is the difference between
CStr and tostring that could be causing the above problem?

Thanks

Regards

=======

Function DLookup(ByVal SearchFld As String, ByVal SearchTbl As String,
ByVal SearchCriteria As String) As Object
Dim Cmd As OleDb.OleDbCommand
Dim Reader As OleDb.OleDbDataReader

Cmd = New OleDb.OleDbCommand("SELECT " & SearchFld & " FROM " & SearchTbl
& " WHERE " & SearchCriteria, DBConnection())
Reader = Cmd.ExecuteReader()

If (Reader.Read()) Then
DLookup = Reader.GetValue(0)
Else
DLookup = DBNull.Value
End If

Reader.Close()
Reader = Nothing
Cmd = Nothing
End Function
Jul 4 '08 #7

P: n/a
I agree with Patrice I think the problem is occuring whenever you trigger the
DBNull return value. I'm surprised it even works, I get the following error:
"Conversion from type 'DBNull' to type 'String' is not valid."

Changing the line

DLookup = DBNull.Value

to

DLookup = ""

might work, but it might also break code elsewhere that tests for the DBNull
return.

You can get away with .ToString in most cases, but sometimes .ToString will
return a description of the object, rather than the contents of the object
converted to a String. CStr (or CType) is usually more reliable, *IF* you are
sure of your input data type.

--
David Streeter
Synchrotech Software
Sydney Australia
"Patrice" wrote:
CStr can't convert DBNull.Value to a string. It's weird you don't have a
more explicit error...

--
Patrice

"John" <in**@nospam.infovis.co.uka crit dans le message de groupe de
discussion : #F**************@TK2MSFTNGP03.phx.gbl...
Hi

I have a WinForm app with a bound form. When user enters a value in field
rateid I lookup the respective rate amount from a table and assign it to
field rate.I am using the DLookup function to achieve this (full code
given below). This function returns a value of type object so I need to
convert it to string. If I use the function as below (using ToString) it
works fine;

Me.txtRate.Text = DLookup(<parameters here>).ToString

but if I use it with a CStr

Me.txtRate.Text = CStr(DLookup(<parameters here>))

and then browse through 2-3 record the form stops showing data in the
bound fields. This brings me to question; what is the difference between
CStr and tostring that could be causing the above problem?

Thanks

Regards

=======

Function DLookup(ByVal SearchFld As String, ByVal SearchTbl As String,
ByVal SearchCriteria As String) As Object
Dim Cmd As OleDb.OleDbCommand
Dim Reader As OleDb.OleDbDataReader

Cmd = New OleDb.OleDbCommand("SELECT " & SearchFld & " FROM " & SearchTbl
& " WHERE " & SearchCriteria, DBConnection())
Reader = Cmd.ExecuteReader()

If (Reader.Read()) Then
DLookup = Reader.GetValue(0)
Else
DLookup = DBNull.Value
End If

Reader.Close()
Reader = Nothing
Cmd = Nothing
End Function
Jul 7 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.