Error creating the same task multiple times in state machine workflow

October 20, 2011

I created my first state machine workflow the other day and just when I thought everything was working as expected I encountered a really strange error. I went for a state machine workflow as I had to deal with the same item being changed numerous times and send emails and create tasks depending on one of the values on the list item. From the reading I had done on state machine workflows this seemed like the perfect fit for the requirements.

I found some articles on how to create the workflow and was surprised at how easy it was, ok my workflow was very simple but I can see the advantages of this approach. This was until I started getting errors when testing the various different use cases. Before I go into the actual error its important to first give some background into the actual workflow.

When an item is added to a list the workflow is initiated and the Initial State is executed. The next state has an event driven activity and within this I have a ‘OnWorkflowItemChanged’ activity in it so I can detect when the item has been modified. I then have an if else branch with 4 possible options with different tasks in each of the branches. In the ‘OnWorkflowItemChanged’ activity I perform some calculations which are used in the branching logic.

What I was finding was everything was working as expected first time around, however if I had already created a task within a and then encountered the same situation rather than creating a new task the workflow was failing. The details in the workflow history on the list item is not helpful in the slightest and it can be difficult to locate details in the ULS log but eventually I found the error below.

Workflow Error

System.InvalidOperationException: Correlation value on declaration "longTermSickToken" is already initialized.

Which I noticed this I looked around on Google and soon found it was because I had set the OwnerActivityName to the name of the workflow. This made sense as I guess these have to be unique so I changed the OwnerActivityName to be the name of the state but I found the same issues so next I changed it to be the name of the Event Driven Activity within my state and this corrected the problem.

I am sure people who are familiar with state machine workflows will think this obvious but it was an interesting learning curve and one I am determined not to repeat hence this article


LinqDataSource has no values to insert

February 1, 2011

As promised in my first post the issued described in this post was what inspired me to start blogging as I was unable to find much information around similar issues.

I encountered the issue described in this blog when updating my wedding website. Before getting into the details of the error I will first give some background information into the original functionality and the changes that resulted in the error occurring.

When developing my wedding website one of the key requirements I identified was the ability for users to add comments about the wedding or the website. The idea being to promote conversation among users to increase their use of the site. When I was initially building the site I quickly identified I didn’t want anonymous users to be able to add comments as this could result in inappropriate comments or language. To ensure this did not happen I put in place two safeguards. Firstly, I created the comments page within the secure guests section of the website that only allows logged in users who are in a certain role access. Secondly, I added functionality to ensure all comments must first be reviewed and then approved by an authorized user.

Using this approach I was confident that I could prevent any unauthorized or unwelcome comments. After further consideration I decided while this two-pronged approach to comments was secure I decided it may affect users interaction with the site and it may be better for the comments to be on the publically accessible section of the site. After considering the options I implemented an approach that allowed anonymous users to see comments but only grant logged in users in a specific group access to add comments. This offered the visibly and ease of use for authorised users but also allowed people browsing the site to view comments. The solution I implemented was to add a login view control within the insert item template of the listview that was being used to render the comments.

Before change:

<InsertItemTemplate>
<div>
<div>
Comment:
<tinymce:TextArea ID=”tinyTextArea” runat=”server” Value='<%# Bind(“Name”) %>’ />
CustomValidator ValidationGroup=”InsertItem” ID=”CfvComment” runat=”server”
ErrorMessage=”Please enter a comment” OnServerValidate=”CfvComment_ServerValidate”>CustomValidator>
<div>
<asp:Button ID=”InsertButton” CssClass=”purpleButton” ValidationGroup=”InsertItem”
runat=”server” CommandName=”Insert” Text=”Insert” />
<asp:Button ID=”CancelButton” CssClass=”purpleButton” runat=”server” CommandName=”Cancel”
Text=”Clear” />
</div>
</div>
</InsertItemTemplate>

After change:

<InsertItemTemplate>
LoginView ID=”LvComments” runat=”server” EnableViewState=”false”>
<AnonymousTemplate>
<div>
In order to add any comments you must first <a href=”/Account/Login.aspx”>Log In</a>
or if you have not already created an account please <a href=”/Account/Register.aspx”>
Register</a> to get full access to the site, including the ability to choose
your menu options and RSVP.
</div>
</AnonymousTemplate>
<LoggedInTemplate>
<div>
<div>
Comment:
<tinymce:TextArea ID=”tinyTextArea” runat=”server” />
<asp:CustomValidator ValidationGroup=”InsertItem” ID=”CfvComment” runat=”server”
ErrorMessage=”Please enter a comment” OnServerValidate=”CfvComment_ServerValidate”></asp:CustomValidator></div>
<div>
<asp:Button ID=”InsertButton” CssClass=”purpleButton” ValidationGroup=”InsertItem”
runat=”server” CommandName=”Insert” Text=”Insert” />
<asp:Button ID=”CancelButton” CssClass=”purpleButton” runat=”server” CommandName=”Cancel”
Text=”Clear” />
</div>
</div>
</LoggedInTemplate>
</asp:LoginView>
</InsertItemTemplate>

The other crucial factor in this example is the fact that I am using a LinqDataSource to populate the listview and insert the details into the database. Prior to implementing the change above I was populating the data to be inserted in the database in the ‘LinqDataSource1_Inserting’ method, however afterwards I started getting the error below:

Exception information:
Exception type: InvalidOperationException
Exception message: LinqDataSource ‘LinqDataSource1’ has no values to insert. Check that the ‘values’ dictionary contains values.

While debugging the issue on my development environment I found I was unable to hit any break points in the ‘LinqDataSource1_Inserting’ method. Further testing found I was able to hit the ‘LsvComments_ItemInserting’ method before getting the error thrown by the LinqDataSource. I then adjusted my code to use the ‘LsvComments_ItemInserting’ to populate the details to be stored in the database and get around this issue, however I would be very interested to hear other people’s experience around this.


%d bloggers like this: