One more LinqDataSource problem ... Get inserted record ID

Discussion in 'ASP .Net' started by shapper, Feb 12, 2008.

  1. shapper

    shapper Guest

    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
    )
     
    shapper, Feb 12, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Craig
    Replies:
    1
    Views:
    1,901
    =?Utf-8?B?SmFtZXM=?=
    Jan 25, 2006
  2. shapper

    LinqDataSource. Get ID

    shapper, Dec 18, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    453
    Mike Placentra II
    Dec 23, 2007
  3. fig000
    Replies:
    0
    Views:
    626
    fig000
    Sep 6, 2008
  4. Bobby Edward
    Replies:
    2
    Views:
    3,060
    Bobby Edward
    Oct 5, 2008
  5. Jim in Arizona
    Replies:
    2
    Views:
    4,708
    Jim in Arizona
    Oct 23, 2008
Loading...

Share This Page