Home All Groups Group Topic Archive Search About

A float data type is recognized as varchar

Author
12 Aug 2005 4:34 PM
the friendly display name
Hello,

I am on a c# project right now. If I want to insert a float data type (c#)
into the sql server, I am getting the message then, that a varchar cannot be
inserted into a float. I tried it with money, small money and decimal, too.
And the result is ever the same. (Varchar cannot be entered into a
float/decimal, money etc..)

I am using direct sql command, not a SP.

As example: (simplified, without connection opening, etc.)

In c#:

float variable = 20;

sqlcommandobject.commandtext =  "insert into products (price) VALUES
('"+variable+"')"

Author
12 Aug 2005 4:47 PM
Alejandro Mesa
Why are you putting the value between apostrophes?

> sqlcommandobject.commandtext =  "insert into products (price) VALUES
> ('"+variable+"')"

sqlcommandobject.commandtext =  "insert into products (price) VALUES
(" + variable + ")"


AMB

Show quote
"the friendly display name" wrote:

> Hello,
>
> I am on a c# project right now. If I want to insert a float data type (c#)
> into the sql server, I am getting the message then, that a varchar cannot be
> inserted into a float. I tried it with money, small money and decimal, too.
> And the result is ever the same. (Varchar cannot be entered into a
> float/decimal, money etc..)
>
> I am using direct sql command, not a SP.
>
> As example: (simplified, without connection opening, etc.)
>
> In c#:
>
> float variable = 20;
>
> sqlcommandobject.commandtext =  "insert into products (price) VALUES
> ('"+variable+"')"
>
>
>
Author
12 Aug 2005 4:51 PM
the friendly display name
Wait people, I got it a bit wrong.

The above example works, what doesn't work are floating point numbers.

An integer works.

something like float thing = 20 works.

But, double thing = 20.5 gives the described failure.
Author
12 Aug 2005 4:53 PM
Mike Jansen
Get rid of the single quotes around the value, e.g.

sqlCommandObject.CommandText =
    "INSERT INTO PRODUCTS (price) VALUES (" + variable.ToString() + ")"

Better yet, use parameters in your query so that it will automatically do
the formatting for you, this is also more flexible for data types like
binary, etc. and you don't have to worry about escaping text strings::

sqlCommandObject.CommandText =
    "INSERT INTO PRODUCTS (price) VALUES (@var)";
sqlCommandObject.Parameters.Add("@var", variable);

Mike

"the friendly display name"
<thefriendlydisplayn***@discussions.microsoft.com> wrote in message
Show quote
news:26A40CD8-45CE-4149-BF00-6064A0741640@microsoft.com...
> Hello,
>
> I am on a c# project right now. If I want to insert a float data type (c#)
> into the sql server, I am getting the message then, that a varchar cannot
> be
> inserted into a float. I tried it with money, small money and decimal,
> too.
> And the result is ever the same. (Varchar cannot be entered into a
> float/decimal, money etc..)
>
> I am using direct sql command, not a SP.
>
> As example: (simplified, without connection opening, etc.)
>
> In c#:
>
> float variable = 20;
>
> sqlcommandobject.commandtext =  "insert into products (price) VALUES
> ('"+variable+"')"
>
>
>
Author
12 Aug 2005 5:12 PM
the friendly display name
Thank you.

The parameters solved the problem.


Show quote
"Mike Jansen" wrote:

> Get rid of the single quotes around the value, e.g.
>
> sqlCommandObject.CommandText =
>     "INSERT INTO PRODUCTS (price) VALUES (" + variable.ToString() + ")"
>
> Better yet, use parameters in your query so that it will automatically do
> the formatting for you, this is also more flexible for data types like
> binary, etc. and you don't have to worry about escaping text strings::
>
> sqlCommandObject.CommandText =
>     "INSERT INTO PRODUCTS (price) VALUES (@var)";
> sqlCommandObject.Parameters.Add("@var", variable);
>
> Mike

AddThis Social Bookmark Button