Solved

ASP.NET 3.5 Linq insert a child record LISTVIEW

Posted on 2009-04-02
1
1,048 Views
Last Modified: 2013-11-26
I am trying to build an application Visual Studio using VB SQL server 2008 and LINQ. I have a simple one to many structure and I wish to INSERT items into the child database using the Listview Control and LINQ.
I am using the InsertItems template etc. I can enter data but when I click on insert nothing is happening. I have done a databing in the vb code behind, am I missing something?
This is driving me nuts. I also need to inert a value into the FK of the child database. I am getting no error messages, but nothing is being written to the database.
Can anyone help
Thanks

TrilobyteMKR
<asp:LinqDataSource ID="LinqDataSource2" runat="server" 
        ContextTypeName="DataClassesDataContext" EnableDelete="True" 
        EnableInsert="True" EnableUpdate="True" 
        select="New
         with consID =linqdatasource1"
         TableName="Tbl_ExtraNotes">
    </asp:LinqDataSource>
 
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID" 
                    DataSourceID="LinqDataSource2" InsertItemPosition="LastItem">
                    <ItemTemplate>
                        <tr style="background-color: #FFFBD6;color: #333333;">
                            <td>
                                <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                            </td>
                            <td>
                                <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                            </td>
                            <td>
                                <asp:Label ID="DateAddedLabel" runat="server" Text='<%# Eval("DateAdded") %>' />
                            </td>
                            <td>
                                <asp:Label ID="AddedByLabel" runat="server" Text='<%# Eval("AddedBy") %>' />
                            </td>
                            <td>
                                <asp:Label ID="NotesLabel" runat="server" Text='<%# Eval("Notes") %>' />
                            </td>
                            <td>
                                <asp:Label ID="stampLabel" runat="server" Text='<%# Eval("stamp") %>' />
                            </td>
                            <td>
                                <asp:Label ID="ConsIDLabel" runat="server" Text='<%# Eval("ConsID") %>' />
                            </td>
                            <td>
                                <asp:Label ID="DateNextActionLabel" runat="server" 
                                    Text='<%# Eval("DateNextAction") %>' />
                            </td>
                            <td>
                                <asp:Label ID="NextActionOnLabel" runat="server" 
                                    Text='<%# Eval("NextActionOn") %>' />
                            </td>
                            <td>
                                <asp:Label ID="Tbl_ConsumerCallLabel" runat="server" 
                                    Text='<%# Eval("Tbl_ConsumerCall") %>' />
                            </td>
                        </tr>
                    </ItemTemplate>
                    <AlternatingItemTemplate>
                        <tr style="background-color: #FAFAD2;color: #284775;">
                            <td>
                                <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                            </td>
                            <td>
                                <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                            </td>
                            <td>
                                <asp:Label ID="DateAddedLabel" runat="server" Text='<%# Eval("DateAdded") %>' />
                            </td>
                            <td>
                                <asp:Label ID="AddedByLabel" runat="server" Text='<%# Eval("AddedBy") %>' />
                            </td>
                            <td>
                                <asp:Label ID="NotesLabel" runat="server" Text='<%# Eval("Notes") %>' />
                            </td>
                            <td>
                                <asp:Label ID="stampLabel" runat="server" Text='<%# Eval("stamp") %>' />
                            </td>
                            <td>
                                <asp:Label ID="ConsIDLabel" runat="server" Text='<%# Eval("ConsID") %>' />
                            </td>
                            <td>
                                <asp:Label ID="DateNextActionLabel" runat="server" 
                                    Text='<%# Eval("DateNextAction") %>' />
                            </td>
                            <td>
                                <asp:Label ID="NextActionOnLabel" runat="server" 
                                    Text='<%# Eval("NextActionOn") %>' />
                            </td>
                            <td>
                                <asp:Label ID="Tbl_ConsumerCallLabel" runat="server" 
                                    Text='<%# Eval("Tbl_ConsumerCall") %>' />
                            </td>
                        </tr>
                    </AlternatingItemTemplate>
                    <EmptyDataTemplate>
                        <table runat="server" 
                            style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
                            <tr>
                                <td>
                                    No data was returned.</td>
                            </tr>
                        </table>
                    </EmptyDataTemplate>
                    <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>
                                &nbsp;</td>
                            <td>
                                <asp:TextBox ID="DateAddedTextBox" runat="server" 
                                    Text='<%# Bind("DateAdded") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="AddedByTextBox" runat="server" Text='<%# Bind("AddedBy") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="NotesTextBox" runat="server" Text='<%# Bind("Notes") %>' />
                            </td>
                            <td>
                                &nbsp;</td>
                            <td>
                                <asp:TextBox ID="ConsIDTextBox" runat="server" Text='<%# Bind("ConsID") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="DateNextActionTextBox" runat="server" 
                                    Text='<%# Bind("DateNextAction") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="NextActionOnTextBox" runat="server" 
                                    Text='<%# Bind("NextActionOn") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="Tbl_ConsumerCallTextBox" runat="server" 
                                    Text='<%# Bind("Tbl_ConsumerCall") %>' />
                            </td>
                        </tr>
                    </InsertItemTemplate>
                    <LayoutTemplate>
                        <table runat="server">
                            <tr runat="server">
                                <td runat="server">
                                    <table ID="itemPlaceholderContainer" runat="server" border="1" 
                                        style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
                                        <tr runat="server" style="background-color: #FFFBD6;color: #333333;">
                                            <th runat="server">
                                            </th>
                                            <th runat="server">
                                                ID</th>
                                            <th runat="server">
                                                DateAdded</th>
                                            <th runat="server">
                                                AddedBy</th>
                                            <th runat="server">
                                                Notes</th>
                                            <th runat="server">
                                                stamp</th>
                                            <th runat="server">
                                                ConsID</th>
                                            <th runat="server">
                                                DateNextAction</th>
                                            <th runat="server">
                                                NextActionOn</th>
                                            <th runat="server">
                                                Tbl_ConsumerCall</th>
                                        </tr>
                                        <tr ID="itemPlaceholder" runat="server">
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            <tr runat="server">
                                <td runat="server" 
                                    style="text-align: center;background-color: #FFCC66;font-family: Verdana, Arial, Helvetica, sans-serif;color: #333333;">
                                </td>
                            </tr>
                        </table>
                    </LayoutTemplate>
                    <EditItemTemplate>
                        <tr style="background-color: #FFCC66;color: #000080;">
                            <td>
                                <asp:Button ID="UpdateButton" runat="server" CommandName="Update" 
                                    Text="Update" />
                                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                                    Text="Cancel" />
                            </td>
                            <td>
                                <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="DateAddedTextBox" runat="server" 
                                    Text='<%# Bind("DateAdded") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="AddedByTextBox" runat="server" Text='<%# Bind("AddedBy") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="NotesTextBox" runat="server" Text='<%# Bind("Notes") %>' />
                            </td>
                            <td>
                                <asp:Label ID="stampLabel1" runat="server" Text='<%# Eval("stamp") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="ConsIDTextBox" runat="server" Text='<%# Bind("ConsID") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="DateNextActionTextBox" runat="server" 
                                    Text='<%# Bind("DateNextAction") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="NextActionOnTextBox" runat="server" 
                                    Text='<%# Bind("NextActionOn") %>' />
                            </td>
                            <td>
                                <asp:TextBox ID="Tbl_ConsumerCallTextBox" runat="server" 
                                    Text='<%# Bind("Tbl_ConsumerCall") %>' />
                            </td>
                        </tr>
                    </EditItemTemplate>
                    <SelectedItemTemplate>
                        <tr style="background-color: #FFCC66;font-weight: bold;color: #000080;">
                            <td>
                                <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                            </td>
                            <td>
                                <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                            </td>
                            <td>
                                <asp:Label ID="DateAddedLabel" runat="server" Text='<%# Eval("DateAdded") %>' />
                            </td>
                            <td>
                                <asp:Label ID="AddedByLabel" runat="server" Text='<%# Eval("AddedBy") %>' />
                            </td>
                            <td>
                                <asp:Label ID="NotesLabel" runat="server" Text='<%# Eval("Notes") %>' />
                            </td>
                            <td>
                                <asp:Label ID="stampLabel" runat="server" Text='<%# Eval("stamp") %>' />
                            </td>
                            <td>
                                <asp:Label ID="ConsIDLabel" runat="server" Text='<%# Eval("ConsID") %>' />
                            </td>
                            <td>
                                <asp:Label ID="DateNextActionLabel" runat="server" 
                                    Text='<%# Eval("DateNextAction") %>' />
                            </td>
                            <td>
                                <asp:Label ID="NextActionOnLabel" runat="server" 
                                    Text='<%# Eval("NextActionOn") %>' />
                            </td>
                            <td>
                                <asp:Label ID="Tbl_ConsumerCallLabel" runat="server" 
                                    Text='<%# Eval("Tbl_ConsumerCall") %>' />
                            </td>
                        </tr>
                    </SelectedItemTemplate>
                </asp:ListView>
 
