簡體   English   中英

更新時從字符串到類型'Double'的轉換無效

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

我正在嘗試更新表格行。 查詢似乎可以,但是不明白為什么會出現此錯誤

錯誤-

System.InvalidCastException:從字符串“ UPDATE hospitals SET ticketsCount”到類型“ Double”的轉換無效。 ---> System.FormatException:輸入字符串的格式不正確。 在Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value,NumberFormatInfo NumberFormat)處-在Microsoft.VisualBasic處內部異常堆棧跟蹤的結尾-在Microsoft.VisualBasic處E:\\ MY WEB \\ Health Saviour \\ website \\中Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value)的Hospital.details.sendReview_Click(Object sender,EventArgs e)處的.CompilerServices.Conversions.ToDouble(String Value,NumberFormatInfo NumberFormat)網站\\ 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()

在給定的代碼段中,您以錯誤的方式使用了參數。 它不會向查詢添加任何參數,使用方式如下:

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)

注意:在這種情況下, Parameters.Add()會更適合,因為您也可以指定參數類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM