SUN
SUN

Reputation: 973

Conversion from string to type 'Double' is not valid while updating

I am trying to update table row. Query seems to be ok but don't understand why this error is coming

ERROR -

System.InvalidCastException: Conversion from string "UPDATE hospitals SET votesCount " to type 'Double' is not valid. ---> System.FormatException: Input string was not in a correct format. at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat) --- End of inner exception stack trace --- at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat) at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value) at hospital_details.sendReview_Click(Object sender, EventArgs e) in E:\MY WEB\Health Saviour\website\Website\hospital-details.aspx.vb:line 281

`

Dim hospitalID As String = Request.QueryString("hospitalID")
Dim totalScoreFrom As Integer
Dim currentCount As Integer
Dim newAvgRating As Integer
Dim currentScore As Integer
Dim newVotingCount As Integer
Dim votesGiven As Integer
Dim newCurrentScore As Integer

                    currentCount = totalVotes.Text
                    newVotingCount = (Val(currentCount) + 1)
                    totalScoreFrom = newVotingCount * 6 * 10
                    votesGiven = Val(Mrating2) + Val(Mrating3) + Val(Mrating4) + Val(Mrating5) + Val(Mrating6) + Val(Mrating7)
                    newCurrentScore = Val(currentScore) + Val(votesGiven)
                    newAvgRating = newCurrentScore * 10 / totalScoreFrom
                    'formula for avg rating = currentScore * 10 / totalScroreFrom
                    Dim con As New MySqlConnection
                    Dim query As New MySqlCommand
                    con.ConnectionString = ConfigurationManager _
                    .ConnectionStrings("conio").ConnectionString()
                    query.Connection = con
                    query.CommandText = "UPDATE hospitals SET votesCount = '" + newVotingCount + "', currentAvgRating = '" + newAvgRating + "', totalScoreGiven = '" + newCurrentScore + "' WHERE hospitalID = '" + hospitalID + "'"
                    query.Parameters.AddWithValue("@hospitalID", hospitalID)
                    query.Parameters.AddWithValue("@votesCount", newVotingCount)
                    query.Parameters.AddWithValue("@newAvgRating", newAvgRating)
                    query.Parameters.AddWithValue("@newCurrentScore", newCurrentScore)
                    query.ExecuteNonQuery()
                    con.Close()

Upvotes: 0

Views: 1179

Answers (1)

sujith karivelil
sujith karivelil

Reputation: 29006

In the given snippet you are using the parameters in wrong way. It wont add any parameters to the query, use like the following:

query.CommandText = "UPDATE hospitals SET votesCount = @votesCount," &_ 
                     currentAvgRating = @currentAvgRating," &_ 
                     totalScoreGiven = @totalScoreGiven" &_
                     newCurrentScore =@newCurrentScore WHERE hospitalID = @hospitalID"   
query.Parameters.AddWithValue("@votesCount", newVotingCount)
query.Parameters.AddWithValue("@currentAvgRating ", currentAvgRating)
query.Parameters.AddWithValue("@totalScoreGiven", newAvgRating)
query.Parameters.AddWithValue("@newCurrentScore", newCurrentScore)
query.Parameters.AddWithValue("@hospitalID", hospitalID)

Note : Parameters.Add() will be more suitable in this case, through that you can specify the parameter type too.

Upvotes: 0

Related Questions