473,385 Members | 1,606 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,385 software developers and data experts.

Reading money Type Field in DB

Hello.
I have some problem to read Money Type Field from my database.
I do so:
I'm using SqlDataReader object to select some records and I have "float"
type variable to store result.
I write:

myFloatVar = (float)mySqlDataReader["SomeField"];

When this line executed the cast exception occurs.

The same problem I have when I'm try to assign record value to "char" type
variable.

myCharVar = (char)mySqlDataReader["SomeField"];

How do I these assignments?
Nov 16 '05 #1
3 18426
Use the C# decimal datatype rather than float - this is compatible with the
Sql Server money datatype.

One other point is that to get the best performance out of the sqldatareader
you should use the Getxxxx methods where possible - e.g.

decimal d = (decimal)mySqlDataReader["SomeField"];

decimal d = mySqlDataReader.GetDecimal(2);

Using this method you need to know the column ordinal rather than the name
but it's faster.

--
Steve Willcock, MCSD
http://www.willcockconsulting.com/
"Ghost" <da*********@hotmail.com> wrote in message
news:uY**************@TK2MSFTNGP10.phx.gbl...
Hello.
I have some problem to read Money Type Field from my database.
I do so:
I'm using SqlDataReader object to select some records and I have "float"
type variable to store result.
I write:

myFloatVar = (float)mySqlDataReader["SomeField"];

When this line executed the cast exception occurs.

The same problem I have when I'm try to assign record value to "char" type
variable.

myCharVar = (char)mySqlDataReader["SomeField"];

How do I these assignments?

Nov 16 '05 #2
How about this problem:
The same problem I have when I'm try to assign record value to "char" type
variable.
myCharVar = (char)mySqlDataReader["SomeField"];

"Steve Willcock" <st***@N-O-S-P-A-Mwillcockconsulting.com> wrote in message
news:cf*******************@news.demon.co.uk...
Use the C# decimal datatype rather than float - this is compatible with the Sql Server money datatype.

One other point is that to get the best performance out of the sqldatareader you should use the Getxxxx methods where possible - e.g.

decimal d = (decimal)mySqlDataReader["SomeField"];

decimal d = mySqlDataReader.GetDecimal(2);

Using this method you need to know the column ordinal rather than the name
but it's faster.

--
Steve Willcock, MCSD
http://www.willcockconsulting.com/
"Ghost" <da*********@hotmail.com> wrote in message
news:uY**************@TK2MSFTNGP10.phx.gbl...
Hello.
I have some problem to read Money Type Field from my database.
I do so:
I'm using SqlDataReader object to select some records and I have "float"
type variable to store result.
I write:

myFloatVar = (float)mySqlDataReader["SomeField"];

When this line executed the cast exception occurs.

The same problem I have when I'm try to assign record value to "char" type variable.

myCharVar = (char)mySqlDataReader["SomeField"];

How do I these assignments?


Nov 16 '05 #3
That depends what datatype "SomeField" is in the here - if it's a char or
varchar Sql Server datatype then use the C# string datatype as follows:

string s = (string)mySqlDataReader["SomeField"];
or
string s = mySqlDataReader.GetString(2);

Steve

"Ghost" <da*********@hotmail.com> wrote in message
news:ey*************@TK2MSFTNGP10.phx.gbl...
How about this problem:
The same problem I have when I'm try to assign record value to "char" type
variable.
myCharVar = (char)mySqlDataReader["SomeField"];

"Steve Willcock" <st***@N-O-S-P-A-Mwillcockconsulting.com> wrote in message news:cf*******************@news.demon.co.uk...
Use the C# decimal datatype rather than float - this is compatible with

the
Sql Server money datatype.

One other point is that to get the best performance out of the

sqldatareader
you should use the Getxxxx methods where possible - e.g.

decimal d = (decimal)mySqlDataReader["SomeField"];

decimal d = mySqlDataReader.GetDecimal(2);

Using this method you need to know the column ordinal rather than the name
but it's faster.

--
Steve Willcock, MCSD
http://www.willcockconsulting.com/
"Ghost" <da*********@hotmail.com> wrote in message
news:uY**************@TK2MSFTNGP10.phx.gbl...
Hello.
I have some problem to read Money Type Field from my database.
I do so:
I'm using SqlDataReader object to select some records and I have "float" type variable to store result.
I write:

myFloatVar = (float)mySqlDataReader["SomeField"];

When this line executed the cast exception occurs.

The same problem I have when I'm try to assign record value to "char"

type variable.

myCharVar = (char)mySqlDataReader["SomeField"];

How do I these assignments?



Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing the code. Thank you. .. Facundo
1
by: Stephane Charette | last post by:
QUICK VERSION: How do I create an index on a field of type "MONEY"? ----------------------------- LONG VERSION: I have a table with a field of type "money". I very often need to access...
4
by: digitalavatar | last post by:
I am having a problem with my Access 2000 front end to a SQL 7 or 2000 database. I create a table in SQL with a field named, say, amt, data type MONEY. When I link the table in Access using ODBC,...
2
by: Alberto | last post by:
I have a decimal field in a SQL Server DataBase who stores money and I want to show it in a TextBox and in a ListView controls in C#. If I do: txtMoney.Text = Convert.ToSingle(dr); I see...
4
by: ahaupt | last post by:
Hi all, I'm trying to get a null value into a sql money field through a SqlCommand in c#. However, I get this lovely message (which sparks loads of happy emotions in this overworked body): ...
0
by: Sam | last post by:
I am trying to use a Simple form with 3 fields from SQL NorthWind Database (Order Details Table with 3 Fields. - OrderId, ProductId and Unit Price). The Field Unit Price has a data type of 'Money...
0
by: Sam | last post by:
Folks.. I am trying to use a Simple form with 3 fields from SQL NorthWind Database (Order Details Table with 3 Fields. - OrderId, ProductId and Unit Price). The Field Unit Price has a data...
15
by: soni2926 | last post by:
Hi, I have the following: float.Parse(myproduct.Price.Value.ToString()); myproduct.Price.Value.ToString() returns $24.00 (with the $) Is there anyway to do the above cast, I know the...
4
by: Ronald S. Cook | last post by:
We're designing the data model for a project. The app will be in .NET, the database will be in SQL Server 2005. I'm a little confused on type conversion between the two and which I should...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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...

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.