Search code examples
c#sql-serverasp-classicvarchar

SQL Server varchar(max) and varchar incompatible


I have a big problem..

I'm using this to perform a research that measure the distance (latitude and longitude) of an address (inserted through a textbox), and if the distance is < 0.86....

In addition I'm using offset to perform a paging.

public static List<Galleria> GetRistorante(int page, string key, string lon)
{
    Galleria Galleria = new Galleria(); 
    string im = string.Empty;

    int items = 10;
    int offset = (page - 1) * items;

    List<Galleria> lst = new List<Galleria>();

    using (SqlConnection sqlConn = new SqlConnection(DB.GetConnectionString()))
    {
        using (SqlCommand sqlComm = new SqlCommand())
        {
            sqlConn.Open();
            sqlComm.Connection = sqlConn;

            sqlComm.CommandText = @"SELECT R.* , C.* FROM dbo.Ristorante R INNER JOIN CategoriaRistorante C ON C.Id = R.CategoriaRistoranteId                                         
            HAVING (SQRT(POWER(69.1 * (R.lat - @key), 2) + POWER(69.1 * (@lon - R.lon) * COS(R.lat / 57.3), 2)) ) < 0.8699197
            ORDER BY R.Id OFFSET @offset ROWS FETCH NEXT @items ROWS ONLY";

            SqlParameter s1 = new SqlParameter();
            s1.SqlDbType = System.Data.SqlDbType.Int;
            s1.ParameterName = "@offset";
            s1.Value = offset;

            sqlComm.Parameters.Add(s1);

            SqlParameter s2 = new SqlParameter();
            s2.SqlDbType = System.Data.SqlDbType.Int;
            s2.ParameterName = "@items";
            s2.Value = items;

            sqlComm.Parameters.Add(s2);

            SqlParameter s3 = new SqlParameter();
            s3.SqlDbType = System.Data.SqlDbType.VarChar;
            s3.ParameterName = "@key";
            s3.Value = string.Format("%{0}%", Convert.ToDouble(key));

            sqlComm.Parameters.Add(s3);

            SqlParameter s4 = new SqlParameter();
            s4.SqlDbType = System.Data.SqlDbType.VarChar;
            s4.ParameterName = "@lon";
            s4.Value = string.Format("%{0}%", Convert.ToDouble(lon));

            sqlComm.Parameters.Add(s4);

            SqlDataReader reader = sqlComm.ExecuteReader();

            while (reader.Read())
            {
                Galleria = new Galleria();
                Galleria.Id = Convert.ToInt32(reader["Id"]);
                Galleria.Nome = reader["Nome"].ToString();
                Galleria.Info = reader["Info"].ToString();
                Galleria.Immagine = reader["Immagine"].ToString();
                Galleria.Telefono = reader["Telefono"].ToString();
                Galleria.Cap = Convert.ToInt32(reader["Cap"]);           
                Galleria.Via = reader["Via"].ToString();
                Galleria.Citta = reader["Citta"].ToString();

                lst.Add(Galleria);
            }

            return lst;
        }
    }
}

Now when I try this method, I get this error:

enter image description here

Initially the query worked, but when I added the "having" it gives me this error.

P.S. Latitude and Longitude are varchar(max) in the database


Solution

  • Try casting it.

    HAVING (SQRT(POWER(69.1 * (CAST(R.lat AS DECIMAL(9,6)) - @key), 2) + POWER(69.1 * (@lon - CAST(R.lon AS DECIMAL(9,6))) * COS(CAST(R.lat AS DECIMAL(9,6)) / 57.3), 2)) ) < 0.8699197
    

    However as mentioned in above comments I would recommend using decimal field itself as this casting is going to be a performance hit.