Click here to Skip to main content
15,904,655 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm following this article, and, this MSDN article, to try to get an <asp:formview> that updates changes to a database, including: browsing through data, editing, inserting new items, and, deleting.

I'm stuck at the problem where I would like to save changes updated within two textboxes (txtSubject and txtBody - see Articles.aspx). However, when I update the text, nothing happens.

I'm using Visual Studio 2008 / .NET 3.5 SP1 / WinXP SP3 / IIS 5.1 on my development pc.

Any ideas please?

:confused:

Articles.aspx
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Articles.aspx.cs" Inherits="Articles" %>
<!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 id="Head1" runat="server">
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <title>@Jon's</title>
</head>
<body>
    <form id="form1" runat="server">
    <h1>@Jon's <img src="images/sofa.jpg" alt="logo"/> </h1 >
    <div style="float:left;width:122px">
        <p style="width: 122px; height: 663px; margin-top: 0px; margin-right: 0px;">
        <a href="About.aspx">About me</a> <br />
        <a href="Diary.aspx">My diary</a> <br />
        <a href="Articles.aspx">Articles</a> <br />
        <a href="Bookmarks.aspx">My bookmarks</a> <br />
        <a href="Promotions.aspx">Promotions</a> <br />
        <a href="Resume.aspx">My resume</a> <br />
        <a href="Warez.aspx">Filesharing</a> <br />
        </p>
      </div>
   <div>
   <asp:Calendar ID="calArticles" runat="server" BackColor="White"
    BorderColor="#999999" CellPadding="4" DayNameFormat="Shortest"
    Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="180px"
    Width="200px" onselectionchanged="calArticles_SelectionChanged">
    <SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />
    <SelectorStyle BackColor="#CCCCCC" />
    <WeekendDayStyle BackColor="#4AA02C" />
    <TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
    <OtherMonthDayStyle ForeColor="#808080" />
    <NextPrevStyle VerticalAlign="Bottom" />
    <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
    <TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
</asp:Calendar>
   <asp:FormView ID="Envelope" runat="server" DataSourceID="SqlDataSource1"
            AllowPaging="True"
            OnItemDeleted = "ArticleEnvelope_Deleted"
            OnItemInserted = "ArticleEnvelope_Inserted"
            OnItemUpdated = "ArticleEnvelope_Updated">
        <HeaderTemplate>
        </HeaderTemplate>
        <EditItemTemplate>
        <p> <asp:TextBox runat="server" Width="1000" ReadOnly="false" ID="txtSubject" Text='<%# Bind("Subject")%>'></asp:TextBox> </p>
        <p> <asp:TextBox runat="server" Height="300" Width="1000" ReadOnly="false" ID="txtBody" TextMode="MultiLine" Text='<%# Bind("EntryText") %>'></asp:TextBox>
        <p>
            &nbsp;<asp:Button ID="btnNew" enabled="false" runat="server" Text="New" CommandName="New" />
            &nbsp;<asp:Button ID="btnEdit" enabled="false" runat="server" Text="Edit" CommandName="Edit" />
            &nbsp;<asp:Button ID="btnSave" enabled="true" runat="server" Text="Save" OnClick="btnSave_Click" CommandName="Update" />
            &nbsp;<asp:Button ID="btnCancel" enabled="true" runat="server" Text="Cancel" CommandName="Cancel" />
            &nbsp;<asp:Button ID="btnRefresh" enabled="false" runat="server" Text="Refresh" />
            &nbsp;<asp:Button ID="btnDelete" enabled="false" runat="server" Text="Delete"/>
        </p>
        </p>
        </EditItemTemplate>
         <InsertItemTemplate>
        <p> <asp:TextBox runat="server" Width="1000" ReadOnly="false" ID="txtSubject" Text='<%# Bind("Subject")%>'></asp:TextBox> </p>
        <p> <asp:TextBox runat="server" Height="300" Width="1000" ReadOnly="false" ID="txtBody" TextMode="MultiLine" Text='<%# Bind("EntryText") %>'></asp:TextBox>
        <p>
            &nbsp;<asp:Button enabled="false" ID="btnNew" runat="server" Text="New" CommandName="New" />
            &nbsp;<asp:Button enabled="false" ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
            &nbsp;<asp:Button enabled="true" ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" CommandName="Update" />
            &nbsp;<asp:Button enabled="true" ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
            &nbsp;<asp:Button enabled="false" ID="btnRefresh" runat="server" Text="Refresh" />
            &nbsp;<asp:Button enabled="false" ID="btnDelete" runat="server" Text="Delete"/>
        </p>
        </p>
        </InsertItemTemplate>
        <ItemTemplate>
        <p> <asp:TextBox runat="server" Width="1000" ReadOnly="false" ID="txtSubject" Text='<%# Bind("Subject")%>'></asp:TextBox> </p>
        <p> <asp:TextBox runat="server" Height="300" Width="1000" ReadOnly="false" ID="txtBody" TextMode="MultiLine" Text='<%# Bind("EntryText") %>'></asp:TextBox>
        <p>
            &nbsp;<asp:Button enabled="true" ID="btnNew" runat="server" Text="New" CommandName="New" />
            &nbsp;<asp:Button enabled="true" ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
            &nbsp;<asp:Button enabled="true" ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" CommandName="Update" />
            &nbsp;<asp:Button enabled="true" ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
            &nbsp;<asp:Button enabled="true" ID="btnRefresh" runat="server" Text="Refresh" />
            &nbsp;<asp:Button enabled="true" ID="btnDelete" runat="server" Text="Delete"/>
        </p>
        </p>
        </ItemTemplate>
        </asp:FormView>
