ListView Problem. Going Crazy. Please, I don't know what else to try.Thank You.

S

shapper

Hello,

I created a ListView connected to a LinqDataSource in DesignView using
VS2008.
The ListView displays the records and I am able to Delete and Update
any record.

I am even able to insert one record but when I try to insert the
second record I get the following error:

Violation of PRIMARY KEY constraint 'PK_Tag'.
Cannot insert duplicate key in object 'dbo.Tags'. The statement has
been terminated.

This is because when I insert the first record the ListView creates
the following ID:
00000000-0000-0000-0000-000000000000

On the second time it creates again the same ID.

I am on this for 4 days and I am not able to solve it. I really don't
know what else to do!

Could someone please help me out?
I post all my code ... maybe someone could try it to or find where the
problem is.

Thank You Very Much,
Miguel
Create Tags Table

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

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

<!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 Design</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="ListView1" runat="server"
DataSourceID="LinqDataSource1"
DataKeyNames="TagID" InsertItemPosition="FirstItem">
<AlternatingItemTemplate>
<tr style="">
<td>
<asp:Label ID="TextLabel" runat="server" Text='<%#
Eval("Text") %>' />
</td>
</tr>
</AlternatingItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="itemPlaceholderContainer" runat="server"
border="0"
style="">
<tr runat="server" style="">
<th runat="server">
Text</th>
</tr>
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server"
style="">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button"
ShowFirstPageButton="True"
ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server"
CommandName="Insert"
Text="Insert" />
<asp:Button ID="CancelButton" runat="server"
CommandName="Cancel"
Text="Clear" />
</td>
<td>
<asp:TextBox ID="TextTextBox" runat="server" Text='<%#
Bind("Text") %>' />
</td>
</tr>
</InsertItemTemplate>
<SelectedItemTemplate>
<tr style="">
<td>
<asp:Label ID="TextLabel" runat="server" Text='<%#
Eval("Text") %>' />
</td>
</tr>
</SelectedItemTemplate>
<EmptyDataTemplate>
<table runat="server"
style="">
<tr>
<td>
No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<EditItemTemplate>
<tr style="">
<td>
<asp:Button ID="UpdateButton" runat="server"
CommandName="Update"
Text="Update" />
<asp:Button ID="CancelButton" runat="server"
CommandName="Cancel"
Text="Cancel" />
</td>
<td>
<asp:TextBox ID="TextTextBox" runat="server" Text='<%#
Bind("Text") %>' />
</td>
</tr>
</EditItemTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Label ID="TextLabel" runat="server" Text='<%#
Eval("Text") %>' />
</td>
<td>
<asp:LinkButton ID="EditButton" runat="Server" Text="Edit"
CommandName="Edit" />
<asp:LinkButton ID="DeleteButton" runat="Server"
Text="Delete" CommandName="Delete" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="CodeDataContext" TableName="Tags"
EnableDelete="True" EnableInsert="True" EnableUpdate="True" >
</asp:LinqDataSource>
</div>
</form>
</body>
ListViewDesign.aspx.vb:

Partial Class ListViewDesign
Inherits System.Web.UI.Page

End Class

Generated code for Linq to SQL Classes (I just dragged the table Tags in VS 2008):

'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
' Runtime Version:2.0.50727.1433
'
' Changes to this file may cause incorrect behavior and will be
lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Data.Linq
Imports System.Data.Linq.Mapping
Imports System.Linq
Imports System.Linq.Expressions
Imports System.Reflection


<System.Data.Linq.Mapping.DatabaseAttribute(Name:="Code")> _
Partial Public Class CodeDataContext
Inherits System.Data.Linq.DataContext

Private Shared mappingSource As
System.Data.Linq.Mapping.MappingSource = New AttributeMappingSource

#Region "Extensibility Method Definitions"
Partial Private Sub OnCreated()
End Sub
Partial Private Sub InsertTag(instance As Tag)
End Sub
Partial Private Sub UpdateTag(instance As Tag)
End Sub
Partial Private Sub DeleteTag(instance As Tag)
End Sub
#End Region

