Error converting data type nvarchar to float

匿名 (未验证) 提交于 2019-12-03 01:27:01

问题:

I am getting the following error in my code.

Error converting data type nvarchar to float.

My code on button click is

 protected void btnFind_Click(object sender, EventArgs e)     {          SqlParameter LatParam;         SqlParameter LngParam;       if (zipcode.Text != "")     {         litAddress.Text = "";         litAddress1.Text = "";         string addressstring = zipcode.Text;           string connstring = "Data Source=win2008-2;Initial Catalog=h1tm11;User ID=sa;Password=#1cub3123*;Persist Security Info=True;";           string SQL1 = "SELECT *, 6371.01 * ACOS( SIN( @lat*PI()/180 ) * SIN( store_lat*PI()/180 ) + COS( @lat*PI()/180 ) * COS( store_lat*PI()/180 ) * COS( (store_long*PI()/180) - (@lng*PI()/180) ) ) AS distance from storelocator where 6371.01 * ACOS( SIN( @lat*PI()/180 ) * SIN( store_lat*PI()/180 ) + COS( @lng*PI()/180 ) * COS( store_lat*PI()/180 ) * COS( (store_long*PI()/180) - (@lng*PI()/180) ) ) < '" + ddl_distance.SelectedItem.Value + "' order by distance asc;";          SqlConnection conn = new SqlConnection(connstring);          conn.Open();         SqlCommand comm = new SqlCommand(SQL1, conn);         LatParam = new SqlParameter();         LatParam.ParameterName = "@lat";         LatParam.SqlDbType = SqlDbType.NVarChar;         LatParam.Value =  "select lat from tbl_pincode where codes='" + zipcode.Text + "';";          LngParam = new SqlParameter();         LngParam.ParameterName = "@lng";         LngParam.SqlDbType = SqlDbType.NVarChar;         LngParam.Value ="select lat from tbl_pincode where  codes='" + zipcode.Text + "';";            comm.Parameters.Add(LatParam);         comm.Parameters.Add(LngParam);              SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);               while (reader.Read())             {                 string area = reader["lat"].ToString();                 string codes =reader["lng"].ToString();                    litAddress.Text += area;                 litAddress1.Text += codes;                 }        } 

The datatype in the table are nvarchar for latitude,longitude and pincodes.

Thanks,

回答1:

So I think your error is with your select statement.

You are trying to perform calculations on nvarchar values. You either need to change the data types, or perform a Cast within your select statement.

For example...

CAST (lat AS float) 

A section from your select statement for example should be...

ACOS( SIN( CAST ((@lat) AS float) * PI() / 180 ) 

NOTE: With the Cast method you will need to make sure that all you data values are numeric so there will be no cast exceptions



回答2:

Your @lat etc contain string values, which happen to contain TSQL, but the server doesn't care about that - the server just considers it as a string. You then perform math operations, @lat*PI()/180, where @lat is a string like select lat from tbl_pincode where codes=...blah.... The server does not evaluate them - they are just strings.

What you should do, is something like:

declare @lat numeric, @long numeric -- replace with correct data types select @lat = [lat], @long = [long] from tbl_pincode where codes=@zipcode -- your math work here 

and add a parameter called zipcode to the command, with the desired value.



回答3:

Because you're multiplying a nvarchar type parameter by Pi.

And even if the query was correct, you would still be multiplying a latitude or longitude value (which you say also are of type nvarchar) by Pi.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!