Connecting Tech Pros Worldwide Forums | Help | Site Map

Reading money Type Field in DB

Ghost
Guest
 
Posts: n/a
#1: Nov 16 '05
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?



Steve Willcock
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Reading money Type Field in DB


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" <david_dvali@hotmail.com> wrote in message
news:uYK0X2TfEHA.2908@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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?
>
>[/color]


Ghost
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Reading money Type Field in DB


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" <steve@N-O-S-P-A-Mwillcockconsulting.com> wrote in message
news:cf54m7$216$1$8302bc10@news.demon.co.uk...[color=blue]
> Use the C# decimal datatype rather than float - this is compatible with[/color]
the[color=blue]
> Sql Server money datatype.
>
> One other point is that to get the best performance out of the[/color]
sqldatareader[color=blue]
> 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" <david_dvali@hotmail.com> wrote in message
> news:uYK0X2TfEHA.2908@TK2MSFTNGP10.phx.gbl...[color=green]
> > 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"[/color][/color]
type[color=blue][color=green]
> > variable.
> >
> > myCharVar = (char)mySqlDataReader["SomeField"];
> >
> > How do I these assignments?
> >
> >[/color]
>
>[/color]


Steve Willcock
Guest
 
Posts: n/a
#4: Nov 16 '05

re: Reading money Type Field in DB


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" <david_dvali@hotmail.com> wrote in message
news:eycYm$TfEHA.224@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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" <steve@N-O-S-P-A-Mwillcockconsulting.com> wrote in[/color]
message[color=blue]
> news:cf54m7$216$1$8302bc10@news.demon.co.uk...[color=green]
> > Use the C# decimal datatype rather than float - this is compatible with[/color]
> the[color=green]
> > Sql Server money datatype.
> >
> > One other point is that to get the best performance out of the[/color]
> sqldatareader[color=green]
> > 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[/color][/color]
name[color=blue][color=green]
> > but it's faster.
> >
> > --
> > Steve Willcock, MCSD
> > http://www.willcockconsulting.com/
> >
> >
> > "Ghost" <david_dvali@hotmail.com> wrote in message
> > news:uYK0X2TfEHA.2908@TK2MSFTNGP10.phx.gbl...[color=darkred]
> > > 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[/color][/color][/color]
"float"[color=blue][color=green][color=darkred]
> > > 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"[/color][/color]
> type[color=green][color=darkred]
> > > variable.
> > >
> > > myCharVar = (char)mySqlDataReader["SomeField"];
> > >
> > > How do I these assignments?
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Closed Thread