Update method always gets old values from a form view

B

bob

Hi,

I have an object data source attached to a form view. The data source calls
bll's update method. I can't figure out why the bll's update method never
gets updated fields - it always gets original values. I made sure that
fields are two-way bound in the form's <EditItemTemplate>. I don't get any
runtime errors. I set a breakpoint in the update method and can clearly see
that old values are passed to it.

My bll's update method is declared as follows:

[System.ComponentModel.DataObjectMethodAttribute
(System.ComponentModel.DataObjectMethodType.Update, true)]
public bool UpdateItem(int itemID, DateTime startDate, DateTime endDate,
string content);

The form view:

<asp:FormView ID="itemFormView" runat="server" DataKeyNames="ItemID"
DataSourceID="itemDataSource">
<EditItemTemplate>
Start Date:
<asp:TextBox ID="StartDateTextBox" runat="server" Text='<%#
Bind("StartDate") %>'></asp:TextBox><br />
End Date:
<asp:TextBox ID="EndDateTextBox" runat="server" Text='<%#
Bind("EndDate") %>'></asp:TextBox><br />
Content:
<asp:TextBox ID="ContentTextBox" runat="server" Text='<%#
Bind("Content") %>'></asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="True" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
[...]
</InsertItemTemplate>
<ItemTemplate>
[...]
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="itemDataSource" runat="server"
DeleteMethod="DeleteItem"
InsertMethod="AddItem" OldValuesParameterFormatString="{0}"
TypeName="ItemsBLL" UpdateMethod="UpdateItem"
SelectMethod="GetItemByItemID" >
<UpdateParameters>
<asp:parameter Name="startDate" Type="DateTime" />
<asp:parameter Name="endDate" Type="DateTime" />
<asp:parameter Name="content" Type="String" />
</UpdateParameters>
[...]
</asp:ObjectDataSource>


I'd appreciate any help/pointers because I'm stuck at this point.

Thanks,
Bob.
 
W

wisccal

Hi Bob,

I can't reproduce your problem. How do you determine the the ID of the
record to update? Here is my example that works just fine on my
machine:


Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication2._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">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ItemDDL" AutoPostback="true" runat="server">
<asp:ListItem Value="1" Text="This one" />
<asp:ListItem Value="2" Text="That one" />
<asp:ListItem Value="3" Text="Maybe here" />
<asp:ListItem Value="4" Text="Or there" />
</asp:DropDownList>
<asp:FormView ID="itemFormView" runat="server" DataKeyNames="ItemID"
DataSourceID="itemDataSource">
<EditItemTemplate>
Start Date:
<asp:TextBox ID="StartDateTextBox" runat="server"
Text='<%#
Bind("StartDate") %>'></asp:TextBox><br />
End Date:
<asp:TextBox ID="EndDateTextBox" runat="server" Text='<
%#
Bind("EndDate") %>'></asp:TextBox><br />
Content:
<asp:TextBox ID="ContentTextBox" runat="server" Text='<
%#
Bind("Content") %>'></asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="False" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
Start Date:
<asp:TextBox ID="StartDateTextBox" runat="server"
Text='<%#
Bind("StartDate") %>'></asp:TextBox><br />
End Date:
<asp:TextBox ID="EndDateTextBox" runat="server" Text='<
%#
Bind("EndDate") %>'></asp:TextBox><br />
Content:
<asp:TextBox ID="ContentTextBox" runat="server" Text='<
%#
Bind("Content") %>'></asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="False" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
Start Date:
<asp:TextBox ID="StartDateTextBox" runat="server"
Text='<%#
Bind("StartDate") %>'></asp:TextBox><br />
End Date:
<asp:TextBox ID="EndDateTextBox" runat="server" Text='<
%#
Bind("EndDate") %>'></asp:TextBox><br />
Content:
<asp:TextBox ID="ContentTextBox" runat="server" Text='<
%#
Bind("Content") %>'></asp:TextBox><br />
<asp:LinkButton ID="EditButton" runat="server"
CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="itemDataSource" runat="server"
OldValuesParameterFormatString="{0}"
TypeName="Webapplication2.ItemsBLL" UpdateMethod="UpdateItem"
SelectMethod="GetItemByItemID" >
<UpdateParameters>
<asp:ControlParameter ControlID="ItemDDL" Name="itemID"
Type="Int32" />
<asp:parameter Name="startDate" Type="DateTime" />
<asp:parameter Name="endDate" Type="DateTime" />
<asp:parameter Name="content" Type="String" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="ItemDDL" Name="itemID"
Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>


