ulikechicken Posted March 22, 2005 Posted March 22, 2005 Hi, I'm 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: <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 user's 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 hasn't 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 = "You've 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 Quote
fizzled Posted March 22, 2005 Posted March 22, 2005 Dim [color=red]dtrMyDataReader[/color] as System.Data.SqlClient.sqlDataReader Dim [color=green]dtrKb[/color] as System.Data.SqlClient.sqlDataReader If Not IsPostback Then strCon = System.Configuration.ConfigurationSettings.AppSettings("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 [color=red]dtrMyDataReader[/color] = cmdMyCommand.ExecuteReader() [color=red]dtrMyDataReader[/color].Read() If [color=green]dtrKb[/color]("RatingCount") <> 0 Then I see where you assign dtrMyDataReader, but I don't see you assign dtrKb anywhere before you use it in the if statement. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.