Ask a questionAsk a question
 

AnswerException when query returns a float

  • Friday, November 06, 2009 8:02 PMChristopher Pisz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I haven't done too much with .NET and databases, but I am pretty sure my code should work when comparing it to online examples and MSDN. For some reason I get an exception "cannot convert" when trying to extract a float from the data reader. Am I missing some nuance?

    When I make a SQL query in the data designer and hit properties on the field of the table I am working with, it claims it is a float with size 8 and precision of 53.
    I can excecute the query fine there.

             float reviewTestScore;
    
             // Retreive from EEDO DB
             SqlConnection mssqlConnection = new SqlConnection(m_eedoDBConnectionString);
             SqlCommand mssqlCommand = mssqlConnection.CreateCommand();
    
             string query = "SELECT TOP(1) c.SCORE ";
             query +=       "FROM USER_TBL AS u INNER JOIN COURSE_COMPLETIONS AS c ON u.USER_ID = c.USER_ID ";
             query +=       "WHERE (u.USER_ID = @userID) AND (c.COURSE_ID = @reviewCourseID) ";
             query +=       "ORDER BY c.COURSE_END_TIME DESC";
             mssqlCommand.CommandText = query;
    
             mssqlCommand.Parameters.AddWithValue("@userID", userID);
             mssqlCommand.Parameters.AddWithValue("@reviewCourseID", reviewCourseID);
    
             try
             {
                mssqlConnection.Open();
                SqlDataReader mssqlReader = mssqlCommand.ExecuteReader();
    
                if (mssqlReader.Read())
                {
                   reviewTestScore = mssqlReader.GetFloat(0);   // EXCEPTION THROWN HERE!!!
                }
                else
                {
                   // Error, no record returned
                   return false;
                }
             }
             catch (Exception error)
             {
                return false;
             }
    
             mssqlConnection.Close();
    



Answers

  • Monday, November 09, 2009 6:12 AMYichun_FengMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Christopher,

     
    The float’ precision you defined in DB is 15 digits. However, the float’s precision in .NET Framework is 7 digits.
    Because Double type in .NET Framework can be 15 digits. The float type in DB is parsed as Double in .NET Framework. You can use GetDouble for this scenario.

    You can also try to use mssqlReade(0) to access the value directly.

     

    Does this work for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

All Replies

  • Saturday, November 07, 2009 4:25 PMtechnocrat_aspire Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    hi,

    Can you try changing it to

    reviewTestScore = (float) mssqlReader.GetFloat(0);   


    I know syntactically it doesnt make too much sense but worth giving it a try.

  • Monday, November 09, 2009 6:12 AMYichun_FengMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Christopher,

     
    The float’ precision you defined in DB is 15 digits. However, the float’s precision in .NET Framework is 7 digits.
    Because Double type in .NET Framework can be 15 digits. The float type in DB is parsed as Double in .NET Framework. You can use GetDouble for this scenario.

    You can also try to use mssqlReade(0) to access the value directly.

     

    Does this work for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.