Update and Insert problems in ListView

S

shapper

Hello,

I created a ListView with a LinqDataSource and a DataPager. I have two
problems:

1. When I insert a record I get the following error:

LinqDataSource 'ldsTags' has no values to insert. Check that the
'values' dictionary contains values.

2. When I update a record the value is updated but if I click the
browser's Refresh button immediately after I click the Update button I
get the following error:

Row not found or changed.

I have been all weekend trying to solve these problems but I can't
find a way.

Could someone, please, help me out?

I am posting all my code.

Thank You,

Miguel

ASPX

<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="ListViewRuntime.aspx.vb"
Inherits="ListViewRuntime" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>List View Runtime</title>
</head>
<body>
<form id="fListViewRuntime" runat="server">
<asp:placeHolder ID="phListViewRuntime" runat="server"></
asp:placeHolder>
</form>
</body>
</html>

ASPX.VB

Public Enum TemplateType
AlternatingItemTemplate
EditItemTemplate
EmptyDataTemplate
EmptyItemTemplate
GroupSeparatorTemplate
GroupTemplate
InsertItemTemplate
ItemSeparatorTemplate
ItemTemplate
LayoutTemplate
SelectedItemTemplate
End Enum

Partial Class ListViewRuntime
Inherits System.Web.UI.Page

Protected WithEvents ldsTags As New LinqDataSource
Protected WithEvents lvTags As New ListView

Protected Sub Page_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles Me.Init

phListViewRuntime.Controls.Add(ldsTags)
phListViewRuntime.Controls.Add(lvTags)

End Sub

Private Sub ldsTags_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles ldsTags.Init

With ldsTags
.ContextTypeName = "CodeDataContext"
.EnableDelete = True
.EnableInsert = True
.EnableUpdate = True
.ID = "ldsTags"
.TableName = "Tags"
End With

End Sub

Private Sub ldsTags_Updating(ByVal sender As Object, ByVal e As
LinqDataSourceUpdateEventArgs) Handles ldsTags.Updating

Dim tag As Tag = CType(e.NewObject, Tag)
Dim tb As TextBox = CType(Code.Base.Common.FindControl(lvTags,
"updatetext"), TextBox)
tag.Text = tb.Text

End Sub

Private Sub ldsTags_Inserting(ByVal sender As Object, ByVal e As
LinqDataSourceInsertEventArgs) Handles ldsTags.Inserting

Dim tag As Tag = CType(e.NewObject, Tag)
Dim tb As TextBox = CType(Code.Base.Common.FindControl(lvTags,
"inserttext"), TextBox)
tag.Text = tb.Text

End Sub

Private Sub lvTags_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles lvTags.Init

With lvTags
.DataKeyNames = New String() {"TagID"}
.DataSourceID = "ldsTags"
.ID = "lvTags"
.InsertItemPosition = InsertItemPosition.FirstItem
End With

With lvTags
.AlternatingItemTemplate = New
Template(TemplateType.AlternatingItemTemplate)
.LayoutTemplate = New Template(TemplateType.LayoutTemplate)
.InsertItemTemplate = New
Template(TemplateType.InsertItemTemplate)
.SelectedItemTemplate = New
Template(TemplateType.SelectedItemTemplate)
.EmptyDataTemplate = New
Template(TemplateType.EmptyDataTemplate)
.EditItemTemplate = New Template(TemplateType.EditItemTemplate)
.ItemTemplate = New Template(TemplateType.ItemTemplate)
End With

End Sub

End Class


Public Class Template
Implements ITemplate

Private _Type As TemplateType
Public Property Type() As TemplateType
Get
Return _Type
End Get
Set(ByVal value As TemplateType)
_Type = value
End Set
End Property

Public Sub New(ByVal type As TemplateType)
Me.Type = type
End Sub

Public Sub InstantiateIn(ByVal container As Control) Implements
ITemplate.InstantiateIn

' Select template type
Select Case Me.Type

Case TemplateType.AlternatingItemTemplate
ItemTemplate(container)

Case TemplateType.LayoutTemplate
LayoutTemplate(container)

