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,
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…
A section from your select statement for example should be…
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