3

enter image description here

i use the following code to map data from sql reader to C# Objects. the Power is of data type float in both code and sql database .. y is the cast error message occuring?

private Entity.PanelDetail MapDataReader(SqlDataReader dataReader)
    {
        Entity.PanelDetail panelDetail = new Entity.PanelDetail();
        panelDetail.IdPanelDetail = DataReaderExtensions.GetStringOrNull(dataReader, "idPanelDetail");
        panelDetail.IdDeviceDetail = DataReaderExtensions.GetStringOrNull(dataReader, "idDeviceDetail");
        panelDetail.Power = DataReaderExtensions.GetFloatOrNull(dataReader, "Power");
        panelDetail.Current = DataReaderExtensions.GetFloatOrNull(dataReader, "Current");
        panelDetail.Length = DataReaderExtensions.GetFloatOrNull(dataReader, "Length");
        panelDetail.Width = DataReaderExtensions.GetFloatOrNull(dataReader, "Width");
        panelDetail.CreatedBy = DataReaderExtensions.GetStringOrNull(dataReader, "CreatedBy");
        panelDetail.CreationDate = DataReaderExtensions.GetDateTimeOrNull(dataReader, "CreationDate");
        panelDetail.ModifiedBy = DataReaderExtensions.GetStringOrNull(dataReader, "ModifiedBy");
        panelDetail.ModifiedDate = DataReaderExtensions.GetDateTimeOrNull(dataReader, "ModifiedDate");
        panelDetail.IsActive = DataReaderExtensions.GetBoolOrNull(dataReader, "IsActive");
        panelDetail.IsDeleted = DataReaderExtensions.GetBoolOrNull(dataReader, "IsDeleted");

        return panelDetail;
    }
Sana.91
  • 1,999
  • 4
  • 33
  • 52
  • possible duplicate of [Why is a SQL float different from a C# float](http://stackoverflow.com/questions/122523/why-is-a-sql-float-different-from-a-c-sharp-float) – Leo Feb 07 '14 at 04:42

3 Answers3

13

My guess that the value is being returned as a boxed double instead of float. Try to use

 (float) dataReader.GetDouble(fieldOrdinal);

See Mapping CLR Parameter Data

Sameer
  • 2,143
  • 1
  • 16
  • 22
  • that means i will also have to change all attributes to double in my codE? – Sana.91 Feb 07 '14 at 04:42
  • so if i change all attributes to double and am using GetDoubleOrNull then i dun have to use type cast: (float)dataReader.GetDouble(fieldOrdinal) ryt? – Sana.91 Feb 07 '14 at 04:49
  • i just tried .. now its giving the following error: An exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll but was not handled in user code – Sana.91 Feb 07 '14 at 04:58
  • check your fieldOrdinal. – Sameer Feb 07 '14 at 05:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/46987/discussion-between-sameer-and-sana-91) – Sameer Feb 07 '14 at 05:44
  • Sameer, thanks a lot for the guidance... the problem got solved :) – Sana.91 Feb 08 '14 at 13:05
6

I would make a good bet that the data type being return is actually a (boxed) double.

Update: Actually I just found that an SQL float maps to a .NET double on MSDN (so yes this is your problem): http://msdn.microsoft.com/en-us/library/ms131092.aspx

Try this as a test:

(float)dataReader.GetDouble(fieldOrdinal);

Timeout
  • 7,759
  • 26
  • 38
  • @Sana.91 Yes, I would recommend that. – Timeout Feb 07 '14 at 04:42
  • then should i use get float or null, or Get double or null method? because data type in my database are floats – Sana.91 Feb 07 '14 at 04:44
  • @Sana.91 Use GetDouble or null. It's a float in the database when when it reaches .NET it's a double. You can see for yourself in the link I posted how it maps. – Timeout Feb 07 '14 at 04:45
  • so if i change all attributes to double and am using GetDoubleOrNull then i dun have to use type cast: (float)dataReader.GetDouble(fieldOrdinal) ryt? – Sana.91 Feb 07 '14 at 04:49
  • 1
    @Sana.91 No then just stick with double. No need for float. – Timeout Feb 07 '14 at 05:14
3

The reason is that SQL Float is double in .net. You can see the complete mapping here. Therefore, as others have suggested you need to read double and then try to cast it to float.

(float)dataReader.GetDouble("Power");
Ehsan
  • 31,833
  • 6
  • 56
  • 65