One more LinqDataSource problem ... Get inserted record ID


S

shapper

Hello,

I need to get the the ID of a record that was just inserted by a
ListView which is connected to a LinqDataSource so I added the
following:

Protected Sub LinqDataSource1_Inserted(ByVal sender As Object, ByVal
e As LinqDataSourceStatusEventArgs) Handles LinqDataSource1.Inserted
Dim tag As Tag = CType(e.Result, Tag)
Dim id As String = tag.TagID.ToString()
End Sub

I got an error:

Object reference not set to an instance of an object.

I debugged it and I found that e.Result is nothing.

Without LinqDataSource1_Inserted everything works fine in my ListView
including inserting.

Could someone, please, help me out? I am posting my code.

Thanks,

Miguel

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="fListViewDesign" runat="server">
<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>
<td>
<asp:LinkButton ID="EditButton" runat="Server" Text="Edit"
CommandName="Edit" />
<asp:LinkButton ID="DeleteButton" runat="Server"
Text="Delete" CommandName="Delete" />
</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>
</form>
</body>
</html>

ASPX.VB

Partial Class ListViewDesign
Inherits System.Web.UI.Page

Protected Sub LinqDataSource1_Inserted(ByVal sender As Object, ByVal
e As LinqDataSourceStatusEventArgs) Handles LinqDataSource1.Inserted
Dim tag As Tag = CType(e.Result, Tag)
Dim id As String = tag.TagID.ToString()
End Sub

End Class

SQL Script to Create TableASPX.VB

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

Advertisements


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