473,621 Members | 2,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(<parame ters here>).ToString

but if I use it with a CStr

Me.txtRate.Text = CStr(DLookup(<p arameters 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.OleDbComm and
Dim Reader As OleDb.OleDbData Reader

Cmd = New OleDb.OleDbComm and("SELECT " & SearchFld & " FROM " & SearchTbl
& " WHERE " & SearchCriteria, DBConnection())
Reader = Cmd.ExecuteRead er()

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 2761
CStr can't convert DBNull.Value to a string. It's weird you don't have a
more explicit error...

--
Patrice

"John" <in**@nospam.in fovis.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(<parame ters here>).ToString

but if I use it with a CStr

Me.txtRate.Text = CStr(DLookup(<p arameters 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.OleDbComm and
Dim Reader As OleDb.OleDbData Reader

Cmd = New OleDb.OleDbComm and("SELECT " & SearchFld & " FROM " & SearchTbl
& " WHERE " & SearchCriteria, DBConnection())
Reader = Cmd.ExecuteRead er()

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.in fovis.co.ukwrot e in message
news:%2******** ********@TK2MSF TNGP03.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(<parame ters here>).ToString

but if I use it with a CStr

Me.txtRate.Text = CStr(DLookup(<p arameters 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.OleDbComm and
Dim Reader As OleDb.OleDbData Reader

Cmd = New OleDb.OleDbComm and("SELECT " & SearchFld & " FROM " & SearchTbl
& " WHERE " & SearchCriteria, DBConnection())
Reader = Cmd.ExecuteRead er()

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.in fovis.co.ukwrot e in message
news:%2******** ********@TK2MSF TNGP03.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(<parame ters here>).ToString

but if I use it with a CStr

Me.txtRate.Text = CStr(DLookup(<p arameters 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.OleDbComm and
Dim Reader As OleDb.OleDbData Reader

Cmd = New OleDb.OleDbComm and("SELECT " & SearchFld & " FROM " & SearchTbl
& " WHERE " & SearchCriteria, DBConnection())
Reader = Cmd.ExecuteRead er()

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.in fovis.co.ukschr eef in bericht
news:%2******** ********@TK2MSF TNGP03.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(<parame ters here>).ToString

but if I use it with a CStr

Me.txtRate.Text = CStr(DLookup(<p arameters 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.OleDbComm and
Dim Reader As OleDb.OleDbData Reader

Cmd = New OleDb.OleDbComm and("SELECT " & SearchFld & " FROM " & SearchTbl
& " WHERE " & SearchCriteria, DBConnection())
Reader = Cmd.ExecuteRead er()

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.Visua lBasic.Compiler Services.Conver sions -
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.in fovis.co.ukschr eef in bericht
news:%2******* *********@TK2MS FTNGP03.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.Tex t = DLookup(<parame ters here>).ToString

but if I use it with a CStr

Me.txtRate.Tex t = CStr(DLookup(<p arameters 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.OleDbComm and
Dim Reader As OleDb.OleDbData Reader

Cmd = New OleDb.OleDbComm and("SELECT " & SearchFld & " FROM " & SearchTbl
& " WHERE " & SearchCriteria, DBConnection())
Reader = Cmd.ExecuteRead er()

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.in fovis.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(<parame ters here>).ToString

but if I use it with a CStr

Me.txtRate.Text = CStr(DLookup(<p arameters 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.OleDbComm and
Dim Reader As OleDb.OleDbData Reader

Cmd = New OleDb.OleDbComm and("SELECT " & SearchFld & " FROM " & SearchTbl
& " WHERE " & SearchCriteria, DBConnection())
Reader = Cmd.ExecuteRead er()

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
1528
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 objects etc as Business layer classes and then have an data access layer which actually save / updates / delete to and from the DB.. If so, and bearing in mind the 'easiest' way to pass data between the layers would be with a dataset - should my BL...
9
7705
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 vb.net CStr function will return ten digits to the right of the decimal, regardless of the data value. In this case, if the data value is 5, the vb.net CStr function will return 5.0000000000. The vb6 CStr function will simply return 5. In other...
101
19183
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
1506
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 Server database and parses some of the data and puts the parsed data into a text field. I omitted a some of the code and left the data parsing part of it, which is what my question is about. Exactly what the code below does is gets data from SQL...
0
8213
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8156
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8653
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8457
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6101
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2587
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1460
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.