Default.aspx.cs:

using System;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.UI;

namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
}

public class ItemsBLL {
private static System.Collections.Generic.List<Item> items =
new System.Collections.Generic.List<Item>() {
new Item {ItemID = 1, StartDate = new DateTime(2007,5,3),
EndDate = new DateTime(2007,11,12), Content="Great News"},
new Item {ItemID = 2, StartDate = new DateTime(2008,1,1),
EndDate = new DateTime(2008,1,31), Content="Bad News"},
new Item {ItemID = 4, StartDate = new DateTime(1999,7,4),
EndDate = DateTime.Now, Content="Not Sure"}
};


[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select,
true)]
public Item GetItemByItemID(int itemID)
{
var its = items.Where(i => i.ItemID == itemID);
if(its.Count() > 0) {
return its.First();
}
return null;
}

[System.ComponentModel.DataObjectMethodAttribute
(System.ComponentModel.DataObjectMethodType.Update, true)]
public bool UpdateItem(int itemID, DateTime startDate,
DateTime endDate, string content) {
var its = items.Where(i => i.ItemID == itemID);
if(its.Count() > 0) {
Item it = its.First();
it.StartDate = startDate;
it.EndDate = endDate;
it.Content = content;
return true;
}
return false;
}
}
public class Item {
public int ItemID {
get;set;
}
public DateTime StartDate {
get;set;
}
public DateTime EndDate {
get;set;
}
public string Content {
get;set;
}
}
}


===========
Regards,
Steve
www.stkomp.com
Hi,

I have an object data source attached to a form view. The data source calls
bll's update method. I can't figure out why the bll's update method never
gets updated fields - it always gets original values. I made sure that
fields are two-way bound in the form's <EditItemTemplate>. I don't get any
runtime errors. I set a breakpoint in the update method and can clearly see
that old values are passed to it.

My bll's update method is declared as follows:

[System.ComponentModel.DataObjectMethodAttribute
(System.ComponentModel.DataObjectMethodType.Update, true)]
public bool UpdateItem(int itemID, DateTime startDate, DateTime endDate,
string content);

The form view:

<asp:FormView ID="itemFormView" runat="server" DataKeyNames="ItemID"
DataSourceID="itemDataSource">
<EditItemTemplate>
Start Date:
<asp:TextBox ID="StartDateTextBox" runat="server" Text='<%#
Bind("StartDate") %>'></asp:TextBox><br />
End Date:
<asp:TextBox ID="EndDateTextBox" runat="server" Text='<%#
Bind("EndDate") %>'></asp:TextBox><br />
Content:
<asp:TextBox ID="ContentTextBox" runat="server" Text='<%#
Bind("Content") %>'></asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="True" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
[...]
</InsertItemTemplate>
<ItemTemplate>
[...]
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="itemDataSource" runat="server"
DeleteMethod="DeleteItem"
InsertMethod="AddItem" OldValuesParameterFormatString="{0}"
TypeName="ItemsBLL" UpdateMethod="UpdateItem"
SelectMethod="GetItemByItemID" >
<UpdateParameters>
<asp:parameter Name="startDate" Type="DateTime" />
<asp:parameter Name="endDate" Type="DateTime" />
<asp:parameter Name="content" Type="String" />
</UpdateParameters>
[...]
</asp:ObjectDataSource>


I'd appreciate any help/pointers because I'm stuck at this point.

Thanks,
Bob.
 
B

bob

Thanks for the reply. The ItemID is read from another control which is a
dropdownlist.

I have created similar pages with the same BLL in the past and had no
problems. I just created a replacement page that is almost identical to the
one I had problems with and it works as expected. I could use the new page
instead but it bothers me that I can't figure out why the original did not
work. In insert mode all parameters passed to BLL were null or
uninitialized and in update mode old values were passed.
I wasted a whole day trying to debug it. I'm afraid that something like
that can happen on the deployment machine. The problem is that I do not
know what else to do in order to debug it. It almost looks like the
framework is generating (or using from a cache) code that I do not expect.

Any hints on how to debug it? Although I have another page working well
(and doing what the original was supposed to) I'm still holding on to the
original hoping to find out what the problem is.