VB CODE ***********************************************************************
 Protected Sub ListView1_ItemInserted(ByVal sender As Object, _
                ByVal e As System.Web.UI.WebControls.ListViewInsertedEventArgs) _
                Handles ListView1.ItemInserted
        If e.Exception IsNot Nothing Then
            lblError.Text = "A database error has occurred. " _
            & e.Exception.Message
            e.ExceptionHandled = True
        Else
            ListView1.DataBind()
        End If
    End Sub

Open in new window

0
Comment
Question by:TrilobyteMKR
1 Comment
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 24058659
Following is what a Microsoft article says

"If you want the LinqDataSource control to automatically generate update commands, you cannot set the Select property. When the Select property is set, the LinqDataSource control returns an object that is an instance of a dynamic class. It is not an instance of the class that represents the database table. Therefore, the dynamic class cannot infer how to update the values in the database table. "

http://msdn.microsoft.com/en-us/library/bb547113.aspx
http://weblogs.asp.net/scottgu/archive/2007/07/16/linq-to-sql-part-5-binding-ui-using-the-asp-linqdatasource-control.aspx
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Crystal reports vb.net 2 39
EF5: Question about Metadata Artifact Processing 4 15
.NET 2008 VB and C# 6 27
Dot Net 4.6.1 installation fails on W2012 R2 server 14 16
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question