Case TemplateType.ItemTemplate
ItemTemplate(container)

Case TemplateType.SelectedItemTemplate
ItemTemplate(container)

Case TemplateType.EmptyDataTemplate

Case TemplateType.EditItemTemplate
EditItemTemplate(container)

Case TemplateType.InsertItemTemplate
InsertItemTemplate(container)

End Select

End Sub

Private Sub LayoutTemplate(ByVal container As Control)

Dim table As New HtmlGenericControl("table")

Dim header As New HtmlGenericControl("thead")
table.Controls.Add(header)

Dim headerrow As New HtmlGenericControl("tr")
header.Controls.Add(headerrow)

Dim text As New HtmlTableCell("th")
text.InnerHtml = "Tag"
headerrow.Controls.Add(text)

Dim command As New HtmlTableCell("th")
command.InnerHtml = "Command"
headerrow.Controls.Add(command)

Dim body As New HtmlGenericControl("tbody")
body.ID = "itemPlaceholder"
table.Controls.Add(body)

Dim footer As New HtmlGenericControl("tfoot")
table.Controls.Add(footer)

Dim footerrow As New HtmlGenericControl("tr")
footer.Controls.Add(footerrow)

Dim pagercell As New HtmlTableCell("th")
pagercell.ColSpan = 2
footerrow.Controls.Add(pagercell)

Dim pager As New DataPager
footerrow.Controls.Add(pager)

Dim field As New NextPreviousPagerField
With field
.ButtonType = ButtonType.Button
.ShowFirstPageButton = True
.ShowLastPageButton = True
End With
pager.Fields.Add(field)

container.Controls.Add(table)

End Sub

Private Sub ItemTemplate(ByVal container As Control)

Dim item As New HtmlTableRow

Dim tag As New HtmlTableCell
item.Cells.Add(tag)

Dim command As New HtmlTableCell
item.Cells.Add(command)

Dim delete As New Button
delete.ID = "delete"
delete.Text = "Delete"
delete.CommandName = "Delete"
command.Controls.Add(delete)

Dim edit As New Button
edit.ID = "edit"
edit.Text = "Edit"
edit.CommandName = "Edit"
command.Controls.Add(edit)

Dim text As New Literal
text.ID = "text"
tag.Controls.Add(text)
AddHandler text.DataBinding, AddressOf text_DataBinding

container.Controls.Add(item)

End Sub

Private Sub InsertItemTemplate(ByVal container As Control)

Dim item As New HtmlTableRow

Dim tag As New HtmlTableCell
item.Cells.Add(tag)

Dim command As New HtmlTableCell
item.Cells.Add(command)

Dim insert As New Button
insert.ID = "insert"
insert.Text = "Insert"
insert.CommandName = "Insert"
command.Controls.Add(insert)

Dim cancel As New Button
cancel.ID = "cancel"
cancel.Text = "Cancel"
cancel.CommandName = "Cancel"
command.Controls.Add(cancel)

Dim inserttext As New TextBox
inserttext.ID = "inserttext"
tag.Controls.Add(inserttext)
AddHandler inserttext.DataBinding, AddressOf
inserttext_DataBinding

container.Controls.Add(item)

End Sub

Private Sub EditItemTemplate(ByVal container As Control)

Dim item As New HtmlTableRow

Dim tag As New HtmlTableCell
item.Cells.Add(tag)

Dim command As New HtmlTableCell
item.Cells.Add(command)

Dim update As New Button
update.ID = "update"
update.Text = "Update"
update.CommandName = "Update"
command.Controls.Add(update)

Dim cancel As New Button
cancel.ID = "cancel"
cancel.Text = "Cancel"
cancel.CommandName = "Cancel"
command.Controls.Add(cancel)

Dim updatetext As New TextBox
updatetext.ID = "updatetext"
tag.Controls.Add(updatetext)
AddHandler updatetext.DataBinding, AddressOf
updatetext_DataBinding

container.Controls.Add(item)

End Sub

