473,396 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Newbie question: Cstr vs toString

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
7 2747
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
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

"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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ken H | last post by:
Hi I have a question about architecting solutions.. I have a part of a project which requires me to track person details (name, addresses, etc... Should I be creating Person objects, Address...
9
by: Bill L. | last post by:
We recently noticed that the vb.net CStr function yields different results than the vb6 version when converting SQL decimal data. If, for example, the data is defined in SQL as decimal(19,10), the...
101
by: Sean | last post by:
Book I am reading says that Cstr() is best method for efficency and safety however it doesnt compare that method of the .ToString() method. Which is best. Thanks
4
by: Joseph | last post by:
Hi all- I am a former VB6 programmer and new at C# and I have a question dealing with converting some code from VB6 to C#. The code is below and essentially, what it does is gets data from a SQL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.