Can anyone help me explain why when I rate my items the actual average rating is not being updated???
(I used the rating + comments demo as the foundation for this code)
(I used the rating + comments demo as the foundation for this code)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Get ratings and display on page
Dim tbRatings As DataTable = New IMCSelect().getRatings(-1, 8, Request.QueryString("ID"), 1, 0, Application("logMaterials")).Tables(0)
Dim Sum As Single
For Each dr As DataRow In tbRatings.Rows
If dr.RowState <> DataRowState.Deleted Then
Sum += Convert.ToInt32(dr("Rating"))
End If
Next
Dim iAvgRating As Single = 0
If tbRatings.Rows.Count <> 0 Then
iAvgRating = Math.Round(Sum / tbRatings.Rows.Count, 2)
lblAvgRating.Text = iAvgRating
End If
Dim tbComments As DataTable = New IMCSelect().getRatings(-1, 8, Request.QueryString("ID"), 1, 0, Application("logPublications")).Tables(0)
Dim comments = From r In tbComments.AsEnumerable Where r.Field(Of String)("RatingComments") <> "" Select r
If comments.Count() <> 0 Then
tbComments = comments.CopyToDataTable()
Else
tbComments.Rows.Clear()
End If
hlComments.Text = "View comments"
hlComments.NavigateUrl = "Javascript:ViewComments(" & Request.QueryString("ID") & ");"
If tbComments.Rows.Count() <> 0 Then
divComments.Style.Add("display", "block;")
Else
divComments.Style.Add("display", "none;")
End If
End Sub
Private Property Rating() As Dictionary(Of String, Decimal)
Get
Dim varRating As Dictionary(Of String, Decimal) = DirectCast(ViewState("Rating"), Dictionary(Of String, Decimal))
If [Object].Equals(varRating, Nothing) Then
varRating = New Dictionary(Of String, Decimal)()
varRating("sum") = 0
varRating("counter") = 0
End If
Return varRating
End Get
Set(ByVal value As Dictionary(Of String, Decimal))
ViewState("Rating") = value
End Set
End Property
Protected Sub btnPostComment_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim iRatingID As Integer
iRatingID = New IMCInsert().addRating(RadRating1.Value, txtComments.Text, 8, Request.QueryString("ID"), Session("RBPOnline_UserID"), Application("logMaterials"))
txtComments.Text = ""
'Close tooltip
Dim str As String = "CloseToolTip1();"
ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(Page), "closeTooltip", str, True)
End Sub
Protected Sub RadRating1_Rate(ByVal sender As Object, ByVal e As EventArgs)
Dim currentRating As Dictionary(Of String, Decimal) = Rating
currentRating("sum") = currentRating("sum") + RadRating1.Value
currentRating("counter") = currentRating("counter") + 1
Rating = currentRating
Dim averageRating As Decimal = Math.Round(currentRating("sum") / currentRating("counter"), 1)
End Sub