Private Sub text_DataBinding(ByVal sender As Object, ByVal e As
EventArgs)
Dim text As Literal = CType(sender, Literal)
Dim lvdiContainer As ListViewDataItem =
CType(text.NamingContainer, ListViewDataItem)
text.Text = DataBinder.Eval(lvdiContainer.DataItem, "Text")
End Sub

Private Sub inserttext_DataBinding(ByVal sender As Object, ByVal e
As EventArgs)
Dim inserttext As TextBox = CType(sender, TextBox)
Dim lvdiContainer As ListViewDataItem =
CType(inserttext.NamingContainer, ListViewDataItem)
inserttext.Text = DataBinder.Eval(lvdiContainer.DataItem, "Text")
End Sub

Private Sub updatetext_DataBinding(ByVal sender As Object, ByVal e
As EventArgs)
Dim updatetext As TextBox = CType(sender, TextBox)
Dim lvdiContainer As ListViewDataItem =
CType(updatetext.NamingContainer, ListViewDataItem)
updatetext.Text = DataBinder.Eval(lvdiContainer.DataItem, "Text")
End Sub

End Class

SQL code to create Tags table

create table dbo.Tags
(
TagID uniqueidentifier not null
default NewID()
constraint PK_Tag primary key clustered,
[Text] nvarchar(100) not null
)
 
S

shapper

Hello,

I created a ListView with a LinqDataSource and a DataPager. I have two
problems:

1. When I insert a record I get the following error:

LinqDataSource 'ldsTags' has no values to insert. Check that the
'values' dictionary contains values.

2. When I update a record the value is updated but if I click the
browser's Refresh button immediately after I click the Update button I
get the following error:

Row not found or changed.

I have been all weekend trying to solve these problems but I can't
find a way.

Could someone, please, help me out?

I am posting all my code.

Thank You,

Miguel

ASPX

<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="ListViewRuntime.aspx.vb"
Inherits="ListViewRuntime" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>List View Runtime</title>
</head>
<body>
<form id="fListViewRuntime" runat="server">
<asp:placeHolder ID="phListViewRuntime" runat="server"></
asp:placeHolder>
</form>
</body>
</html>

ASPX.VB

Public Enum TemplateType
AlternatingItemTemplate
EditItemTemplate
EmptyDataTemplate
EmptyItemTemplate
GroupSeparatorTemplate
GroupTemplate
InsertItemTemplate
ItemSeparatorTemplate
ItemTemplate
LayoutTemplate
SelectedItemTemplate
End Enum

Partial Class ListViewRuntime
Inherits System.Web.UI.Page

Protected WithEvents ldsTags As New LinqDataSource
Protected WithEvents lvTags As New ListView

Protected Sub Page_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles Me.Init

phListViewRuntime.Controls.Add(ldsTags)
phListViewRuntime.Controls.Add(lvTags)

End Sub

Private Sub ldsTags_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles ldsTags.Init

With ldsTags
.ContextTypeName = "CodeDataContext"
.EnableDelete = True
.EnableInsert = True
.EnableUpdate = True
.ID = "ldsTags"
.TableName = "Tags"
End With

End Sub

Private Sub ldsTags_Updating(ByVal sender As Object, ByVal e As
LinqDataSourceUpdateEventArgs) Handles ldsTags.Updating

Dim tag As Tag = CType(e.NewObject, Tag)
Dim tb As TextBox = CType(Code.Base.Common.FindControl(lvTags,
"updatetext"), TextBox)
tag.Text = tb.Text

End Sub

Private Sub ldsTags_Inserting(ByVal sender As Object, ByVal e As
LinqDataSourceInsertEventArgs) Handles ldsTags.Inserting

Dim tag As Tag = CType(e.NewObject, Tag)
Dim tb As TextBox = CType(Code.Base.Common.FindControl(lvTags,
"inserttext"), TextBox)
tag.Text = tb.Text

End Sub

Private Sub lvTags_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles lvTags.Init

With lvTags
.DataKeyNames = New String() {"TagID"}
.DataSourceID = "ldsTags"
.ID = "lvTags"
.InsertItemPosition = InsertItemPosition.FirstItem
End With