<p>
    <asp:TextBox ID="txtSearch" runat="server" Width="211px"></asp:TextBox>
    <asp:Button ID="btnSearch" runat="server" Text="Search" />

</p>
<p>
    <asp:Label ID="lblFooter" runat="server"></asp:Label>

</p>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:couch_dbConnectionString %>"
        SelectCommand="SELECT [Timestamp], [Subject], [EntryText] FROM [Article]"
        UpdateCommand="UPDATE [Article] SET [Timestamp] = @Timestamp, [Subject] = @Subject, [EntryText] = @EntryText WHERE [EntryID] = @EntryID;"
        DeleteCommand="DELETE FROM [Article] WHERE [EntryID]=@EntryID"
        InsertCommand="INSERT INTO [Article] VALUES [Timestamp]=@Timestamp, [Subject] = @Subject, [EntryText] = @EntryText;"> </asp:SqlDataSource>
</div>
</form>
</body>
</html>

Articles.aspx.cs
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Articles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "Jon's Couch";
        Envelope.DataBind();
    }
    protected void calArticles_SelectionChanged(object sender, EventArgs e)
    {
            SqlDataSource1.SelectCommand =
            System.String.Concat("SELECT [Timestamp], [Subject], [EntryText] FROM [Article] WHERE [Timestamp] >= '" +
            calArticles.SelectedDate.ToString() + "' AND [Timestamp] < (DATEADD(day, 1, [Timestamp]));");
            Envelope.DataBind();
    }
    protected void ArticleEnvelope_Inserted(object sender, EventArgs e)
    {
        Envelope.DataBind();
    }
    protected void ArticleEnvelope_Deleted(object sender, EventArgs e)
    {
    }
    protected void ArticleEnvelope_Updated(object sender, EventArgs e)
    {
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Envelope.DataBind();
    }
}


Source code here, and, I also tried a separate project as proof of concept...doesn't work either.

:confused:
Posted
Updated 29-Aug-10 11:15am
v2

1 solution

You provided source code, but, didn't provide your database backup. So, couldn't try to debug the codes in Visual Studio.

However, are you trying to update data using the btnSave_Click()? If that is so, instead of using the following code to update data:

<asp:Button enabled="true" ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" CommandName="Update" />


C#
protected void btnSave_Click(object sender, EventArgs e)
{
       Envelope.DataBind();
}


You should try to remove the OnClick="btnSave_Click" event handler and see what happens:

<asp:Button enabled="true" ID="btnSave" runat="server" Text="Save" CommandName="Update" />


Because, using the "CommandName" property of the Button, the Asp.net runtime binds the Button's update command with the Update command of the SqlDataSource and executes the Update Query.

But, if you really want to use the btnSaveClick() method , you should write your own codes to update data (Like the following you've already written):

C#
protected void calArticles_SelectionChanged(object sender, EventArgs e)
{
           SqlDataSource1.SelectCommand =
           System.String.Concat("SELECT [Timestamp], [Subject], [EntryText] FROM [Article] WHERE [Timestamp] >= '" +
           calArticles.SelectedDate.ToString() + "' AND [Timestamp] < (DATEADD(day, 1, [Timestamp]));");
           Envelope.DataBind();
}



I can suggest you the following link to see how to update data using FormView and SqlDataSource:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.aspx[^]
 
Share this answer
 
Comments
jon-80 30-Aug-10 12:26pm    
"You provided source code, but, didn't provide your database backup."
No problem, I uploaded the .mdf file to http://cid-b712073b3513eb8e.office.live.com/self.aspx/.Public/dumps/couch%5E_db.rar

"You should try to remove the OnClick="btnSave_Click" event handler and see what happens"
When I tried that the form data is still not updated, and, I am not sure how to get the <formview> to read the @EntryID parameter:

UpdateCommand="UPDATE [Article] SET [Timestamp] = @Timestamp, [Subject] = @Subject, [EntryText] = @EntryText WHERE [EntryID] = @EntryID;"

The code that I tried, like the one within protected void calArticles_SelectionChanged, updates the SQL Statement correctly, however, I'm missing a link. How do I ask the <formview> to update the data based on this SQL Query. That is, the data within the <formview> remains unchanged, notwithstanding calArticles selection is changed.
jon-80 30-Aug-10 12:57pm    
"That is, the data within the remains unchanged, notwithstanding calArticles selection is changed"

This problem is freaking me out (:confused), coz it's intermittent. One minute the calendar updates the data when debugging. This seems to be an SQL issue however (maybe my logic)

SELECT [Timestamp], [Subject], [EntryText] FROM [Article] WHERE [Timestamp] >= '8/25/2010 12:00:00 AM' AND [Timestamp] < (DATEADD(day, 1, [Timestamp]));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900