Thanks

Hi Bob,

I can't reproduce your problem. How do you determine the the ID of the
record to update? Here is my example that works just fine on my
machine:


Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication2._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">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ItemDDL" AutoPostback="true" runat="server">
<asp:ListItem Value="1" Text="This one" />
<asp:ListItem Value="2" Text="That one" />
<asp:ListItem Value="3" Text="Maybe here" />
<asp:ListItem Value="4" Text="Or there" />
</asp:DropDownList>
<asp:FormView ID="itemFormView" runat="server" DataKeyNames="ItemID"
DataSourceID="itemDataSource">
<EditItemTemplate>
Start Date:
<asp:TextBox ID="StartDateTextBox" runat="server"
Text='<%#
Bind("StartDate") %>'></asp:TextBox><br />
End Date:
<asp:TextBox ID="EndDateTextBox" runat="server" Text='<
%#
Bind("EndDate") %>'></asp:TextBox><br />
Content:
<asp:TextBox ID="ContentTextBox" runat="server" Text='<
%#
Bind("Content") %>'></asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="False" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
Start Date:
<asp:TextBox ID="StartDateTextBox" runat="server"
Text='<%#
Bind("StartDate") %>'></asp:TextBox><br />
End Date:
<asp:TextBox ID="EndDateTextBox" runat="server" Text='<
%#
Bind("EndDate") %>'></asp:TextBox><br />
Content:
<asp:TextBox ID="ContentTextBox" runat="server" Text='<
%#
Bind("Content") %>'></asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="False" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
Start Date:
<asp:TextBox ID="StartDateTextBox" runat="server"
Text='<%#
Bind("StartDate") %>'></asp:TextBox><br />
End Date:
<asp:TextBox ID="EndDateTextBox" runat="server" Text='<
%#
Bind("EndDate") %>'></asp:TextBox><br />
Content:
<asp:TextBox ID="ContentTextBox" runat="server" Text='<
%#
Bind("Content") %>'></asp:TextBox><br />
<asp:LinkButton ID="EditButton" runat="server"
CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="itemDataSource" runat="server"
OldValuesParameterFormatString="{0}"
TypeName="Webapplication2.ItemsBLL" UpdateMethod="UpdateItem"
SelectMethod="GetItemByItemID" >
<UpdateParameters>
<asp:ControlParameter ControlID="ItemDDL" Name="itemID"
Type="Int32" />
<asp:parameter Name="startDate" Type="DateTime" />
<asp:parameter Name="endDate" Type="DateTime" />
<asp:parameter Name="content" Type="String" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="ItemDDL" Name="itemID"
Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>


Default.aspx.cs:

using System;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.UI;

namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
}

public class ItemsBLL {
private static System.Collections.Generic.List<Item> items =
new System.Collections.Generic.List<Item>() {
new Item {ItemID = 1, StartDate = new DateTime(2007,5,3),
EndDate = new DateTime(2007,11,12), Content="Great News"},
new Item {ItemID = 2, StartDate = new DateTime(2008,1,1),
EndDate = new DateTime(2008,1,31), Content="Bad News"},
new Item {ItemID = 4, StartDate = new DateTime(1999,7,4),
EndDate = DateTime.Now, Content="Not Sure"}
};


[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select,
true)]
public Item GetItemByItemID(int itemID)
{
var its = items.Where(i => i.ItemID == itemID);
if(its.Count() > 0) {
return its.First();
}
return null;
}

[System.ComponentModel.DataObjectMethodAttribute
(System.ComponentModel.DataObjectMethodType.Update, true)]
public bool UpdateItem(int itemID, DateTime startDate,
DateTime endDate, string content) {
var its = items.Where(i => i.ItemID == itemID);
if(its.Count() > 0) {
Item it = its.First();
it.StartDate = startDate;
it.EndDate = endDate;
it.Content = content;
return true;
}
return false;
}
}
public class Item {
public int ItemID {
get;set;
}
public DateTime StartDate {
get;set;
}
public DateTime EndDate {
get;set;
}
public string Content {
get;set;
}
}
}


===========
Regards,
Steve
www.stkomp.com
Hi,

I have an object data source attached to a form view. The data source
calls
bll's update method. I can't figure out why the bll's update method
never
gets updated fields - it always gets original values. I made sure that
fields are two-way bound in the form's <EditItemTemplate>. I don't get
any
runtime errors. I set a breakpoint in the update method and can clearly
see
that old values are passed to it.