With lvTags
.AlternatingItemTemplate = New
Template(TemplateType.AlternatingItemTemplate)
.LayoutTemplate = New Template(TemplateType.LayoutTemplate)
.InsertItemTemplate = New
Template(TemplateType.InsertItemTemplate)
.SelectedItemTemplate = New
Template(TemplateType.SelectedItemTemplate)
.EmptyDataTemplate = New
Template(TemplateType.EmptyDataTemplate)
.EditItemTemplate = New Template(TemplateType.EditItemTemplate)
.ItemTemplate = New Template(TemplateType.ItemTemplate)
End With

End Sub

End Class

Public Class Template
Implements ITemplate

Private _Type As TemplateType
Public Property Type() As TemplateType
Get
Return _Type
End Get
Set(ByVal value As TemplateType)
_Type = value
End Set
End Property

Public Sub New(ByVal type As TemplateType)
Me.Type = type
End Sub

Public Sub InstantiateIn(ByVal container As Control) Implements
ITemplate.InstantiateIn

' Select template type
Select Case Me.Type

Case TemplateType.AlternatingItemTemplate
ItemTemplate(container)

Case TemplateType.LayoutTemplate
LayoutTemplate(container)

Case TemplateType.ItemTemplate
ItemTemplate(container)

Case TemplateType.SelectedItemTemplate
ItemTemplate(container)

Case TemplateType.EmptyDataTemplate

Case TemplateType.EditItemTemplate
EditItemTemplate(container)

Case TemplateType.InsertItemTemplate
InsertItemTemplate(container)

End Select

End Sub

Private Sub LayoutTemplate(ByVal container As Control)

Dim table As New HtmlGenericControl("table")

Dim header As New HtmlGenericControl("thead")
table.Controls.Add(header)

Dim headerrow As New HtmlGenericControl("tr")
header.Controls.Add(headerrow)

Dim text As New HtmlTableCell("th")
text.InnerHtml = "Tag"
headerrow.Controls.Add(text)

Dim command As New HtmlTableCell("th")
command.InnerHtml = "Command"
headerrow.Controls.Add(command)

Dim body As New HtmlGenericControl("tbody")
body.ID = "itemPlaceholder"
table.Controls.Add(body)

Dim footer As New HtmlGenericControl("tfoot")
table.Controls.Add(footer)

Dim footerrow As New HtmlGenericControl("tr")
footer.Controls.Add(footerrow)

Dim pagercell As New HtmlTableCell("th")
pagercell.ColSpan = 2
footerrow.Controls.Add(pagercell)

Dim pager As New DataPager
footerrow.Controls.Add(pager)

Dim field As New NextPreviousPagerField
With field
.ButtonType = ButtonType.Button
.ShowFirstPageButton = True
.ShowLastPageButton = True
End With
pager.Fields.Add(field)

container.Controls.Add(table)

End Sub

Private Sub ItemTemplate(ByVal container As Control)

Dim item As New HtmlTableRow

Dim tag As New HtmlTableCell
item.Cells.Add(tag)

Dim command As New HtmlTableCell
item.Cells.Add(command)

Dim delete As New Button
delete.ID = "delete"
delete.Text = "Delete"
delete.CommandName = "Delete"
command.Controls.Add(delete)

Dim edit As New Button
edit.ID = "edit"
edit.Text = "Edit"
edit.CommandName = "Edit"
command.Controls.Add(edit)

Dim text As New Literal
text.ID = "text"
tag.Controls.Add(text)
AddHandler text.DataBinding, AddressOf text_DataBinding

container.Controls.Add(item)

End Sub

Private Sub InsertItemTemplate(ByVal container As Control)

Dim item As New HtmlTableRow

Dim tag As New HtmlTableCell
item.Cells.Add(tag)

Dim command As New HtmlTableCell
item.Cells.Add(command)

Dim insert As New Button
insert.ID = "insert"
insert.Text = "Insert"
insert.CommandName = "Insert"
command.Controls.Add(insert)

Dim cancel As New Button
cancel.ID = "cancel"
cancel.Text = "Cancel"
cancel.CommandName = "Cancel"
command.Controls.Add(cancel)

