Using Math.Round() on the result of ExecuteScalar(

Results 1 to 2 of 2

Thread: Using Math.Round() on the result of ExecuteScalar(

  1. #1
    Join Date
    Dec 1969

    Default Using Math.Round() on the result of ExecuteScalar(

    I have a VB.NET page that extracts values from one table in a database and inserts them into another table. <BR><BR>I am currently getting the following error:<BR><BR> <BR>Exception Details: System.Reflection.AmbiguousMatchException: No accessible overloaded &#039;Math.Round&#039; can be called without a narrowing conversion.<BR><BR>Source Error:<BR><BR>Line 80: Try<BR>Line 81:<BR>Line 82: pNumAverage = System.Math.Round(cmd2.ExecuteScalar(),1)<BR>Line 83: <BR>Line 84: Finally<BR><BR><BR>Here is the code for cmd2:<BR><BR><BR>cmd2 = new OleDbCommand("SELECT avg(ReviewScore) FROM Reviews WHERE (ReviewID = 132), Objconn)<BR><BR><BR>NOTE: I am almost positive that cmd2.ExecuteScalar() is returning the Integer "7", as it should be.<BR><BR><BR>HELP!

  2. #2
    Join Date
    Dec 1969

    Default ExecuteScalar returns System.Object... does *NOT* return a floating point number, so Math.Round is complaining.<BR><BR>You need to first cast the result of ExecuteScalar to its actualy type (which I presume is Double???) and *THEN* use Round on it.<BR><BR>In VB.NET, I *think* you could simply use CDBL() to do that.<BR><BR>Dim pNumAverage AS Double = Math.Round( CDBL( cmd2.ExecuteScalar() ), 1 )<BR><BR>If that doesn&#039;t work, you&#039;ll likely have to use CTYPE or maybe CONVERT.<BR><BR>**********<BR><BR>Wait a minute!!! I just read your NOTE!!!<BR><BR>If ExecuteScalar is returning an Integer, what POSSIBLE point is there in calling Round???? By definition, an integer is *already* rounded!!<BR><BR>So just<BR> Dim pNumAverage As Integer = CInt( cmd2.ExecuteScalar() )<BR><BR>Again, I *think* that CINT() is good enough with VB.NET. If not, there is always CTYPE and CONVERT.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts