Problem with adding reply to Forum thread. Help please

Topics: Help, Request
Jan 29, 2008 at 7:45 PM
For some reason my club web site throws the following error when a reply is posted to a thread. I am using ver 3 beta 1. Other sites using this seem to work ok. I have not carried out any mods to the code. The error being thrown is:
The INSERT statement conflicted with the FOREIGN KEY constraint "FKMembersThreadsThreads". The conflict occurred in database "westmidlandsvsoc", table "dbo.Threads", column 'threadid'.
The statement has been terminated.
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.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FKMembersThreadsThreads". The conflict occurred in database "westmidlandsvsoc", table "dbo.Threads", column 'threadid'.
The statement has been terminated.

Source Error:


Line 108:
Line 109: Public Shared Sub MarkThreadRead(ByVal threadid As Integer, ByVal memberid As Guid)
Line 110: SPs.MarkThreadRead(threadid, memberid).Execute()
Line 111: End Sub
Line 112:


Source File: C:\Users\Chris\Desktop\Documents\Visual Studio 2005\Projects\Club Site 7088 2008\ClubStarterKit.Web\App_Code\BLL\Forum.vb Line: 110

Stack Trace:


[SqlException (0x80131904): The INSERT statement conflicted with the FOREIGN KEY constraint "FK_MembersThreads_Threads". The conflict occurred in database "westmidlandsvsoc", table "dbo.Threads", column 'threadid'.
The statement has been terminated.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +859322
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +736198
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +188
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1959
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +149
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +903
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +132
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +415
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +135
SubSonic.SqlDataProvider.ExecuteQuery(QueryCommand qry) in C:\SubSonic 2.0.3\src\SubSonic\DataProviders\SqlDataProvider.cs:196
SubSonic.StoredProcedure.Execute() in C:\SubSonic 2.0.3\src\SubSonic\ActiveRecord\StoredProcedure.cs:152
ClubStarterKit.Web.Forum.MarkThreadRead(Int32 threadid, Guid memberid) in C:\Users\Chris\Desktop\Documents\Visual Studio 2005\Projects\Club Site 7088 2008\ClubStarterKit.Web\App_Code\BLL\Forum.vb:110
ASP.forumaddpostaspx.UpdateButton_Click(Object sender, EventArgs e) in C:\Users\Chris\Desktop\Documents\Visual Studio 2005\Projects\Club Site 7088 2008\ClubStarterKit.Web\Forum\AddPost.aspx:102
System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +105
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +174
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102

Does anyone have any suggestions?
Feb 2, 2008 at 10:22 AM
Edited Feb 2, 2008 at 10:27 AM
I have investigated further and have tracked it down to the following code in 'Forum/AddPost.aspx'

Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim uGuid As Guid = New Guid(Membership.GetUser.ProviderUserKey.ToString())

>...............................................................................................ooOOOoo......................................................

ClubStarterKit.Web.Forum.MarkThreadRead(newmsgthreadid, uGuid)
ClubStarterKit.Web.Forum.SetThreadWatch(newmsgthreadid, uGuid, CheckBoxWatch.Checked)

Response.Redirect("thread.aspx?th=" & newmsgthreadid.ToString, True)
End Sub

The parameter uGuid in the call to 'MarkThreadRead' highlighted in bold is 'Empty' at this point.

I have tried replacing the first line of the function:
Dim uGuid As Guid = New Guid(Membership.GetUser.ProviderUserKey.ToString())

with this:
Dim guidString As String = Membership.GetUser.ProviderUserKey.ToString()
Dim uGuid As Guid = New Guid(guidString)

but uGuid still returns 'Empty'

Does anyone have any suggestions on how this can be fixed?
Feb 4, 2008 at 2:44 AM
It's not clear if your problem is related to mine, which is that the forum works ok, but the blog doesn't allow carriage returns. When pressing "enter", the cursor stays on the same line, so there's no way to create paragraphs. Is it designed that way? It's the latest release running on Windows Server 2008, and this is something I've not noticed in other programs. Thanks for any possible help on this.
Feb 18, 2008 at 2:33 PM
When you do a quick watch on a guid it will always say empty, add the .tostring after the guid in quickwatch and it will display the GUID.

I have nailed this issue down to a problem with the BLL forum.vb line 101.

If you call sp.exectue its returning the number of lines that have been effected. Not the thread number.

I resolved this by chaniging the SP AddMessage to Select the @threadID then rather than calling execute I called exectue scalar.

-- Ammended BLL

Dim sp As StoredProcedure = SPs.AddMessage(msg.parentmsgid, msg.memberid, msg.body, msg.subject, AdminOnly, msg.topicid)
Dim threadid As Integer = sp.ExecuteScalar()

---Ammended SP Code

ALTER PROCEDURE dbo.AddMessage
@parentmsgid int,
@memberid uniqueidentifier,
@body ntext,
@subject nvarchar(255),
@admin bit,
@topicid int
AS
declare @threadid int

IF @parentmsgid = 0
BEGIN
INSERT INTO Threads (lastpostdate, lastpostby, topicid, admin) values(GetDate(), @memberid, @topicid, @admin)
SELECT @threadid = IDENT_CURRENT('Threads')
END
else
BEGIN
SELECT @threadid = threadid FROM Messages WHERE id = @parentmsgid
UPDATE Threads SET lastpostdate = GetDate(), lastpostby = @memberid WHERE threadid = @threadid
END

INSERT INTO Messages
(memberid, subject, body, threadid, parentmsgid, answer)
VALUES (@memberid,@subject,@body,@threadid,@parentmsgid, 0)

select @threadid







Mar 28, 2008 at 10:30 PM
Hi,

I too would appreciate some help with this issue. I've tried to follow the fix above and now as admin I can create, delete and edit forum posts with no errors, but if I try and view a post when not logged in I get :-

Object reference not set to an instance of an object.
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 266:
Line 267: Private Function IsSelf(ByVal messageid As Integer) As Boolean
Line 268: Dim CurrentGUID As Guid = Membership.GetUser().ProviderUserKey()
Line 269: Dim OrigionalMessageGUID As Guid = ClubStarterKit.Web.Forum.GetMemGuidByMsg(messageid)
Line 270:

Source File: G:\My Documents\Visual Studio 2005\WebSites\Clubs2\Forum\Thread.aspx Line: 268

Stack Trace:

NullReferenceException: Object reference not set to an instance of an object.
ASP.forumthreadaspx.IsSelf(Int32 messageid) in G:\My Documents\Visual Studio 2005\WebSites\Clubs2\Forum\Thread.aspx:268
ASP.forumthreadaspx.CanEdit(Int32 messageid) in G:\My Documents\Visual Studio 2005\WebSites\Clubs2\Forum\Thread.aspx:144
ASP.forumthreadaspx._DataBinding_control18(Object sender, EventArgs e) in G:\My Documents\Visual Studio 2005\WebSites\Clubs2\Forum\Thread.aspx:356
System.Web.UI.Control.OnDataBinding(EventArgs e) +99
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +206
System.Web.UI.Control.DataBind() +12
System.Web.UI.Control.DataBindChildren() +204
System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +216
System.Web.UI.Control.DataBind() +12
System.Web.UI.WebControls.Repeater.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem) +130
System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource) +454
System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +53
System.Web.UI.WebControls.Repeater.DataBind() +72
System.Web.UI.WebControls.Repeater.EnsureDataBound() +55
System.Web.UI.WebControls.Repeater.OnPreRender(EventArgs e) +12
System.Web.UI.Control.PreRenderRecursiveInternal() +86
System.Web.UI.Control.PreRenderRecursiveInternal() +170
System.Web.UI.Control.PreRenderRecursiveInternal() +170
System.Web.UI.Control.PreRenderRecursiveInternal() +170
System.Web.UI.Control.PreRenderRecursiveInternal() +170
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2041

I guess because the GUID is zero? I'm rather new to asp so would appreciate any help on how to fix this error.

Cheers.
Apr 15, 2008 at 2:59 AM
"Dim sp As StoredProcedure = SPs.AddMessage(msg.parentmsgid, msg.memberid, msg.body, msg.subject, AdminOnly, msg.topicid)
Dim threadid As Integer = sp.ExecuteScalar()"...martinbeeby

Where do I put this code? Inside "MarkThreadRead"?