Dim inserttext As New TextBox
inserttext.ID = "inserttext"
tag.Controls.Add(inserttext)
AddHandler inserttext.DataBinding, AddressOf
inserttext_DataBinding

container.Controls.Add(item)

End Sub

Private Sub EditItemTemplate(ByVal container As Control)

Dim item As New HtmlTableRow

Dim tag As New HtmlTableCell
item.Cells.Add(tag)

Dim command As New HtmlTableCell
item.Cells.Add(command)

Dim update As New Button
update.ID = "update"
update.Text = "Update"
update.CommandName = "Update"
command.Controls.Add(update)

Dim cancel As New Button
cancel.ID = "cancel"
cancel.Text = "Cancel"
cancel.CommandName = "Cancel"
command.Controls.Add(cancel)

Dim updatetext As New TextBox
updatetext.ID = "updatetext"
tag.Controls.Add(updatetext)
AddHandler updatetext.DataBinding, AddressOf
updatetext_DataBinding

container.Controls.Add(item)

End Sub

Private Sub text_DataBinding(ByVal sender As Object, ByVal e As
EventArgs)
Dim text As Literal = CType(sender, Literal)
Dim lvdiContainer As ListViewDataItem =
CType(text.NamingContainer, ListViewDataItem)
text.Text = DataBinder.Eval(lvdiContainer.DataItem, "Text")
End Sub

Private Sub inserttext_DataBinding(ByVal sender As Object, ByVal e
As EventArgs)
Dim inserttext As TextBox = CType(sender, TextBox)
Dim lvdiContainer As ListViewDataItem =
CType(inserttext.NamingContainer, ListViewDataItem)
inserttext.Text = DataBinder.Eval(lvdiContainer.DataItem, "Text")
End Sub

Private Sub updatetext_DataBinding(ByVal sender As Object, ByVal e
As EventArgs)
Dim updatetext As TextBox = CType(sender, TextBox)
Dim lvdiContainer As ListViewDataItem =
CType(updatetext.NamingContainer, ListViewDataItem)
updatetext.Text = DataBinder.Eval(lvdiContainer.DataItem, "Text")
End Sub

End Class

SQL code to create Tags table

create table dbo.Tags
(
TagID uniqueidentifier not null
default NewID()
constraint PK_Tag primary key clustered,
[Text] nvarchar(100) not null
)

Hello,

I was able to solve the first problem using the following:

Private Sub ldsTags_Updating(ByVal sender As Object, ByVal e As
LinqDataSourceUpdateEventArgs) Handles ldsTags.Updating
Dim tag As Tag = CType(e.NewObject, Tag)
tag.Text = "Testing Updating"
End Sub

Private Sub ldsTags_Inserting(ByVal sender As Object, ByVal e As
LinqDataSourceInsertEventArgs) Handles ldsTags.Inserting
Dim tag As Tag = CType(e.NewObject, Tag)
tag.Text = "Testing Inserting"
End Sub

Protected Sub lvTags_ItemInserting(ByVal sender As Object, ByVal e
As ListViewInsertEventArgs) Handles lvTags.ItemInserting
e.Values("Text") = ""
End Sub

Protected Sub lvTags_ItemUpdating(ByVal sender As Object, ByVal e As
ListViewUpdateEventArgs) Handles lvTags.ItemUpdating
e.NewValues("Text") = ""
End Sub

It works but it also works the following way:

Protected Sub lvTags_ItemInserting(ByVal sender As Object, ByVal e
As ListViewInsertEventArgs) Handles lvTags.ItemInserting
e.Values("Text") = "Testing Inserting"
End Sub

Protected Sub lvTags_ItemUpdating(ByVal sender As Object, ByVal e As
ListViewUpdateEventArgs) Handles lvTags.ItemUpdating
e.NewValues("Text") = "Testing Updating"
End Sub

The question that remains is why do I need to define the values in
ListView events when the ListView is connected to a LinqDataSource?

I was following this article in MSDN:

http://msdn2.microsoft.com/en-us/library/bb514963.aspx

Am I doing something wrong?

I would like to understand this.

Thanks,

Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top