Public Sub New()

MyBase.New(Global.System.Configuration.ConfigurationManager.ConnectionStrings("CodeConnectionString").ConnectionString,
mappingSource)
OnCreated
End Sub

Public Sub New(ByVal connection As String)
MyBase.New(connection, mappingSource)
OnCreated
End Sub

Public Sub New(ByVal connection As System.Data.IDbConnection)
MyBase.New(connection, mappingSource)
OnCreated
End Sub

Public Sub New(ByVal connection As String, ByVal mappingSource As
System.Data.Linq.Mapping.MappingSource)
MyBase.New(connection, mappingSource)
OnCreated
End Sub

Public Sub New(ByVal connection As System.Data.IDbConnection, ByVal
mappingSource As System.Data.Linq.Mapping.MappingSource)
MyBase.New(connection, mappingSource)
OnCreated
End Sub

Public ReadOnly Property Tags() As System.Data.Linq.Table(Of Tag)
Get
Return Me.GetTable(Of Tag)
End Get
End Property
End Class

<Table(Name:="dbo.Tags")> _
Partial Public Class Tag
Implements System.ComponentModel.INotifyPropertyChanging,
System.ComponentModel.INotifyPropertyChanged

Private Shared emptyChangingEventArgs As PropertyChangingEventArgs =
New PropertyChangingEventArgs(String.Empty)

Private _TagID As System.Guid

Private _Text As String

#Region "Extensibility Method Definitions"
Partial Private Sub OnLoaded()
End Sub
Partial Private Sub OnValidate(action As
System.Data.Linq.ChangeAction)
End Sub
Partial Private Sub OnCreated()
End Sub
Partial Private Sub OnTagIDChanging(value As System.Guid)
End Sub
Partial Private Sub OnTagIDChanged()
End Sub
Partial Private Sub OnTextChanging(value As String)
End Sub
Partial Private Sub OnTextChanged()
End Sub
#End Region

Public Sub New()
MyBase.New
OnCreated
End Sub

<Column(Storage:="_TagID", DbType:="UniqueIdentifier NOT NULL",
IsPrimaryKey:=true)> _
Public Property TagID() As System.Guid
Get
Return Me._TagID
End Get
Set
If ((Me._TagID = value) _
= false) Then
Me.OnTagIDChanging(value)
Me.SendPropertyChanging
Me._TagID = value
Me.SendPropertyChanged("TagID")
Me.OnTagIDChanged
End If
End Set
End Property

<Column(Storage:="_Text", DbType:="NVarChar(100) NOT NULL",
CanBeNull:=false)> _
Public Property Text() As String
Get
Return Me._Text
End Get
Set
If (String.Equals(Me._Text, value) = false) Then
Me.OnTextChanging(value)
Me.SendPropertyChanging
Me._Text = value
Me.SendPropertyChanged("Text")
Me.OnTextChanged
End If
End Set
End Property

Public Event PropertyChanging As PropertyChangingEventHandler
Implements
System.ComponentModel.INotifyPropertyChanging.PropertyChanging

Public Event PropertyChanged As PropertyChangedEventHandler
Implements
System.ComponentModel.INotifyPropertyChanged.PropertyChanged

Protected Overridable Sub SendPropertyChanging()
If ((Me.PropertyChangingEvent Is Nothing) _
= false) Then
RaiseEvent PropertyChanging(Me, emptyChangingEventArgs)
End If
End Sub

Protected Overridable Sub SendPropertyChanged(ByVal propertyName As
[String])
If ((Me.PropertyChangedEvent Is Nothing) _
= false) Then
RaiseEvent PropertyChanged(Me, New
PropertyChangedEventArgs(propertyName))
End If
End Sub
End Class
 
S

shapper

Hello,

I created a ListView connected to a LinqDataSource in DesignView using
VS2008.
The ListView displays the records and I am able to Delete and Update
any record.

I am even able to insert one record but when I try to insert the
second record I get the following error:

Violation of PRIMARY KEY constraint 'PK_Tag'.
Cannot insert duplicate key in object 'dbo.Tags'. The statement has
been terminated.

This is because when I insert the first record the ListView creates
the following ID:
00000000-0000-0000-0000-000000000000

On the second time it creates again the same ID.

I am on this for 4 days and I am not able to solve it. I really don't
know what else to do!

Could someone please help me out?
I post all my code ... maybe someone could try it to or find where the
problem is.

Thank You Very Much,
Miguel
Create Tags Table

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

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

<!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 Design</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="ListView1" runat="server"
DataSourceID="LinqDataSource1"
DataKeyNames="TagID" InsertItemPosition="FirstItem">
<AlternatingItemTemplate>
<tr style="">
<td>
<asp:Label ID="TextLabel" runat="server" Text='<%#
Eval("Text") %>' />
</td>
</tr>
</AlternatingItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="itemPlaceholderContainer" runat="server"
border="0"
style="">
<tr runat="server" style="">
<th runat="server">
Text</th>
</tr>
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server"
style="">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button"
ShowFirstPageButton="True"
ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server"
CommandName="Insert"
Text="Insert" />
<asp:Button ID="CancelButton" runat="server"
CommandName="Cancel"
Text="Clear" />
</td>
<td>
<asp:TextBox ID="TextTextBox" runat="server" Text='<%#
Bind("Text") %>' />
</td>
</tr>
</InsertItemTemplate>
<SelectedItemTemplate>
<tr style="">
<td>
<asp:Label ID="TextLabel" runat="server" Text='<%#
Eval("Text") %>' />
</td>
</tr>
</SelectedItemTemplate>
<EmptyDataTemplate>
<table runat="server"
style="">
<tr>
<td>
No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<EditItemTemplate>
<tr style="">
<td>
<asp:Button ID="UpdateButton" runat="server"
CommandName="Update"
Text="Update" />
<asp:Button ID="CancelButton" runat="server"
CommandName="Cancel"
Text="Cancel" />
</td>
<td>
<asp:TextBox ID="TextTextBox" runat="server" Text='<%#
Bind("Text") %>' />
</td>
</tr>
</EditItemTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Label ID="TextLabel" runat="server" Text='<%#
Eval("Text") %>' />
</td>
<td>
<asp:LinkButton ID="EditButton" runat="Server" Text="Edit"
CommandName="Edit" />
<asp:LinkButton ID="DeleteButton" runat="Server"
Text="Delete" CommandName="Delete" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="CodeDataContext" TableName="Tags"
EnableDelete="True" EnableInsert="True" EnableUpdate="True" >
</asp:LinqDataSource>
</div>
</form>
</body>
ListViewDesign.aspx.vb:

Partial Class ListViewDesign
Inherits System.Web.UI.Page

End Class
Generated code for Linq to SQL Classes (I just dragged the table Tags in VS 2008):

'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
' Runtime Version:2.0.50727.1433
'
' Changes to this file may cause incorrect behavior and will be
lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Data.Linq
Imports System.Data.Linq.Mapping
Imports System.Linq
Imports System.Linq.Expressions
Imports System.Reflection

<System.Data.Linq.Mapping.DatabaseAttribute(Name:="Code")> _
Partial Public Class CodeDataContext
Inherits System.Data.Linq.DataContext

Private Shared mappingSource As
System.Data.Linq.Mapping.MappingSource = New AttributeMappingSource

#Region "Extensibility Method Definitions"
Partial Private Sub OnCreated()
End Sub
Partial Private Sub InsertTag(instance As Tag)
End Sub
Partial Private Sub UpdateTag(instance As Tag)
End Sub
Partial Private Sub DeleteTag(instance As Tag)
End Sub
#End Region

Public Sub New()

