LinqDataSource has no values to insert

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.

Advertisements

One Response to LinqDataSource has no values to insert

  1. […] Raymond Little Just another WordPress.com site Skip to content HomeAbout LinqDataSource has no values to insert → […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: