2016-06-29 26 views
0

テーブル行を更新しようとしています。クエリは大丈夫のようですが、このエラーが文字列から型への変換 'Double'は更新中無効です

ERROR来ている理由を理解していない -

System.InvalidCastExceptionの:「ダブル」は有効ではありません入力した文字列「UPDATE病院 SETのvotesCount」からの変換を。 ---> System.FormatException:入力文字列の形式が正しくありませんでした。 Microsoft.VisualBasic.CompilerServices.Conversions.ToDoubleで Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(文字列 値、のNumberFormatInfoのNumberFormat)(文字列 値、のNumberFormatInfoのNumberFormat)で---内部例外スタックトレース の終わり - - Eでhospital_details.sendReview_Clickで Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(文字列 値)で Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(文字列 値のNumberFormatInfoのNumberFormat)(オブジェクト送信者、のEventArgs e)に:\ MY WEB \健康 救い主\ウェブサイト\ウェブサイト\ hospital-details.aspx.vb:行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() 
+2

+、&を使用していません。したがって、文字列をdoubleに変換してnewVotingCountに追加しようとしています –

+0

@ProGrammer助けてくれてありがとう – SUN

+0

パラメータを追加しても良いですが、値を連結しているので使い物になりません。 .. –

答えて

0

あなたは間違った方法でパラメータを使用しているスニペット。それは文句を言わないクエリにパラメータを追加し、次のように使用します。

query.CommandText = "UPDATE hospitals SET votesCount = @votesCount," &_ 
        currentAvgRating = @currentAvgRating," &_ 
        totalScoreGiven = @totalScoreGiven" &_ 
        newCurrentScore [email protected] 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) 

注:Parameters.Add()が、この場合は、より適しているであろうことを通じてあなたも、パラメータの種類を指定することができます。

関連する問題