MyBase.New(Global.System.Configuration.ConfigurationManager.ConnectionStrings("CodeConnectionString").ConnectionString,
mappingSource)
OnCreated
End Sub

Public Sub New(ByVal connection As String)
MyBase.New(connection, mappingSource)
OnCreated
End Sub

Public Sub New(ByVal connection As System.Data.IDbConnection)
MyBase.New(connection, mappingSource)
OnCreated
End Sub

Public Sub New(ByVal connection As String, ByVal mappingSource As
System.Data.Linq.Mapping.MappingSource)
MyBase.New(connection, mappingSource)
OnCreated
End Sub

Public Sub New(ByVal connection As System.Data.IDbConnection, ByVal
mappingSource As System.Data.Linq.Mapping.MappingSource)
MyBase.New(connection, mappingSource)
OnCreated
End Sub

Public ReadOnly Property Tags() As System.Data.Linq.Table(Of Tag)
Get
Return Me.GetTable(Of Tag)
End Get
End Property
End Class

<Table(Name:="dbo.Tags")> _
Partial Public Class Tag
Implements System.ComponentModel.INotifyPropertyChanging,
System.ComponentModel.INotifyPropertyChanged

Private Shared emptyChangingEventArgs As PropertyChangingEventArgs =
New PropertyChangingEventArgs(String.Empty)

Private _TagID As System.Guid

Private _Text As String

#Region "Extensibility Method Definitions"
Partial Private Sub OnLoaded()
End Sub
Partial Private Sub OnValidate(action As
System.Data.Linq.ChangeAction)
End Sub
Partial Private Sub OnCreated()
End Sub
Partial Private Sub OnTagIDChanging(value As System.Guid)
End Sub
Partial Private Sub OnTagIDChanged()
End Sub
Partial Private Sub OnTextChanging(value As String)
End Sub
Partial Private Sub OnTextChanged()
End Sub
#End Region

Public Sub New()
MyBase.New
OnCreated
End Sub

<Column(Storage:="_TagID", DbType:="UniqueIdentifier NOT NULL",
IsPrimaryKey:=true)> _
Public Property TagID() As System.Guid
Get
Return Me._TagID
End Get
Set
If ((Me._TagID = value) _
= false) Then
Me.OnTagIDChanging(value)
Me.SendPropertyChanging
Me._TagID = value
Me.SendPropertyChanged("TagID")
Me.OnTagIDChanged
End If
End Set
End Property

<Column(Storage:="_Text", DbType:="NVarChar(100) NOT NULL",
CanBeNull:=false)> _
Public Property Text() As String
Get
Return Me._Text
End Get
Set
If (String.Equals(Me._Text, value) = false) Then
Me.OnTextChanging(value)
Me.SendPropertyChanging
Me._Text = value
Me.SendPropertyChanged("Text")
Me.OnTextChanged
End If
End Set
End Property

Public Event PropertyChanging As PropertyChangingEventHandler
Implements
System.ComponentModel.INotifyPropertyChanging.PropertyChanging

Public Event PropertyChanged As PropertyChangedEventHandler
Implements
System.ComponentModel.INotifyPropertyChanged.PropertyChanged

Protected Overridable Sub SendPropertyChanging()
If ((Me.PropertyChangingEvent Is Nothing) _
= false) Then
RaiseEvent PropertyChanging(Me, emptyChangingEventArgs)
End If
End Sub

Protected Overridable Sub SendPropertyChanged(ByVal propertyName As
[String])
If ((Me.PropertyChangedEvent Is Nothing) _
= false) Then
RaiseEvent PropertyChanged(Me, New
PropertyChangedEventArgs(propertyName))
End If
End Sub
End Class

I found the solution!

Here it is for future reference:

TagId should be marked as IsDbGenerated in the class model. Something
like:

[Column(Name = "TagId", IsPrimaryKey = true, IsDbGenerated=true,
AutoSync=AutoSync.OnInsert)]

The Autosync that instructs Linq To Sql when to sync this column with
the database.

Cheers,
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,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top