My bll's update method is declared as follows:

[System.ComponentModel.DataObjectMethodAttribute
(System.ComponentModel.DataObjectMethodType.Update, true)]
public bool UpdateItem(int itemID, DateTime startDate, DateTime
endDate,
string content);

The form view:

<asp:FormView ID="itemFormView" runat="server" DataKeyNames="ItemID"
DataSourceID="itemDataSource">
<EditItemTemplate>
Start Date:
<asp:TextBox ID="StartDateTextBox" runat="server" Text='<%#
Bind("StartDate") %>'></asp:TextBox><br />
End Date:
<asp:TextBox ID="EndDateTextBox" runat="server" Text='<%#
Bind("EndDate") %>'></asp:TextBox><br />
Content:
<asp:TextBox ID="ContentTextBox" runat="server" Text='<%#
Bind("Content") %>'></asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="True" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
[...]
</InsertItemTemplate>
<ItemTemplate>
[...]
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="itemDataSource" runat="server"
DeleteMethod="DeleteItem"
InsertMethod="AddItem" OldValuesParameterFormatString="{0}"
TypeName="ItemsBLL" UpdateMethod="UpdateItem"
SelectMethod="GetItemByItemID" >
<UpdateParameters>
<asp:parameter Name="startDate" Type="DateTime" />
<asp:parameter Name="endDate" Type="DateTime" />
<asp:parameter Name="content" Type="String" />
</UpdateParameters>
[...]
</asp:ObjectDataSource>


I'd appreciate any help/pointers because I'm stuck at this point.

Thanks,
Bob.
 
W

wisccal

Hi Bob,

Without seeing your code, it's not that easy to pinpoint the problem.
You're saying you created an <almost> identical page. So what is
different between the two pages? If codebehind is exactly the same,
then the problem is most likely the web page file.

Also, I would try and hook into the different events of the
ObjectDataSource control like OnObjectCreated, OnDataBinding, etc. An
example would be:

in your aspx file:
<asp:ObjectDataSource ID="itemDataSource"
OnObjectCreated="itemDataSource_ObjectCreated" ...>

in the codebehind:
protected void itemDataSource_ObjectCreated(object s,
ObjectDataSourceEventArgs e)
{
///...test your objects. E.g. what is the value of
e.ObjectInstance, of your controls?
}

HTH.

==========
Regards,
Steve
www.stkomp.com
Thanks for the reply. The ItemID is read from another control which is a
dropdownlist.

I have created similar pages with the same BLL in the past and had no
problems. I just created a replacement page that is almost identical to the
one I had problems with and it works as expected. I could use the new page
instead but it bothers me that I can't figure out why the original did not
work. In insert mode all parameters passed to BLL were null or
uninitialized and in update mode old values were passed.
I wasted a whole day trying to debug it. I'm afraid that something like
that can happen on the deployment machine. The problem is that I do not
know what else to do in order to debug it. It almost looks like the
framework is generating (or using from a cache) code that I do not expect.

Any hints on how to debug it? Although I have another page working well
(and doing what the original was supposed to) I'm still holding on to the
original hoping to find out what the problem is.

Thanks

Hi Bob,

