Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 12th, 2012, 05:01 PM
Authorized User
 
Join Date: Jun 2012
Posts: 27
Thanks: 3
Thanked 0 Times in 0 Posts
Default Issue with Insert command on Formview

Hey Imar, so this isn't directly from one of the books exercises, I just wanted to try something out here after building the Management page.

I wanted to play with Formview, it seems like the most basic data bound control to me, this is what I created:

Code:
<asp:FormView ID="FormView3" runat="server" DataSourceID="SqlDataSource4" RenderOuterTable="False" DefaultMode="Insert">
        <InsertItemTemplate>
            Name:
            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
            <br />
            E-mail:
            <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
            <br />
            Website:
            <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Website") %>'></asp:TextBox>
            <br />
            Comment:
            <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Comment") %>'></asp:TextBox>
            <br />
            pid:
            <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("PostId") %>'></asp:TextBox>
            <br />
            <asp:Button ID="postcomment" runat="server" Text="Button" CommandName="Insert" />
        </InsertItemTemplate>
    </asp:FormView>
I used CommandName="Insert" (after some browsing on the Web) so the button works as an "Insert" text link... similar to the whole "Insert/Cancel" thing we see in the book.

And this is my data source:

Code:
<asp:SqlDataSource ID="SqlDataSource4" runat="server" 
        ConnectionString="<%$ ConnectionStrings:orangefreshConnectionString1 %>" 
        DeleteCommand="DELETE FROM [Comment] WHERE [Id] = @Id" 
        InsertCommand="INSERT INTO [Comment] ([Name], [Email], [Website], [Comment], [PostId]) VALUES (@Name, @Email, @Website, @Comment, @PostId)" 
        SelectCommand="SELECT [Name], [Email], [Website], [Comment], [PostId], [Id] FROM [Comment]" 
        UpdateCommand="UPDATE [Comment] SET [Name] = @Name, [Email] = @Email, [Website] = @Website, [Comment] = @Comment, [PostId] = @PostId WHERE [Id] = @Id">
I'm not entirely sure on what I did wrong here, but my Insert button is not really inserting anything, there seems to be a postback, but the text I punched in the textboxes are still there and nothing went into my Comment table.

Any input would be great.
 
Old August 12th, 2012, 05:27 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Since this is not related to my book directly, you're better off posting this in a general ASP.NET category here: http://p2p.wrox.com/asp-net-4-539/

That said, can you post the remaining code for your SqlDataSource control?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old August 12th, 2012, 05:30 PM
Authorized User
 
Join Date: Jun 2012
Posts: 27
Thanks: 3
Thanked 0 Times in 0 Posts
Default

I apologize for posting this in the wrong place. Will General Dsicussion in mind for next time.

Here is my full DataSource:

Code:
<asp:SqlDataSource ID="SqlDataSource4" runat="server" 
        ConnectionString="<%$ ConnectionStrings:orangefreshConnectionString1 %>" 
        DeleteCommand="DELETE FROM [Comment] WHERE [Id] = @Id" 
        InsertCommand="INSERT INTO [Comment] ([Name], [Email], [Website], [Comment], [PostId]) VALUES (@Name, @Email, @Website, @Comment, @PostId)" 
        SelectCommand="SELECT [Name], [Email], [Website], [Comment], [PostId], [Id] FROM [Comment]" 
        UpdateCommand="UPDATE [Comment] SET [Name] = @Name, [Email] = @Email, [Website] = @Website, [Comment] = @Comment, [PostId] = @PostId WHERE [Id] = @Id">
        <DeleteParameters>
            <asp:Parameter Name="Id" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Email" Type="String" />
            <asp:Parameter Name="Website" Type="String" />
            <asp:Parameter Name="Comment" Type="String" />
            <asp:Parameter Name="PostId" Type="Int32" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Email" Type="String" />
            <asp:Parameter Name="Website" Type="String" />
            <asp:Parameter Name="Comment" Type="String" />
            <asp:Parameter Name="PostId" Type="Int32" />
            <asp:Parameter Name="Id" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
 
Old August 12th, 2012, 06:15 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Can't see anything wrong with this. Could there be a database exception that remains hidden because you redirect after the insert? Just a wild guess though....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old August 12th, 2012, 06:28 PM
Authorized User
 
Join Date: Jun 2012
Posts: 27
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Is there any way to check for such exceptions?

I have other DataSources on that page, I tried removing them to see if there was any conflict... no luck though.
 
Old August 13th, 2012, 12:28 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You would normally see the exception in the page. But you wouldn't if, for example, you redirect the user in an event like ItemInserted, Is your page handling any event of the FormView or SqlDataSource?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old August 13th, 2012, 05:13 AM
Authorized User
 
Join Date: Jun 2012
Posts: 27
Thanks: 3
Thanked 0 Times in 0 Posts
Default

There are no events in this page at all, here is what I'm doing:

First, to get to this page:

