Delete Record

S

shapper

Hello,

I have a ListView defined as follows:

Private Sub lv_Init(ByVal sender As Object, ByVal e As EventArgs)
Handles lv.Init
lv.DataKeyNames = New String() {"TagID"}
lv.ID = "lv"
lvTags.ItemTemplate = New MyItemTemplate()
lvTags.LayoutTemplate = New MyLayoutTemplate()
End Sub

Private Sub lv_Load(ByVal sender As Object, ByVal e As EventArgs)
Handles lv.Load
Dim database As New MyContextDataContext
Dim tags = From t In database.Tags _
Select t.TagID, t.Text
lv.DataSource = tags
lv.DataBind()
End Sub

Private Sub lv_ItemDeleting(ByVal sender As Object, ByVal e As
ListViewDeleteEventArgs) Handles lv.ItemDeleting
Dim database As New MyContextDataContext
Dim tags = From t In database.Tags _
Where t.TagID = New
Guid(lvTags.DataKeys(e.ItemIndex).Value.ToString)
database.Tags.Remove(tags)
End Sub

The ListView displays all records and the ItemDeleting event is fired
but I get an error:
Unable to cast object of type
'System.Data.Linq.DataQuery`1[Undefined.Data.Linq.Tag]' to type
'Undefined.Data.Linq.Tag'

What am I doing wrong?

Thanks,
Miguel
 
C

Cor Ligthert[MVP]

shapper,

As you have this kind of problems, why then not find it yourself by
splitting the sentence in more parts to find the problem.

New Guid(lvTags.DataKeys(e.ItemIndex).Value.ToString)


Cor

"
 
S

shapper

shapper,

As you have this kind of problems, why then not find it yourself by
splitting the sentence in more parts to find the problem.

New Guid(lvTags.DataKeys(e.ItemIndex).Value.ToString)

Cor

"
Dim tags = From t In database.Tags _
Where t.TagID = New
Guid(lvTags.DataKeys(e.ItemIndex).Value.ToString)
database.Tags.Remove(tags)
End Sub
The ListView displays all records and the ItemDeleting event is fired
but I get an error:
Unable to cast object of type
'System.Data.Linq.DataQuery`1[Undefined.Data.Linq.Tag]' to type
'Undefined.Data.Linq.Tag'
What am I doing wrong?
Thanks,
Miguel

I found the problem. Everything was working just fine.
However, because it was not working I started to make changes to try
to find the problem and I got a few errors.

The solution to the problem, was as always, simpler then expected.

I missed:
database.SubmitChanges()

I didn't know it was need this command at the end.

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

Top