I have a formview that uses an object data source with insert and update methods specified. In update method I return a value (bool or int) to specify if updating database was successful or not. Is there any way I can check this value in ItemUpdated event
of formview in order to display appropriate message to user?
I am not using SQL but Oracle database (which I hate!) I think in SQL if SET NOCOUNT is OFF then it returns the number of rows updated otherwise it won't. Not sure if Oracle is casuing this But I have simiar structure as your sample.
NoBullMan
Participant
1022 Points
788 Posts
Check update status in formview
May 03, 2012 07:22 PM|LINK
Hi,
I have a formview that uses an object data source with insert and update methods specified. In update method I return a value (bool or int) to specify if updating database was successful or not. Is there any way I can check this value in ItemUpdated event of formview in order to display appropriate message to user?
Madhu1234
Participant
1380 Points
287 Posts
Re: Check update status in formview
May 03, 2012 09:10 PM|LINK
Instead of returning a value you can dolike this in ItemUpdated event of the formview to check whether data is updated successfully or not.
protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
{
if (e.Exception != null)
{
lblError.Text = e.Exception.Message;
}
}
Thanks
Madhu
NoBullMan
Participant
1022 Points
788 Posts
Re: Check update status in formview
May 03, 2012 11:39 PM|LINK
This was my first option and I posted an issue about it but since I got no response, I thought I try this.
This is the thread about using e.Exception:
http://forums.asp.net/t/1799785.aspx/1?FormView+ItemUpdated+problem
asp.netx.0lo...
Member
362 Points
71 Posts
Re: Check update status in formview
May 07, 2012 09:57 AM|LINK
I have tried a sample without any problems based on your description in this post.
Here are the sample.
Default.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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></title> </head> <body> <form id="form1" runat="server"> <div> <asp:FormView ID="FormView1" runat="server" DataKeyNames="CourseID" DataSourceID="SqlDataSource1" onitemupdated="FormView1_ItemUpdated"> <EditItemTemplate> CourseID: <asp:Label ID="CourseIDLabel1" runat="server" Text='<%# Eval("CourseID") %>' /> <br /> Title: <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' /> <br /> Credits: <asp:TextBox ID="CreditsTextBox" runat="server" Text='<%# Bind("Credits") %>' /> <br /> DepartmentID: <asp:TextBox ID="DepartmentIDTextBox" runat="server" Text='<%# Bind("DepartmentID") %>' /> <br /> <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" /> <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /> </EditItemTemplate> <InsertItemTemplate> CourseID: <asp:TextBox ID="CourseIDTextBox" runat="server" Text='<%# Bind("CourseID") %>' /> <br /> Title: <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' /> <br /> Credits: <asp:TextBox ID="CreditsTextBox" runat="server" Text='<%# Bind("Credits") %>' /> <br /> DepartmentID: <asp:TextBox ID="DepartmentIDTextBox" runat="server" Text='<%# Bind("DepartmentID") %>' /> <br /> <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" /> <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /> </InsertItemTemplate> <ItemTemplate> CourseID: <asp:Label ID="CourseIDLabel" runat="server" Text='<%# Eval("CourseID") %>' /> <br /> Title: <asp:Label ID="TitleLabel" runat="server" Text='<%# Bind("Title") %>' /> <br /> Credits: <asp:Label ID="CreditsLabel" runat="server" Text='<%# Bind("Credits") %>' /> <br /> DepartmentID: <asp:Label ID="DepartmentIDLabel" runat="server" Text='<%# Bind("DepartmentID") %>' /> <br /> <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" /> <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" /> <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New" /> </ItemTemplate> </asp:FormView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:SchoolConnectionString %>" DeleteCommand="DELETE FROM [Course] WHERE [CourseID] = @original_CourseID AND [Title] = @original_Title AND [Credits] = @original_Credits AND [DepartmentID] = @original_DepartmentID" InsertCommand="INSERT INTO [Course] ([CourseID], [Title], [Credits], [DepartmentID]) VALUES (@CourseID, @Title, @Credits, @DepartmentID)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [Course]" UpdateCommand="UPDATE [Course] SET [Title] = @Title, [Credits] = @Credits, [DepartmentID] = @DepartmentID WHERE [CourseID] = @original_CourseID AND [Title] = @original_Title AND [Credits] = @original_Credits AND [DepartmentID] = @original_DepartmentID"> <DeleteParameters> <asp:Parameter Name="original_CourseID" Type="Int32" /> <asp:Parameter Name="original_Title" Type="String" /> <asp:Parameter Name="original_Credits" Type="Int32" /> <asp:Parameter Name="original_DepartmentID" Type="Int32" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="CourseID" Type="Int32" /> <asp:Parameter Name="Title" Type="String" /> <asp:Parameter Name="Credits" Type="Int32" /> <asp:Parameter Name="DepartmentID" Type="Int32" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="Title" Type="String" /> <asp:Parameter Name="Credits" Type="Int32" /> <asp:Parameter Name="DepartmentID" Type="Int32" /> <asp:Parameter Name="original_CourseID" Type="Int32" /> <asp:Parameter Name="original_Title" Type="String" /> <asp:Parameter Name="original_Credits" Type="Int32" /> <asp:Parameter Name="original_DepartmentID" Type="Int32" /> </UpdateParameters> </asp:SqlDataSource> <br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html> Default.aspx.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e) { if (e.Exception == null) { if (e.AffectedRows == 1) { Label1.Text = "Update successfully!"; } else { Label1.Text = "An error occurred during the update operation. "; } } else { Label1.Text = e.Exception.Message; } } } //After updated, the Label can show Update successfully!NoBullMan
Participant
1022 Points
788 Posts
Re: Check update status in formview
May 09, 2012 10:53 PM|LINK
Thanks for the reply.
I am not using SQL but Oracle database (which I hate!) I think in SQL if SET NOCOUNT is OFF then it returns the number of rows updated otherwise it won't. Not sure if Oracle is casuing this But I have simiar structure as your sample.