Code:
<asp:HyperLink ID="HyperLink1" CssClass="dtlink" runat="server" NavigateUrl='<%# Eval("Id","~/fullpost.aspx?Id={0}") %>'><asp:Literal ID="LitTitle" runat="server" Text='<%# Eval("Title") %>'></asp:Literal></asp:HyperLink>
                        <br />
                        <span class="textSum">
                            <asp:Literal ID="LitSummary" runat="server" Text='<%# Eval("Summary") %>'></asp:Literal></span>
                        <span class="dateTime">
                            <asp:Literal ID="Literal1" runat="server" Text='<%# Eval("CreateDateTime") %>'></asp:Literal></span>
This is inside a repeater on default.aspx, the hyperlink redirects to fullpost with the proper Id on the URL.

Here is a pastebin with the content on fullpost.aspx: http://pastebin.com/LPPGwckU

I'm starting to think it might be a good idea to remake the table and this page...
 
Old August 13th, 2012, 01:42 PM
Authorized User
 
Join Date: Jun 2012
Posts: 27
Thanks: 3
Thanked 0 Times in 0 Posts
Default

OK,

So after trying remaking the table and the the fullpost page, I got served with the same deal, nothing that I punch into those textboxes goes in.

In an act of desperation, I tried remaking the fullpost without a master page, so just a default .aspx page... and voila, it works!

So I start to remove stuff from my master page, and once I removed:
Code:
<uc1:ContactForm ID="ContactForm1" runat="server" />
(I decided to include the contact form in a jquery popup effect on every page, hence it being in the master page)

That contact form from one of the initial chapters is somehow stopping my inserts.

Here is my .ascx file:

Code:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContactForm.ascx.cs" Inherits="controls_ContactForm" %>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table class="style1" runat="server" id="FormTable">
    <tr>
        <td class="style2">
            Name</td>
        <td class="style4">
            <asp:TextBox ID="Name" CssClass="style3" runat="server"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                ErrorMessage="Enter your name" ControlToValidate="Name" 
                CssClass="ErrorMessage">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style2">
            E-mail address</td>
        <td class="style4">
            <asp:TextBox ID="EmailAddress" CssClass="style3" runat="server"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                ControlToValidate="EmailAddress" CssClass="ErrorMessage" Display="Dynamic" 
                ErrorMessage="Enter an e‑mail address">*</asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                ControlToValidate="EmailAddress" CssClass="ErrorMessage" Display="Dynamic" 
                ErrorMessage="Enter a valid e‑mail address" 
                ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
        </td>
    </tr>
    <tr>
        <td class="style2">
            E-mail address again</td>
        <td class="style4">
            <asp:TextBox ID="ConfirmEmailAddress" CssClass="style3" runat="server"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
                ControlToValidate="ConfirmEmailAddress" CssClass="ErrorMessage" 
                Display="Dynamic" ErrorMessage="Confirm the e‑mail address">*</asp:RequiredFieldValidator>
            <asp:CompareValidator ID="CompareValidator1" runat="server" 
                ControlToCompare="EmailAddress" ControlToValidate="ConfirmEmailAddress" 
                CssClass="ErrorMessage" Display="Dynamic" 
                ErrorMessage="Retype the e‑mail address">*</asp:CompareValidator>
        </td>
    </tr>
    <tr>
        <td class="style2">
            Comments</td>
        <td class="style4">
            <asp:TextBox ID="Comments" CssClass="style3" TextMode="MultiLine" runat="server" Height="78px" 
                Width="291px"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
                ControlToValidate="Comments" CssClass="ErrorMessage" Display="Dynamic" 
                ErrorMessage="Enter a comment">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style2">
            &nbsp;</td>
        <td>
            <asp:Button ID="Send" runat="server" Text="Send" onclick="Send_Click" />
            <br />
            <asp:ValidationSummary ID="ValidationSummary1" runat="server" 
                CssClass="ErrorMessage" />
        </td>
        <td>
            &nbsp;</td>
    </tr>
</table>
<asp:Label ID="Message" runat="server" CssClass="messagesent" Width="256" Height="286" Text="Message Sent" Visible="False" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server"
AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
<div class="PleaseWait">
Please Wait...
</div>
</ProgressTemplate>
</asp:UpdateProgress>
I think it's pretty similar to yours, I know the code behind is (don't have the book with me to check out right now)

What do you think is causing this?
 
Old August 17th, 2012, 02:14 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Maybe it's just the validator controls in the contact from preventing you from submitting your data to the server? If that's the case, look into ValidationGroups to separate validation in the contact form from that in the rest of your page.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Smart Tag issue with multiple FormView PeterPeiGuo ASP.NET 3.5 Basics 1 January 2nd, 2009 05:45 AM
Row is null after formview insert?? stapes ASP.NET 2.0 Basics 1 June 27th, 2008 10:19 AM
fileupload control in formview insert item templat rakuntal General .NET 10 August 8th, 2007 12:55 AM
Confirm Page for insert formview mar0364 BOOK: Beginning ASP.NET 2.0 and Databases 0 February 22nd, 2007 10:34 PM
Insert command rajanikrishna ASP.NET 1.0 and 1.1 Basics 0 April 3rd, 2006 08:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.