I can't reproduce your problem. How do you determine the the ID of the
record to update? Here is my example that works just fine on my
machine:


Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication2._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">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ItemDDL" AutoPostback="true" runat="server">
<asp:ListItem Value="1" Text="This one" />
<asp:ListItem Value="2" Text="That one" />
<asp:ListItem Value="3" Text="Maybe here" />
<asp:ListItem Value="4" Text="Or there" />
</asp:DropDownList>
<asp:FormView ID="itemFormView" runat="server" DataKeyNames="ItemID"
DataSourceID="itemDataSource">
<EditItemTemplate>
Start Date:
<asp:TextBox ID="StartDateTextBox" runat="server"
Text='<%#
Bind("StartDate") %>'></asp:TextBox><br />
End Date:
<asp:TextBox ID="EndDateTextBox" runat="server" Text='<
%#
Bind("EndDate") %>'></asp:TextBox><br />
Content:
<asp:TextBox ID="ContentTextBox" runat="server" Text='<
%#
Bind("Content") %>'></asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="False" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
Start Date:
<asp:TextBox ID="StartDateTextBox" runat="server"
Text='<%#
Bind("StartDate") %>'></asp:TextBox><br />
End Date:
<asp:TextBox ID="EndDateTextBox" runat="server" Text='<
%#
Bind("EndDate") %>'></asp:TextBox><br />
Content:
<asp:TextBox ID="ContentTextBox" runat="server" Text='<
%#
Bind("Content") %>'></asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="False" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
Start Date:
<asp:TextBox ID="StartDateTextBox" runat="server"
Text='<%#
Bind("StartDate") %>'></asp:TextBox><br />
End Date:
<asp:TextBox ID="EndDateTextBox" runat="server" Text='<
%#
Bind("EndDate") %>'></asp:TextBox><br />
Content:
<asp:TextBox ID="ContentTextBox" runat="server" Text='<
%#
Bind("Content") %>'></asp:TextBox><br />
<asp:LinkButton ID="EditButton" runat="server"
CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="itemDataSource" runat="server"
OldValuesParameterFormatString="{0}"
TypeName="Webapplication2.ItemsBLL" UpdateMethod="UpdateItem"
SelectMethod="GetItemByItemID" >
<UpdateParameters>
<asp:ControlParameter ControlID="ItemDDL" Name="itemID"
Type="Int32" />
<asp:parameter Name="startDate" Type="DateTime" />
<asp:parameter Name="endDate" Type="DateTime" />
<asp:parameter Name="content" Type="String" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="ItemDDL" Name="itemID"
Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>


Default.aspx.cs:

using System;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.UI;

namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
}

public class ItemsBLL {
private static System.Collections.Generic.List<Item> items =
new System.Collections.Generic.List<Item>() {
new Item {ItemID = 1, StartDate = new DateTime(2007,5,3),
EndDate = new DateTime(2007,11,12), Content="Great News"},
new Item {ItemID = 2, StartDate = new DateTime(2008,1,1),
EndDate = new DateTime(2008,1,31), Content="Bad News"},
new Item {ItemID = 4, StartDate = new DateTime(1999,7,4),
EndDate = DateTime.Now, Content="Not Sure"}
};


[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select,
true)]
public Item GetItemByItemID(int itemID)
{
var its = items.Where(i => i.ItemID == itemID);
if(its.Count() > 0) {
return its.First();
}
return null;
}

[System.ComponentModel.DataObjectMethodAttribute
(System.ComponentModel.DataObjectMethodType.Update, true)]
public bool UpdateItem(int itemID, DateTime startDate,
DateTime endDate, string content) {
var its = items.Where(i => i.ItemID == itemID);
if(its.Count() > 0) {
Item it = its.First();
it.StartDate = startDate;
it.EndDate = endDate;
it.Content = content;
return true;
}
return false;
}
}
public class Item {
public int ItemID {
get;set;
}
public DateTime StartDate {
get;set;
}
public DateTime EndDate {
get;set;
}
public string Content {
get;set;
}
}
}


===========
Regards,
Steve
www.stkomp.com
Hi,

I have an object data source attached to a form view. The data source
calls
bll's update method. I can't figure out why the bll's update method
never
gets updated fields - it always gets original values. I made sure that
fields are two-way bound in the form's <EditItemTemplate>. I don't get
any
runtime errors. I set a breakpoint in the update method and can clearly
see
that old values are passed to it.

My bll's update method is declared as follows:

[System.ComponentModel.DataObjectMethodAttribute
(System.ComponentModel.DataObjectMethodType.Update, true)]
public bool UpdateItem(int itemID, DateTime startDate, DateTime
endDate,
string content);

The form view:

<asp:FormView ID="itemFormView" runat="server" DataKeyNames="ItemID"
DataSourceID="itemDataSource">
<EditItemTemplate>
Start Date:
<asp:TextBox ID="StartDateTextBox" runat="server" Text='<%#
Bind("StartDate") %>'></asp:TextBox><br />
End Date:
<asp:TextBox ID="EndDateTextBox" runat="server" Text='<%#
Bind("EndDate") %>'></asp:TextBox><br />
Content:
<asp:TextBox ID="ContentTextBox" runat="server" Text='<%#
Bind("Content") %>'></asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="True" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
[...]
</InsertItemTemplate>
<ItemTemplate>
[...]
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="itemDataSource" runat="server"
DeleteMethod="DeleteItem"
InsertMethod="AddItem" OldValuesParameterFormatString="{0}"
TypeName="ItemsBLL" UpdateMethod="UpdateItem"
SelectMethod="GetItemByItemID" >
<UpdateParameters>
<asp:parameter Name="startDate" Type="DateTime" />
<asp:parameter Name="endDate" Type="DateTime" />
<asp:parameter Name="content" Type="String" />
</UpdateParameters>
[...]
</asp:ObjectDataSource>


