ulikechicken
New member
- Joined
- Mar 22, 2005
- Messages
- 1
Hi, Im trying to adapt a rating system i found on the web at codeproject.com in order to rate websites on an RSS feed website. I have set up a database on sql server, with 2 tables:
RatingSystem - which has ratingId, rating, ip and feedId
Feeds - which has feedId and feedName
I have the following .net code:
However I am getting an Object reference not set to an instance of an object error.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 23: dtrMyDataReader = cmdMyCommand.ExecuteReader()
Line 24: dtrMyDataReader.Read()
Line 25: If dtrKb("RatingCount") <> 0 Then
Line 26: lblRatingCount.Text = " / " & dtrKb("RatingCount")
Line 27: intApprovalWidth = dtrKb("RatingSum")/dtrKb("RatingCount")*15
If anyone can help then I will be eternally grateful because its had me stumped for a week now.
Thanks to anyone who looks
Ulikechicken
RatingSystem - which has ratingId, rating, ip and feedId
Feeds - which has feedId and feedName
I have the following .net code:
Code:
<html>
<head>
<script runat="server">
Private intPageID As Long =1
Sub Page_Load(Src As Object, E As EventArgs)
Dim intApprovalWidth, intBlankWidth as Integer
Dim strQuery as string
Dim strCon As String
Dim conMyConnection as New System.Data.SqlClient.sqlConnection()
Dim cmdMyCommand as New System.Data.SqlClient.SqlCommand()
Dim dtrMyDataReader as System.Data.SqlClient.sqlDataReader
Dim dtrKb as System.Data.SqlClient.sqlDataReader
If Not IsPostback Then
strCon = System.Configuration.ConfigurationSettings.AppSett ings("MyConnectionString")
conMyConnection.ConnectionString = strCon
strQuery = "SELECT SUM(rating) As RatingSum, COUNT(*) As RatingCount "
strQuery += "FROM RatingSystem WHERE feedId=" & intPageID
conMyConnection.Open()
cmdMyCommand.Connection = conMyConnection
cmdMyCommand.CommandType = System.Data.CommandType.Text
cmdMyCommand.CommandText = strQuery
dtrMyDataReader = cmdMyCommand.ExecuteReader()
dtrMyDataReader.Read()
If dtrKb("RatingCount") <> 0 Then
lblRatingCount.Text = " / " & dtrKb("RatingCount")
intApprovalWidth = dtrKb("RatingSum")/dtrKb("RatingCount")*15
intBlankWidth = 75 - intApprovalWidth
imgRatingApproval.Width = System.web.ui.webcontrols.unit.pixel(intApprovalWi dth)
imgRatingBlank.Width = System.web.ui.webcontrols.unit.pixel(intBlankWidth )
Else
lblRatingCount.Text = "/0"
lblRating.Text = "Be the first to rate it!"
End If
dtrMyDataReader.Close()
conMyConnection.Close()
End If
End Sub
Sub btnRating_Click(Src As Object, E As EventArgs)
Variable declarations...
Dim intApprovalWidth, intBlankWidth as Integer
Dim strSelectQuery, strInsertQuery as string
Dim strCon As String
Dim conMyConnection as New System.Data.SqlClient.sqlConnection()
Dim cmdMyCommand as New System.Data.SqlClient.SqlCommand()
Dim dtrMyDataReader as System.Data.SqlClient.sqlDataReader
Dim dtrKb as System.Data.SqlClient.sqlDataReader
Dim MyHttpAppObject As System.Web.HttpContext = System.Web.HttpContext.Current
Dim strRemoteAddress as String
Dim intSelectedRating, intCount As Integer
Get the users ip address and cast its type to string...
strRemoteAddress = Cstr(MyHttpAppObject.Request.UserHostAddress)
Build the query string...
strSelectQuery = "SELECT COUNT(*) As RatingCount "
strSelectQuery += "FROM RatingSystem WHERE feedId=" & intPageID
strSelectQuery += " AND ip = " & strRemoteAddress
Open the connection, and execute the query...
strCon = System.Configuration.ConfigurationSettings.AppSett ings("MyConnectionString")
conMyConnection.ConnectionString = strCon
conMyConnection.Open()
cmdMyCommand.Connection = conMyConnection
cmdMyCommand.CommandType = System.Data.CommandType.Text
cmdMyCommand.CommandText = strSelectQuery
intCount= cmdMyCommand.ExecuteScalar()
conMyConnection.Close()Close the connection to release these resources...
If intCount = 0 Then The user hasnt rated the article before, so perform the insert...
strInsertQuery = "INSERT INTO ratings (rating, ip, feedId) "
strInsertQuery += "VALUES ("
strInsertQuery += intSelectedRating & ", "
strInsertQuery += strRemoteAddress & ", "
strInsertQuery += intPageId & "); "
cmdMyCommand.CommandText = strInsertQuery
conMyConnection.Open()
cmdMyCommand.ExecuteNonQuery()
conMyConnection.Close()
Else The user has rated the article before, so display a message...
lblRating.Text = "Youve already rated this article"
End If
strSelectQuery = "SELECT SUM(rating) As RatingSum, COUNT(*) As RatingCount "
strSelectQuery += "FROM RatingSystem WHERE FeedId=" & intPageID
conMyConnection.Open()
cmdMyCommand.CommandText = strSelectQuery
dtrMyDataReader = cmdMyCommand.ExecuteReader()
dtrMyDataReader.Read()
lblRatingCount.Text = " / " & dtrKb("RatingCount")
intApprovalWidth = dtrKb("RatingSum")/dtrKb("RatingCount")*15
dtrMyDataReader.Close()
conMyConnection.Close()
intBlankWidth = 75 - intApprovalWidth
imgRatingApproval.Width = System.web.ui.webcontrols.unit.pixel(intApprovalWi dth)
imgRatingBlank.Width = System.web.ui.webcontrols.unit.pixel(intBlankWidth )
End Sub
</script>
</head>
<body>
<table width="0%" border="0" class="basic11pt">
<tr>
<td colspan="4">Rating:
<asp:image ID="imgRatingApproval" runat="server"></asp:image>
<asp:image ID="imgRatingBlank" runat="server"></asp:image>
<asp:label ID="lblRatingCount" runat="server"></asp:label>
</td>
</tr>
<tr>
<td>poor</td>
<td><asp:radiobuttonlist RepeatLayout="Flow"
RepeatDirection="Horizontal"
ID="rblRating"
runat="server">
<asp:listitem Value="1">1</asp:listitem>
<asp:listitem Value="2">2</asp:listitem>
<asp:listitem Value="3" selected="True">3</asp:listitem>
<asp:listitem Value="4">4</asp:listitem>
<asp:listitem Value="5">5</asp:listitem>
</asp:radiobuttonlist></td>
<td> great</td>
<td> <asp:button ID="btnRating"
OnClick="btnRating_Click"
runat="server"
Text="Rate It!" /></td>
</tr>
<tr>
<td colspan="4"><asp:label ID="lblRating" runat="server"></asp:label> </td>
</tr>
</table>
</body>
</html>
However I am getting an Object reference not set to an instance of an object error.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 23: dtrMyDataReader = cmdMyCommand.ExecuteReader()
Line 24: dtrMyDataReader.Read()
Line 25: If dtrKb("RatingCount") <> 0 Then
Line 26: lblRatingCount.Text = " / " & dtrKb("RatingCount")
Line 27: intApprovalWidth = dtrKb("RatingSum")/dtrKb("RatingCount")*15
If anyone can help then I will be eternally grateful because its had me stumped for a week now.
Thanks to anyone who looks
Ulikechicken