This is a migrated thread and some comments may be shown as answers.

Displays average instead of rating

5 Answers 94 Views
Rating
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 16 Dec 2014, 01:21 PM
Hello,

Everything seems to be working except that I have two labels and I'm getting the wrong rating in my "Your rating is" label. The average rating works fine and displays in the second label. Also, the stars display the correct average on page load. But as soon as I choose a rating, it displays the AVERAGE of the average and what I rated.

For example: If there is an average of 3.6 on page load and then I rate a 5, it will display 3.642857 in the "Your rating is" label.

I have posted the code below. Any help would be greatly appreciated!
Imports System
Imports System.Collections.Generic
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
 
Imports Telerik.Web.UI
Imports System.Data
Imports System.Data.SqlClient
 
Partial Public Class _2014
    Inherits System.Web.UI.Page
 
    Protected Sub RadRating_Rate(sender As Object, e As EventArgs)
        Dim rating As RadRating = DirectCast(sender, RadRating)
        If rating.ID = "RadRating1" Then
 
            Label1.Text = "Your Rating is: " + "<span style='color:Red'>" + RadRating1.Value.ToString + "</span>"
 
            AddRating(RadRating1.Value, "Amanda", 20141)
        End If
 
        If rating.ID = "RadRating2" Then
 
        End If
 
        If rating.ID = "RadRating3" Then
 
        End If
        If rating.ID = "RadRating4" Then
 
        End If
 
    End Sub
 
 
    Public Sub AddRating(ByVal rateVal As Object, ByVal picName As String, ByVal picId As Integer)
 
        Dim cnstr2 = New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString())
        Dim cmd2 = New System.Data.SqlClient.SqlCommand("INSERT INTO Ratings(Rating,CreateDate,name,PicID) VALUES (@rating, @date , @name, @picID)", cnstr2)
        cmd2.CommandType = System.Data.CommandType.Text
        cmd2.Parameters.AddWithValue("@rating", rateVal)
        cmd2.Parameters.AddWithValue("@date", Date.Now)
        cmd2.Parameters.AddWithValue("@name", picName)
        cmd2.Parameters.AddWithValue("@picID", picId)
        'cmd2.Parameters.AddWithValue("@ip", ip)
 
        cnstr2.Open()
 
        cmd2.ExecuteNonQuery()
        cnstr2.Close()
 
    End Sub
 
    Public Function GetCount(ByVal picID As Integer) As Decimal
        Dim theCount As Decimal = 0.0
 
        'Dim currentAverageRating As Object = Vehicles.Rows(dataItem.ItemIndex + RadGrid1.CurrentPageIndex * RadGrid1.PageSize)(2)
        Dim cnstr2 = New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString())
        Dim rdr As SqlDataReader
        Dim cmd2 = New System.Data.SqlClient.SqlCommand("SELECT AVG(Rating) AS Total FROM Ratings  WHERE PicID = @picID ", cnstr2)
        cmd2.CommandType = System.Data.CommandType.Text
        cmd2.Parameters.AddWithValue("@picID", picID)
        cnstr2.Open()
 
        rdr = cmd2.ExecuteReader
 
        If rdr.HasRows Then
 
            rdr.Read()
            If Not IsDBNull(rdr.GetValue(rdr.GetOrdinal("Total"))) Then
                theCount = rdr.GetValue(rdr.GetOrdinal("Total"))
            End If
        End If
        rdr.Close()
 
        cnstr2.Close()
        Return theCount
    End Function
 
 
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
 
        RadRating1.DbValue = GetCount(20141)
 
        'Label1.Text = "Your Rating is: " + "<span style='color:Red'>" + RadRating1.Value.ToString + "</span>"
        Label2.Text = "Average Rating is: " + "<span style='color:Red'>" + Convert.ToString(Format(GetCount(20141), "#.#")) + "</span>"
    End Sub
 
 
End Class

And here is the HTML:
<telerik:RadRating ID="RadRating1" Precision="Item" Value="0" ItemCount="5" runat="server" AutoPostBack="true" OnRate="RadRating_Rate">
                        </telerik:RadRating>
                        <asp:Label ID="Label1" runat="server" ForeColor="Green"></asp:Label>
                        <asp:Label ID="Label2" runat="server" ForeColor="Green"></asp:Label>

5 Answers, 1 is accepted

Sort by
0
Ianko
Telerik team
answered on 19 Dec 2014, 08:08 AM
Hi Andrew,

The same matter is discussed in this forum thread. I suggest following the information provided there.

Regards,
Ianko
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Andrew
Top achievements
Rank 1
answered on 19 Dec 2014, 01:29 PM
lanko,

Thank you for your help. I actually have decided to restructure the project and make it fully dynamic, similar to this example. But we are having trouble getting it to save the rating to the database. I have everything inside of a listview as you will see below. It shows the average correctly.

My apologies for the complete change in direction. It just seemed easier to go dynamic. Thank you again for all your help!

<asp:ListView ID="ListView1" runat="server" DataKeyNames="img_pk" DataSourceID="SqlDataSource3">
                            <ItemTemplate>
                                <div class="col-xs-12 col-sm-4 placeholder">
                                     <asp:Label runat="server" ID="lblPicId" Text='<%#Eval("img_id")%>' Visible="false" />
                                      <asp:Label runat="server" ID="lblPicName" Text='<%#Eval("img_name")%>' Visible="false" />
                                    <img class="img-responsive" src='<%# Eval("img_url")%>' alt='<%# Eval("img_name")%>' />
                                    <telerik:RadRating ID="RadRating1" Precision="Item" ItemCount="5" runat="server" AutoPostBack="true" OnRate="RadRating_Rate"
                                        DbValue='<%# GetCount(Eval("img_id"))%>'>
                                    </telerik:RadRating>
                                    <asp:Label ID="Label1" runat="server" ForeColor="Green" Text='<%#Convert.ToString(Format(GetCount(Eval("img_id")), "#.#"))%>'></asp:Label>
                                    <h4><%# Eval("img_name")%></h4>
                                </div>
                            </ItemTemplate>
                        </asp:ListView>

