Lamloumi Afif
Lamloumi Afif

Reputation: 9081

Casting a float attribut with ado.net and razor

I have an asp.net mvc application in which i have this function :

public List<FichierModels> GetFichiersFromDirectoy(int _id_dir)
{
    List<FichierModels> l = new List<FichierModels>();
    Data.Connect();
    using (Data.connexion)
    {
        string queryString = @"select Id, Id_directory, Url, Upload_date, Last_download, Downloads_count, Taille, Expiration_date, Name from Fichier where Id_directory = @Id_directory ";
        SqlCommand command = new SqlCommand(queryString, Data.connexion);
        command.Parameters.AddWithValue("@Id_directory", _id_dir);
        try
        {
            SqlDataReader reader = command.ExecuteReader();
            do
            {

                while (reader.Read())
                {
                    FichierModels fichier = new FichierModels();
                    fichier.Id = reader.GetInt32(0); 
                        fichier.Id_directory = reader.GetInt32(1);
                        fichier.Url = reader.GetString(2); 
                        fichier.Upload_date = reader.GetDateTime(3); 
                        fichier.Last_download = reader.GetDateTime(4);
                        fichier.Downloads_count = reader.GetInt32(5); 
                        fichier.Taille = reader.GetFloat(6); 
                        fichier.Expiration_date = reader.GetDateTime(7);
                        fichier.Name = reader.GetString(8) ;
                    l.Add(fichier);

                }
            } while (reader.NextResult());

            return l;

        }
        catch { return null; }

    }
}

schema The problem is that an exception of casting in the line fichier.Taille = reader.GetFloat(6); however the attribute taille is float and in the class Fichier also.but always an exception of incorrect cast. record Why this happens? how can i fix it?

Upvotes: 1

Views: 540

Answers (2)

Major Byte
Major Byte

Reputation: 4101

An MSSQL float should be a double in C#. The default precision of a float in MSSQL is double precision (when a column is defined as float it is actually a float(53) ), while a float in C# always has single precision.

Upvotes: 2

Lamloumi Afif
Lamloumi Afif

Reputation: 9081

The float type in Sql server is the Double type in C# http://msdn.microsoft.com/en-us/library/cc716729.aspx

Upvotes: 1

Related Questions