I'd appreciate any help/pointers because I'm stuck at this point.

Thanks,
Bob.
 
B

bob

Steve,

Thanks for your help. I managed to find the problem. Thanks for bringing
up "<almost>". I [reluctantly] went back to the problematic code and looked
for differences. I found a single line in code-behind that called
DataBind() on the form view. The call took place in Page_Load() on post
back. It all makes sense now. By binding the form view I was [implicitly]
restoring original values and initializing new ones with empty/null before
the BLL's methods were called by the framework.
I spend a lot of time looking into BLL, aspx page, and other event handlers
and ignoring the simplest one which was Page_Load(). I think I learned my
lesson.

Thanks,
Bob

Hi Bob,

Without seeing your code, it's not that easy to pinpoint the problem.
You're saying you created an <almost> identical page. So what is
different between the two pages? If codebehind is exactly the same,
then the problem is most likely the web page file.

Also, I would try and hook into the different events of the
ObjectDataSource control like OnObjectCreated, OnDataBinding, etc. An
example would be:

in your aspx file:
<asp:ObjectDataSource ID="itemDataSource"
OnObjectCreated="itemDataSource_ObjectCreated" ...>

in the codebehind:
protected void itemDataSource_ObjectCreated(object s,
ObjectDataSourceEventArgs e)
{
///...test your objects. E.g. what is the value of
e.ObjectInstance, of your controls?
}

HTH.

==========
Regards,
Steve
www.stkomp.com
Thanks for the reply. The ItemID is read from another control which is a
dropdownlist.

I have created similar pages with the same BLL in the past and had no
problems. I just created a replacement page that is almost identical to
the
one I had problems with and it works as expected. I could use the new
page
instead but it bothers me that I can't figure out why the original did
not
work. In insert mode all parameters passed to BLL were null or
uninitialized and in update mode old values were passed.
I wasted a whole day trying to debug it. I'm afraid that something like
that can happen on the deployment machine. The problem is that I do not
know what else to do in order to debug it. It almost looks like the
framework is generating (or using from a cache) code that I do not
expect.

Any hints on how to debug it? Although I have another page working well
(and doing what the original was supposed to) I'm still holding on to the
original hoping to find out what the problem is.

Thanks

Hi Bob,

I can't reproduce your problem. How do you determine the the ID of the
record to update? Here is my example that works just fine on my
machine:


Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication2._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">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ItemDDL" AutoPostback="true" runat="server">
<asp:ListItem Value="1" Text="This one" />
<asp:ListItem Value="2" Text="That one" />
<asp:ListItem Value="3" Text="Maybe here" />
<asp:ListItem Value="4" Text="Or there" />
</asp:DropDownList>
<asp:FormView ID="itemFormView" runat="server" DataKeyNames="ItemID"
DataSourceID="itemDataSource">
<EditItemTemplate>
Start Date:
<asp:TextBox ID="StartDateTextBox" runat="server"
Text='<%#
Bind("StartDate") %>'></asp:TextBox><br />
End Date:
<asp:TextBox ID="EndDateTextBox" runat="server" Text='<
%#
Bind("EndDate") %>'></asp:TextBox><br />
Content:
<asp:TextBox ID="ContentTextBox" runat="server" Text='<
%#
Bind("Content") %>'></asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="False" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
Start Date:
<asp:TextBox ID="StartDateTextBox" runat="server"
Text='<%#
Bind("StartDate") %>'></asp:TextBox><br />
End Date:
<asp:TextBox ID="EndDateTextBox" runat="server" Text='<
%#
Bind("EndDate") %>'></asp:TextBox><br />
Content:
<asp:TextBox ID="ContentTextBox" runat="server" Text='<
%#
Bind("Content") %>'></asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="False" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
Start Date:
<asp:TextBox ID="StartDateTextBox" runat="server"
Text='<%#
Bind("StartDate") %>'></asp:TextBox><br />
End Date:
<asp:TextBox ID="EndDateTextBox" runat="server" Text='<
%#
Bind("EndDate") %>'></asp:TextBox><br />
Content:
<asp:TextBox ID="ContentTextBox" runat="server" Text='<
%#
Bind("Content") %>'></asp:TextBox><br />
<asp:LinkButton ID="EditButton" runat="server"
CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="itemDataSource" runat="server"
OldValuesParameterFormatString="{0}"
TypeName="Webapplication2.ItemsBLL" UpdateMethod="UpdateItem"
SelectMethod="GetItemByItemID" >
<UpdateParameters>
<asp:ControlParameter ControlID="ItemDDL" Name="itemID"
Type="Int32" />
<asp:parameter Name="startDate" Type="DateTime" />
<asp:parameter Name="endDate" Type="DateTime" />
<asp:parameter Name="content" Type="String" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="ItemDDL" Name="itemID"
Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>


