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

Re: Newbie question: Cstr vs toString by Patrice

Patrice
Thu Jul 03 10:32:53 CDT 2008

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

--
Patrice

"John" <info@nospam.infovis.co.uk> a écrit dans le message de groupe de
discussion : #FIy65R3IHA.4272@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
>


Re: Newbie question: Cstr vs toString by John

John
Thu Jul 03 10:53:05 CDT 2008

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

Thanks

Regards

"John" <info@nospam.infovis.co.uk> wrote in message
news:%23FIy65R3IHA.4272@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
>



Re: Newbie question: Cstr vs toString by Lloyd

Lloyd
Thu Jul 03 12:43:57 CDT 2008


"John" <info@nospam.infovis.co.uk> wrote in message
news:%23FIy65R3IHA.4272@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


Re: Newbie question: Cstr vs toString by \(O\)enone

\(O\)enone
Thu Jul 03 17:02:21 CDT 2008

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



Re: Newbie question: Cstr vs toString by Cor

Cor
Thu Jul 03 22:23:37 CDT 2008

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/library/system.object.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" <info@nospam.infovis.co.uk> schreef in bericht
news:%23FIy65R3IHA.4272@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
>


Re: Newbie question: Cstr vs toString by Joergen

Joergen
Fri Jul 04 03:07:36 CDT 2008


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]"
<notmyfirstname@planet.nl> wrote:

>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/library/system.object.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" <info@nospam.infovis.co.uk> schreef in bericht
>news:%23FIy65R3IHA.4272@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
>>


Re: Newbie question: Cstr vs toString by surturz

surturz
Sun Jul 06 23:32:00 CDT 2008

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" <info@nospam.infovis.co.uk> a crit dans le message de groupe de
> discussion : #FIy65R3IHA.4272@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
> >
>