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!
And here is the HTML:
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
>