I am trying to do a select and getting this error:  Error converting data type nvarchar to float.

westdh
westdh used Ask the Experts™
on
error msg:
Exception Details: System.Data.SqlClient.SqlException: Error converting data type nvarchar to float.
----------------
both Latitude & longitude are db float values
----------------

    <asp:SqlDataSource ID="dsPopulateExtWA" runat="server" ConnectionString="<%$ ConnectionStrings:SiteSqlServer %>"
                SelectCommand="SELECT DISTINCT  [State] + ' ' + [City] + ' ' + [Ext2] + ' ' + [Ext3]  + ' ' + Latitude
 + ' ' + Longitude AS MyDataWA
                 FROM [trip4usdnn].[trip4usdnn].[Netism_MapExtreme_Locations] where state = 'WA'"></asp:SqlDataSource>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Manoj PatilSr. Software Engineer

Commented:
Try this

"SELECT DISTINCT  [State] + ' ' + [City] + ' ' + [Ext2] + ' ' + [Ext3]  + ' ' + CASE ISNUMERIC(Latitude) WHEN 1 THEN CAST(Latitude AS float) ELSE null END + ' ' + CASE ISNUMERIC(Longitude) WHEN 1 THEN CAST(Longitude AS float) ELSE null END  AS MyDataWA
FROM [trip4usdnn].[trip4usdnn].[Netism_MapExtreme_Locations] where state = 'WA'"



Author

Commented:
Iam getting the same error:

Error converting data type nvarchar to float.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Error converting data type nvarchar to float.

Manoj PatilSr. Software Engineer

Commented:
Is there any null data or which is not float or can't covert into float.... have u checked the DB Table
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Author

Commented:
No Nulls:

all I am doing is selecting the data and populating a dropdownlist?
Sr. Software Engineer
Commented:
ok

try this, this can solve your problem
DECLARE @Latitude AS VARCHAR(50)

SET @Latitude = '123.340'

SET @Latitude = Replace(@Latitude,'.','')
SET @Latitude = Replace(@Latitude,',','.')

--Perform the casting
SELECT Cast(@Latitude AS FLOAT)

--or

--Perform the conversion
SELECT Convert(FLOAT,@value)

Open in new window

Manoj PatilSr. Software Engineer

Commented:

Author

Commented:
Thanks Much

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial