【问题标题】:Conversion from string to type 'Double' is not valid while updating更新时从字符串到类型“Double”的转换无效
【发布时间】:2016-06-29 11:30:10
【问题描述】:

我正在尝试更新表格行。查询似乎没问题,但不明白为什么会出现这个错误

错误 -

System.InvalidCastException:从字符串“更新医院”转换 SET votesCount " 输入 'Double' 无效。---> System.FormatException:输入字符串的格式不正确。在 Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(字符串 值,NumberFormatInfo NumberFormat)在 Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(字符串 Value, NumberFormatInfo NumberFormat) --- 内部异常堆栈结束 跟踪---在 Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(字符串 值,NumberFormatInfo NumberFormat)在 Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(字符串 值)在 hospital_details.sendReview_Click(对象发件人,EventArgs e) 在 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()

【问题讨论】:

  • 您使用的是 +,而不是 &。所以它试图将字符串转换为双精度并将其添加到newVotingCount
  • @ProGrammer 谢谢你的帮助..
  • 您添加参数(这很好)但它们没有用,因为您连接了您的值(这很糟糕)......
  • @the_lotus 不,我只会在最后使用参数
  • @SUN 然后立即使用参数,您的问题就会消失。

标签: asp.net vb.net


【解决方案1】:

在给定的 sn-p 中,您以错误的方式使用参数。它不会向查询中添加任何参数,使用如下:

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()在这种情况下会更合适,通过它你也可以指定参数类型。

【讨论】:

    猜你喜欢
    • 2011-07-30
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    相关资源
    最近更新 更多