Imports System
Imports System.Collections.Generic
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
 
Imports Telerik.Web.UI
Imports System.Data
Imports System.Data.SqlClient
 
Partial Public Class _2014
    Inherits System.Web.UI.Page
 
    Protected Sub RadRating_Rate(sender As Object, e As EventArgs)
        Dim rating As RadRating = DirectCast(sender, RadRating)
        'Dim radRate1 As RadRating = CType(repeater1.FindControl("RadRating1"), RadRating)
        'Dim radRate2 As RadRating = CType(formView2.FindControl("RadRating2"), RadRating)
        Dim lblPicID As Label = CType(ListView1.FindControl("lblPicId"), Label)
        Dim lblPicName As Label = CType(ListView1.FindControl("lblPicName"), Label)
        AddRating(rating.Value, lblPicName.Text, lblPicID.Text)
 
        'If rating.ID = "RadRating2" Then
 
        '   AddRating(radRate2.Value, "Amber", 20142)
        'End If
 
        'If rating.ID = "RadRating3" Then
        '    AddRating(radRate2.Value, "Amber", 20142)
        'End If
        'If rating.ID = "RadRating4" Then
 
        'End If
 
    End Sub
 
 
    Public Sub AddRating(ByVal rateVal As Object, ByVal picName As String, ByVal picId As Integer)
 
        Dim cnstr2 = New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString())
        Dim cmd2 = New System.Data.SqlClient.SqlCommand("INSERT INTO Ratings(Rating,CreateDate,name,PicID) VALUES (@rating, @date , @name, @picID)", cnstr2)
        cmd2.CommandType = System.Data.CommandType.Text
        cmd2.Parameters.AddWithValue("@rating", rateVal)
        cmd2.Parameters.AddWithValue("@date", Date.Now)
        cmd2.Parameters.AddWithValue("@name", picName)
        cmd2.Parameters.AddWithValue("@picID", picId)
        'cmd2.Parameters.AddWithValue("@ip", ip)
 
        cnstr2.Open()
 
        cmd2.ExecuteNonQuery()
        cnstr2.Close()
 
    End Sub
 
    Public Function GetCount(ByVal picID As Integer) As Decimal
        Dim theCount As Decimal = 0.0
 
        Dim cnstr2 = New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString())
        Dim rdr As SqlDataReader
        Dim cmd2 = New System.Data.SqlClient.SqlCommand("SELECT AVG(Rating) AS Total FROM Ratings WHERE PicID = @picID ", cnstr2)
        cmd2.CommandType = System.Data.CommandType.Text
        cmd2.Parameters.AddWithValue("@picID", picID)
        cnstr2.Open()
 
        rdr = cmd2.ExecuteReader
 
        If rdr.HasRows Then
 
            rdr.Read()
            If Not IsDBNull(rdr.GetValue(rdr.GetOrdinal("Total"))) Then
                theCount = rdr.GetValue(rdr.GetOrdinal("Total"))
            End If
        End If
        rdr.Close()
 
        cnstr2.Close()
        Return theCount
    End Function
 
 
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
 
    End Sub
 
    Protected Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
 
        'If ListView1.SelectedIndex > 0 Then
 
 
        '    Dim radRate1 As RadRating = CType(ListView1.FindControl("RadRating1"), RadRating)
        '    Dim lblPicID As Label = CType(ListView1.FindControl("lblPicId"), Label)
        '    Dim lblPicName As Label = CType(ListView1.FindControl("lblPicName"), Label)
 
 
        '    AddRating(radRate1.Value, lblPicName.Text, lblPicID.Text)
 
        'End If
 
 
    End Sub
End Class
0
Ianko
Telerik team
answered on 23 Dec 2014, 12:59 PM
Hi Andrew,

I am afraid that only by inspecting the code I am unable to identify what might be wrong. Therefore, it would be more appropriate if you could provide a sample that could be ran locally, so that I could test it. 

Also, the demo illustrates the approach to do that quite well and I can only make assumptions what is the exact difficulty that is encountered.

Regards,
Ianko
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Andrew
Top achievements
Rank 1
answered on 23 Dec 2014, 04:51 PM
Lanko,

My apologies. I am very new to developing in general but I'm very lucky to be able to work with developers here. We were able to get it working after all but I still do have a few minor issues.

As you can see, I am using a session to disable the ratings after someone places their vote but I need to be able to adjust the length of the session. Also, for some reason, on my local version you are able to vote on one photo after the other without refreshing. But on the live version as soon as you vote on one photo, all the other ratings become disabled until you refresh the page. Here is the link to a zip file of everything but the photos. It should still work fine. If you need them let me know.

Again, I appreciate all your help. Let me know if there is anything else you need.
0
Ianko
Telerik team
answered on 24 Dec 2014, 01:25 PM

Hi Andrew,

Using Session in the application is a matter rather related to general ASP.NET knowledge and it is not to the RadRating control. Due to that I would advise you to find proper information in relevant resources like these ones:


Regards,

Ianko
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Rating
Asked by
Andrew
Top achievements
Rank 1
Answers by
Ianko
Telerik team
Andrew
Top achievements
Rank 1
Share this question
or