Default.aspx.cs:

using System;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.UI;

namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{
}

public class ItemsBLL {
private static System.Collections.Generic.List<Item> items =
new System.Collections.Generic.List<Item>() {
new Item {ItemID = 1, StartDate = new DateTime(2007,5,3),
EndDate = new DateTime(2007,11,12), Content="Great News"},
new Item {ItemID = 2, StartDate = new DateTime(2008,1,1),
EndDate = new DateTime(2008,1,31), Content="Bad News"},
new Item {ItemID = 4, StartDate = new DateTime(1999,7,4),
EndDate = DateTime.Now, Content="Not Sure"}
};


[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select,
true)]
public Item GetItemByItemID(int itemID)
{
var its = items.Where(i => i.ItemID == itemID);
if(its.Count() > 0) {
return its.First();
}
return null;
}

[System.ComponentModel.DataObjectMethodAttribute
(System.ComponentModel.DataObjectMethodType.Update, true)]
public bool UpdateItem(int itemID, DateTime startDate,
DateTime endDate, string content) {
var its = items.Where(i => i.ItemID == itemID);
if(its.Count() > 0) {
Item it = its.First();
it.StartDate = startDate;
it.EndDate = endDate;
it.Content = content;
return true;
}
return false;
}
}
public class Item {
public int ItemID {
get;set;
}
public DateTime StartDate {
get;set;
}
public DateTime EndDate {
get;set;
}
public string Content {
get;set;
}
}
}


===========
Regards,
Steve
www.stkomp.com

bob wrote:
Hi,

I have an object data source attached to a form view. The data source
calls
bll's update method. I can't figure out why the bll's update method
never
gets updated fields - it always gets original values. I made sure
that
fields are two-way bound in the form's <EditItemTemplate>. I don't
get
any
runtime errors. I set a breakpoint in the update method and can
clearly
see
that old values are passed to it.

My bll's update method is declared as follows:

[System.ComponentModel.DataObjectMethodAttribute
(System.ComponentModel.DataObjectMethodType.Update, true)]
public bool UpdateItem(int itemID, DateTime startDate, DateTime
endDate,
string content);

The form view:

<asp:FormView ID="itemFormView" runat="server" DataKeyNames="ItemID"
DataSourceID="itemDataSource">
<EditItemTemplate>
Start Date:
<asp:TextBox ID="StartDateTextBox" runat="server" Text='<%#
Bind("StartDate") %>'></asp:TextBox><br />
End Date:
<asp:TextBox ID="EndDateTextBox" runat="server" Text='<%#
Bind("EndDate") %>'></asp:TextBox><br />
Content:
<asp:TextBox ID="ContentTextBox" runat="server" Text='<%#
Bind("Content") %>'></asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server"
CausesValidation="True" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
[...]
</InsertItemTemplate>
<ItemTemplate>
[...]
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="itemDataSource" runat="server"
DeleteMethod="DeleteItem"
InsertMethod="AddItem" OldValuesParameterFormatString="{0}"
TypeName="ItemsBLL" UpdateMethod="UpdateItem"
SelectMethod="GetItemByItemID" >
<UpdateParameters>
<asp:parameter Name="startDate" Type="DateTime" />
<asp:parameter Name="endDate" Type="DateTime" />
<asp:parameter Name="content" Type="String" />
</UpdateParameters>
[...]
</asp:ObjectDataSource>


I'd appreciate any help/pointers because I'm stuck at this point.

Thanks,
Bob.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top