472,986 Members | 3,017 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

ObjectDataSource in ASP.NET 2.0

I'm having some trouble using the ObjectDataSource in ASP.NET 2.0.

I have a wrapper that contains the static methods for Select and Update. The
Update-method takes the business object as parameter.

When the Update-method is invoked by the ObjectDataSource, the object
referenced is not the same object returned by the Select-method, but a new
object with only the values from the Edit-template. So basically I get a
reference to a useless object here because it cannot be used by the DAL.

Is this a bug or a feature?

Here is some code to illustrate:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource1">

<Columns>

<asp:CommandField ShowEditButton="True" />

<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />

</Columns>

</asp:GridView>

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DataObjectTypeName="Customer"

SelectMethod="GetCustomers" TypeName="MyWrapperObj"
UpdateMethod="Save"></asp:ObjectDataSource>

And the C#-code:

public class Customer

{

private int id;

public int Id

{

get { return id; }

set { id = value; }

}

private string name;

public string Name

{

get { return name; }

set { name = value; }

}

public void Save()

{

// Do some things here...

}

}

public class MyWrapperObj

{

public static IList<Customer> GetCustomers()

{

List<Customer> customers = new List<Customer>();

for (int i = 0; i < 10; i++)

{

Customer customer = new Customer();

customer.Id = i;

customer.Name = string.Format("Name {0}", i);
customers.Add(customer);

}

return customers;

}

public static void Save(Customer obj)

{

obj.Save(); // This object is NOT in the IList returned by GetCustomers()

}

}
Nov 19 '05 #1
5 1935
Hi Ole M,

Welcome to ASPNET newsgroup.

Regarding on the ObjectDataSource's update method's question, I think it's
the normal behavior which is expected.
For datasourcecontrol and databindig control, after the object data source
pass the data bojects to data controls, it no longer hold the actual
refernce of the oringial data(nor does the data binding control), also
since the actual backend datasource may vary (maybe database or xml file or
....), it don't quite makesense to maintain such a in-memory reference. In
fact, this also somewhat related to the ASP.NET application's runtime
model, it's request/response based, generally after page output response to
client, all the serverside objects will be disposed and in-memory reference
will no longer make sense in the next page lifecycle. This is diferent
from winform application. So in such scenario, we should update the
original object through its primary key value or retrieve the reference
again from orginal datasource, e.g:
======================

private static List<Employee> _list;
.......

public static void UpdateEmployee(Employee emp)
{
EmployeePredicate ep = new EmployeePredicate(emp.ID);

Employee employee = _list.Find(ep.Assert);

if (employee == null)
{
throw new Exception("Invalid Employee info in
UpdateEmployee()...");
}

employee.Name = emp.Name;
employee.Email = emp.Email;
}

public class EmployeePredicate
{
private long _id;

public EmployeePredicate(long id)
{
_id = id;
}

public bool Assert(Employee emp)
{
if (emp.ID == _id)
{
return true;
}
else
{
return false;
}
}

}

============================

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
| From: "Ole M" <ol**@community.nospam>
| Subject: ObjectDataSource in ASP.NET 2.0
| Date: Mon, 31 Oct 2005 22:03:59 +0100
| Lines: 113
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <#a**************@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: fw.home.norlinux.com 213.187.183.210
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP10.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:135092
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| I'm having some trouble using the ObjectDataSource in ASP.NET 2.0.
|
| I have a wrapper that contains the static methods for Select and Update.
The
| Update-method takes the business object as parameter.
|
| When the Update-method is invoked by the ObjectDataSource, the object
| referenced is not the same object returned by the Select-method, but a
new
| object with only the values from the Edit-template. So basically I get a
| reference to a useless object here because it cannot be used by the DAL.
|
| Is this a bug or a feature?
|
| Here is some code to illustrate:
|
| <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
| DataSourceID="ObjectDataSource1">
|
| <Columns>
|
| <asp:CommandField ShowEditButton="True" />
|
| <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"
/>
|
| </Columns>
|
| </asp:GridView>
|
| <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
| DataObjectTypeName="Customer"
|
| SelectMethod="GetCustomers" TypeName="MyWrapperObj"
| UpdateMethod="Save"></asp:ObjectDataSource>
|
| And the C#-code:
|
| public class Customer
|
| {
|
| private int id;
|
| public int Id
|
| {
|
| get { return id; }
|
| set { id = value; }
|
| }
|
| private string name;
|
| public string Name
|
| {
|
| get { return name; }
|
| set { name = value; }
|
| }
|
| public void Save()
|
| {
|
| // Do some things here...
|
| }
|
| }
|
| public class MyWrapperObj
|
| {
|
| public static IList<Customer> GetCustomers()
|
| {
|
| List<Customer> customers = new List<Customer>();
|
| for (int i = 0; i < 10; i++)
|
| {
|
| Customer customer = new Customer();
|
| customer.Id = i;
|
| customer.Name = string.Format("Name {0}", i);
|
|
| customers.Add(customer);
|
| }
|
| return customers;
|
| }
|
| public static void Save(Customer obj)
|
| {
|
| obj.Save(); // This object is NOT in the IList returned by GetCustomers()
|
| }
|
| }
|
|
|

Nov 19 '05 #2
Hi,

I see your point, but there is still a problem with the solution you have
there. The object passed to the Update(or in your case, UpdateEmployee)
method only contains values of the fields in the form. So, if the field "Id"
is not editable by the user, the value of "Id" is always 0.

"Steven Cheng[MSFT]" <st*****@online.microsoft.com> skrev i melding
news:Oc**************@TK2MSFTNGXA01.phx.gbl...
Hi Ole M,

Welcome to ASPNET newsgroup.

Regarding on the ObjectDataSource's update method's question, I think it's
the normal behavior which is expected.
For datasourcecontrol and databindig control, after the object data source
pass the data bojects to data controls, it no longer hold the actual
refernce of the oringial data(nor does the data binding control), also
since the actual backend datasource may vary (maybe database or xml file
or
...), it don't quite makesense to maintain such a in-memory reference. In
fact, this also somewhat related to the ASP.NET application's runtime
model, it's request/response based, generally after page output response
to
client, all the serverside objects will be disposed and in-memory
reference
will no longer make sense in the next page lifecycle. This is diferent
from winform application. So in such scenario, we should update the
original object through its primary key value or retrieve the reference
again from orginal datasource, e.g:
======================

private static List<Employee> _list;
......

public static void UpdateEmployee(Employee emp)
{
EmployeePredicate ep = new EmployeePredicate(emp.ID);

Employee employee = _list.Find(ep.Assert);

if (employee == null)
{
throw new Exception("Invalid Employee info in
UpdateEmployee()...");
}

employee.Name = emp.Name;
employee.Email = emp.Email;
}

public class EmployeePredicate
{
private long _id;

public EmployeePredicate(long id)
{
_id = id;
}

public bool Assert(Employee emp)
{
if (emp.ID == _id)
{
return true;
}
else
{
return false;
}
}

}

============================

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
| From: "Ole M" <ol**@community.nospam>
| Subject: ObjectDataSource in ASP.NET 2.0
| Date: Mon, 31 Oct 2005 22:03:59 +0100
| Lines: 113
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <#a**************@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: fw.home.norlinux.com 213.187.183.210
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP10.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:135092
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| I'm having some trouble using the ObjectDataSource in ASP.NET 2.0.
|
| I have a wrapper that contains the static methods for Select and Update.
The
| Update-method takes the business object as parameter.
|
| When the Update-method is invoked by the ObjectDataSource, the object
| referenced is not the same object returned by the Select-method, but a
new
| object with only the values from the Edit-template. So basically I get a
| reference to a useless object here because it cannot be used by the DAL.
|
| Is this a bug or a feature?
|
| Here is some code to illustrate:
|
| <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
| DataSourceID="ObjectDataSource1">
|
| <Columns>
|
| <asp:CommandField ShowEditButton="True" />
|
| <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"
/>
|
| </Columns>
|
| </asp:GridView>
|
| <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
| DataObjectTypeName="Customer"
|
| SelectMethod="GetCustomers" TypeName="MyWrapperObj"
| UpdateMethod="Save"></asp:ObjectDataSource>
|
| And the C#-code:
|
| public class Customer
|
| {
|
| private int id;
|
| public int Id
|
| {
|
| get { return id; }
|
| set { id = value; }
|
| }
|
| private string name;
|
| public string Name
|
| {
|
| get { return name; }
|
| set { name = value; }
|
| }
|
| public void Save()
|
| {
|
| // Do some things here...
|
| }
|
| }
|
| public class MyWrapperObj
|
| {
|
| public static IList<Customer> GetCustomers()
|
| {
|
| List<Customer> customers = new List<Customer>();
|
| for (int i = 0; i < 10; i++)
|
| {
|
| Customer customer = new Customer();
|
| customer.Id = i;
|
| customer.Name = string.Format("Name {0}", i);
|
|
| customers.Add(customer);
|
| }
|
| return customers;
|
| }
|
| public static void Save(Customer obj)
|
| {
|
| obj.Save(); // This object is NOT in the IList returned by
GetCustomers()
|
| }
|
| }
|
|
|

Nov 19 '05 #3
Hi Ole M,

Thanks for your response. However, as for the further problem you
mentioned, I think we can still let the ID property being passed from
GridView's field to the DataSource for updating. The GridView can let us
specify "DataKeyNames" so as to indicate which field is the key column.
Also, for the fields, we can set the field's "readonly" and "Visible"
property so as to make the primary key field readonly or invisible. So
event we define the following gridView template schema:

==========
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1"
AutoGenerateColumns="False" DataKeyNames="ID"
OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID"
ReadOnly="true" Visible="false" />
<asp:BoundField DataField="Name"
..................

==========================

The "ID" field is set to invisible, the value will still be passed to the
update event of the GridView or DataSource control. We can check it in the
GridView's RowUpdating event like below:

======================
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs
e)
{
foreach (string key in e.Keys.Keys)
{
Response.Write("<br>key: " + e.Keys[key]);
}

foreach (string key in e.OldValues.Keys)
{
Response.Write("<br>Oldvalue: " + e.OldValues[key]);
}

foreach (string key in e.NewValues.Keys)
{
Response.Write("<br>Newvalue: " + e.NewValues[key]);
}

}
=======================

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "Ole M" <ol**@community.nospam>
| References: <#a**************@TK2MSFTNGP10.phx.gbl>
<Oc**************@TK2MSFTNGXA01.phx.gbl>
| Subject: Re: ObjectDataSource in ASP.NET 2.0
| Date: Tue, 1 Nov 2005 14:53:16 +0100
| Lines: 229
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <uy**************@TK2MSFTNGP12.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: fw.home.norlinux.com 213.187.183.210
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP12.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:135220
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hi,
|
| I see your point, but there is still a problem with the solution you have
| there. The object passed to the Update(or in your case, UpdateEmployee)
| method only contains values of the fields in the form. So, if the field
"Id"
| is not editable by the user, the value of "Id" is always 0.
|
| "Steven Cheng[MSFT]" <st*****@online.microsoft.com> skrev i melding
| news:Oc**************@TK2MSFTNGXA01.phx.gbl...
| > Hi Ole M,
| >
| > Welcome to ASPNET newsgroup.
| >
| > Regarding on the ObjectDataSource's update method's question, I think
it's
| > the normal behavior which is expected.
| > For datasourcecontrol and databindig control, after the object data
source
| > pass the data bojects to data controls, it no longer hold the actual
| > refernce of the oringial data(nor does the data binding control), also
| > since the actual backend datasource may vary (maybe database or xml
file
| > or
| > ...), it don't quite makesense to maintain such a in-memory reference.
In
| > fact, this also somewhat related to the ASP.NET application's runtime
| > model, it's request/response based, generally after page output
response
| > to
| > client, all the serverside objects will be disposed and in-memory
| > reference
| > will no longer make sense in the next page lifecycle. This is diferent
| > from winform application. So in such scenario, we should update the
| > original object through its primary key value or retrieve the reference
| > again from orginal datasource, e.g:
| >
| >
| > ======================
| >
| > private static List<Employee> _list;
| > ......
| >
| > public static void UpdateEmployee(Employee emp)
| > {
| > EmployeePredicate ep = new EmployeePredicate(emp.ID);
| >
| > Employee employee = _list.Find(ep.Assert);
| >
| > if (employee == null)
| > {
| > throw new Exception("Invalid Employee info in
| > UpdateEmployee()...");
| > }
| >
| > employee.Name = emp.Name;
| > employee.Email = emp.Email;
| > }
| >
| > public class EmployeePredicate
| > {
| > private long _id;
| >
| > public EmployeePredicate(long id)
| > {
| > _id = id;
| > }
| >
| > public bool Assert(Employee emp)
| > {
| > if (emp.ID == _id)
| > {
| > return true;
| > }
| > else
| > {
| > return false;
| > }
| > }
| >
| > }
| >
| > ============================
| >
| > Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| >
| > --------------------
| > | From: "Ole M" <ol**@community.nospam>
| > | Subject: ObjectDataSource in ASP.NET 2.0
| > | Date: Mon, 31 Oct 2005 22:03:59 +0100
| > | Lines: 113
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| > | X-RFC2646: Format=Flowed; Original
| > | Message-ID: <#a**************@TK2MSFTNGP10.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: fw.home.norlinux.com 213.187.183.210
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP10.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet:135092
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > |
| > | I'm having some trouble using the ObjectDataSource in ASP.NET 2.0.
| > |
| > | I have a wrapper that contains the static methods for Select and
Update.
| > The
| > | Update-method takes the business object as parameter.
| > |
| > | When the Update-method is invoked by the ObjectDataSource, the object
| > | referenced is not the same object returned by the Select-method, but a
| > new
| > | object with only the values from the Edit-template. So basically I
get a
| > | reference to a useless object here because it cannot be used by the
DAL.
| > |
| > | Is this a bug or a feature?
| > |
| > | Here is some code to illustrate:
| > |
| > | <asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
| > | DataSourceID="ObjectDataSource1">
| > |
| > | <Columns>
| > |
| > | <asp:CommandField ShowEditButton="True" />
| > |
| > | <asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name"
| > />
| > |
| > | </Columns>
| > |
| > | </asp:GridView>
| > |
| > | <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
| > | DataObjectTypeName="Customer"
| > |
| > | SelectMethod="GetCustomers" TypeName="MyWrapperObj"
| > | UpdateMethod="Save"></asp:ObjectDataSource>
| > |
| > | And the C#-code:
| > |
| > | public class Customer
| > |
| > | {
| > |
| > | private int id;
| > |
| > | public int Id
| > |
| > | {
| > |
| > | get { return id; }
| > |
| > | set { id = value; }
| > |
| > | }
| > |
| > | private string name;
| > |
| > | public string Name
| > |
| > | {
| > |
| > | get { return name; }
| > |
| > | set { name = value; }
| > |
| > | }
| > |
| > | public void Save()
| > |
| > | {
| > |
| > | // Do some things here...
| > |
| > | }
| > |
| > | }
| > |
| > | public class MyWrapperObj
| > |
| > | {
| > |
| > | public static IList<Customer> GetCustomers()
| > |
| > | {
| > |
| > | List<Customer> customers = new List<Customer>();
| > |
| > | for (int i = 0; i < 10; i++)
| > |
| > | {
| > |
| > | Customer customer = new Customer();
| > |
| > | customer.Id = i;
| > |
| > | customer.Name = string.Format("Name {0}", i);
| > |
| > |
| > | customers.Add(customer);
| > |
| > | }
| > |
| > | return customers;
| > |
| > | }
| > |
| > | public static void Save(Customer obj)
| > |
| > | {
| > |
| > | obj.Save(); // This object is NOT in the IList returned by
| > GetCustomers()
| > |
| > | }
| > |
| > | }
| > |
| > |
| > |
| >
|
|
|

Nov 19 '05 #4
Hi,

Ah, great. I wasnt aware of the DataKeyNames property.

Thanks alot for the help!

"Steven Cheng[MSFT]" <st*****@online.microsoft.com> skrev i melding
news:TW**************@TK2MSFTNGXA01.phx.gbl...
Hi Ole M,

Thanks for your response. However, as for the further problem you
mentioned, I think we can still let the ID property being passed from
GridView's field to the DataSource for updating. The GridView can let us
specify "DataKeyNames" so as to indicate which field is the key column.
Also, for the fields, we can set the field's "readonly" and "Visible"
property so as to make the primary key field readonly or invisible. So
event we define the following gridView template schema:

==========
<asp:GridView ID="GridView1" runat="server"
DataSourceID="ObjectDataSource1"
AutoGenerateColumns="False" DataKeyNames="ID"
OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID"
ReadOnly="true" Visible="false" />
<asp:BoundField DataField="Name"
.................

==========================

The "ID" field is set to invisible, the value will still be passed to the
update event of the GridView or DataSource control. We can check it in the
GridView's RowUpdating event like below:

======================
protected void GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs
e)
{
foreach (string key in e.Keys.Keys)
{
Response.Write("<br>key: " + e.Keys[key]);
}

foreach (string key in e.OldValues.Keys)
{
Response.Write("<br>Oldvalue: " + e.OldValues[key]);
}

foreach (string key in e.NewValues.Keys)
{
Response.Write("<br>Newvalue: " + e.NewValues[key]);
}

}
=======================

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "Ole M" <ol**@community.nospam>
| References: <#a**************@TK2MSFTNGP10.phx.gbl>
<Oc**************@TK2MSFTNGXA01.phx.gbl>
| Subject: Re: ObjectDataSource in ASP.NET 2.0
| Date: Tue, 1 Nov 2005 14:53:16 +0100
| Lines: 229
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <uy**************@TK2MSFTNGP12.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: fw.home.norlinux.com 213.187.183.210
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP12.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:135220
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hi,
|
| I see your point, but there is still a problem with the solution you
have
| there. The object passed to the Update(or in your case, UpdateEmployee)
| method only contains values of the fields in the form. So, if the field
"Id"
| is not editable by the user, the value of "Id" is always 0.
|
| "Steven Cheng[MSFT]" <st*****@online.microsoft.com> skrev i melding
| news:Oc**************@TK2MSFTNGXA01.phx.gbl...
| > Hi Ole M,
| >
| > Welcome to ASPNET newsgroup.
| >
| > Regarding on the ObjectDataSource's update method's question, I think
it's
| > the normal behavior which is expected.
| > For datasourcecontrol and databindig control, after the object data
source
| > pass the data bojects to data controls, it no longer hold the actual
| > refernce of the oringial data(nor does the data binding control), also
| > since the actual backend datasource may vary (maybe database or xml
file
| > or
| > ...), it don't quite makesense to maintain such a in-memory reference.
In
| > fact, this also somewhat related to the ASP.NET application's runtime
| > model, it's request/response based, generally after page output
response
| > to
| > client, all the serverside objects will be disposed and in-memory
| > reference
| > will no longer make sense in the next page lifecycle. This is
diferent
| > from winform application. So in such scenario, we should update the
| > original object through its primary key value or retrieve the
reference
| > again from orginal datasource, e.g:
| >
| >
| > ======================
| >
| > private static List<Employee> _list;
| > ......
| >
| > public static void UpdateEmployee(Employee emp)
| > {
| > EmployeePredicate ep = new EmployeePredicate(emp.ID);
| >
| > Employee employee = _list.Find(ep.Assert);
| >
| > if (employee == null)
| > {
| > throw new Exception("Invalid Employee info in
| > UpdateEmployee()...");
| > }
| >
| > employee.Name = emp.Name;
| > employee.Email = emp.Email;
| > }
| >
| > public class EmployeePredicate
| > {
| > private long _id;
| >
| > public EmployeePredicate(long id)
| > {
| > _id = id;
| > }
| >
| > public bool Assert(Employee emp)
| > {
| > if (emp.ID == _id)
| > {
| > return true;
| > }
| > else
| > {
| > return false;
| > }
| > }
| >
| > }
| >
| > ============================
| >
| > Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| >
| > --------------------
| > | From: "Ole M" <ol**@community.nospam>
| > | Subject: ObjectDataSource in ASP.NET 2.0
| > | Date: Mon, 31 Oct 2005 22:03:59 +0100
| > | Lines: 113
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| > | X-RFC2646: Format=Flowed; Original
| > | Message-ID: <#a**************@TK2MSFTNGP10.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: fw.home.norlinux.com 213.187.183.210
| > | Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP10.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet:135092
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > |
| > | I'm having some trouble using the ObjectDataSource in ASP.NET 2.0.
| > |
| > | I have a wrapper that contains the static methods for Select and
Update.
| > The
| > | Update-method takes the business object as parameter.
| > |
| > | When the Update-method is invoked by the ObjectDataSource, the
object
| > | referenced is not the same object returned by the Select-method, but
a
| > new
| > | object with only the values from the Edit-template. So basically I
get a
| > | reference to a useless object here because it cannot be used by the
DAL.
| > |
| > | Is this a bug or a feature?
| > |
| > | Here is some code to illustrate:
| > |
| > | <asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
| > | DataSourceID="ObjectDataSource1">
| > |
| > | <Columns>
| > |
| > | <asp:CommandField ShowEditButton="True" />
| > |
| > | <asp:BoundField DataField="Name" HeaderText="Name"
SortExpression="Name"
| > />
| > |
| > | </Columns>
| > |
| > | </asp:GridView>
| > |
| > | <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
| > | DataObjectTypeName="Customer"
| > |
| > | SelectMethod="GetCustomers" TypeName="MyWrapperObj"
| > | UpdateMethod="Save"></asp:ObjectDataSource>
| > |
| > | And the C#-code:
| > |
| > | public class Customer
| > |
| > | {
| > |
| > | private int id;
| > |
| > | public int Id
| > |
| > | {
| > |
| > | get { return id; }
| > |
| > | set { id = value; }
| > |
| > | }
| > |
| > | private string name;
| > |
| > | public string Name
| > |
| > | {
| > |
| > | get { return name; }
| > |
| > | set { name = value; }
| > |
| > | }
| > |
| > | public void Save()
| > |
| > | {
| > |
| > | // Do some things here...
| > |
| > | }
| > |
| > | }
| > |
| > | public class MyWrapperObj
| > |
| > | {
| > |
| > | public static IList<Customer> GetCustomers()
| > |
| > | {
| > |
| > | List<Customer> customers = new List<Customer>();
| > |
| > | for (int i = 0; i < 10; i++)
| > |
| > | {
| > |
| > | Customer customer = new Customer();
| > |
| > | customer.Id = i;
| > |
| > | customer.Name = string.Format("Name {0}", i);
| > |
| > |
| > | customers.Add(customer);
| > |
| > | }
| > |
| > | return customers;
| > |
| > | }
| > |
| > | public static void Save(Customer obj)
| > |
| > | {
| > |
| > | obj.Save(); // This object is NOT in the IList returned by
| > GetCustomers()
| > |
| > | }
| > |
| > | }
| > |
| > |
| > |
| >
|
|
|

Nov 19 '05 #5
You're welcome Ole M,

Have a good day!

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "Ole M" <ol**@community.nospam>
| References: <#a**************@TK2MSFTNGP10.phx.gbl>
<Oc**************@TK2MSFTNGXA01.phx.gbl>
<uy**************@TK2MSFTNGP12.phx.gbl>
<TW**************@TK2MSFTNGXA01.phx.gbl>
| Subject: Re: ObjectDataSource in ASP.NET 2.0
| Date: Wed, 2 Nov 2005 11:51:05 +0100
| Lines: 339
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <#N**************@tk2msftngp13.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: fw.home.norlinux.com 213.187.183.210
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msft ngp13.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:135459
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hi,
|
| Ah, great. I wasnt aware of the DataKeyNames property.
|
| Thanks alot for the help!
|
| "Steven Cheng[MSFT]" <st*****@online.microsoft.com> skrev i melding
| news:TW**************@TK2MSFTNGXA01.phx.gbl...
| > Hi Ole M,
| >
| > Thanks for your response. However, as for the further problem you
| > mentioned, I think we can still let the ID property being passed from
| > GridView's field to the DataSource for updating. The GridView can let us
| > specify "DataKeyNames" so as to indicate which field is the key column.
| > Also, for the fields, we can set the field's "readonly" and "Visible"
| > property so as to make the primary key field readonly or invisible. So
| > event we define the following gridView template schema:
| >
| > ==========
| > <asp:GridView ID="GridView1" runat="server"
| > DataSourceID="ObjectDataSource1"
| > AutoGenerateColumns="False" DataKeyNames="ID"
| > OnRowUpdating="GridView1_RowUpdating">
| > <Columns>
| > <asp:CommandField ShowEditButton="True" />
| > <asp:BoundField DataField="ID" HeaderText="ID"
| > ReadOnly="true" Visible="false" />
| > <asp:BoundField DataField="Name"
| > .................
| >
| > ==========================
| >
| > The "ID" field is set to invisible, the value will still be passed to
the
| > update event of the GridView or DataSource control. We can check it in
the
| > GridView's RowUpdating event like below:
| >
| > ======================
| > protected void GridView1_RowUpdating(object sender,
| > GridViewUpdateEventArgs
| > e)
| > {
| > foreach (string key in e.Keys.Keys)
| > {
| > Response.Write("<br>key: " + e.Keys[key]);
| > }
| >
| > foreach (string key in e.OldValues.Keys)
| > {
| > Response.Write("<br>Oldvalue: " + e.OldValues[key]);
| > }
| >
| > foreach (string key in e.NewValues.Keys)
| > {
| > Response.Write("<br>Newvalue: " + e.NewValues[key]);
| > }
| >
| > }
| > =======================
| >
| > Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| > --------------------
| > | From: "Ole M" <ol**@community.nospam>
| > | References: <#a**************@TK2MSFTNGP10.phx.gbl>
| > <Oc**************@TK2MSFTNGXA01.phx.gbl>
| > | Subject: Re: ObjectDataSource in ASP.NET 2.0
| > | Date: Tue, 1 Nov 2005 14:53:16 +0100
| > | Lines: 229
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| > | X-RFC2646: Format=Flowed; Original
| > | Message-ID: <uy**************@TK2MSFTNGP12.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: fw.home.norlinux.com 213.187.183.210
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP12.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet:135220
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > |
| > | Hi,
| > |
| > | I see your point, but there is still a problem with the solution you
| > have
| > | there. The object passed to the Update(or in your case,
UpdateEmployee)
| > | method only contains values of the fields in the form. So, if the
field
| > "Id"
| > | is not editable by the user, the value of "Id" is always 0.
| > |
| > | "Steven Cheng[MSFT]" <st*****@online.microsoft.com> skrev i melding
| > | news:Oc**************@TK2MSFTNGXA01.phx.gbl...
| > | > Hi Ole M,
| > | >
| > | > Welcome to ASPNET newsgroup.
| > | >
| > | > Regarding on the ObjectDataSource's update method's question, I
think
| > it's
| > | > the normal behavior which is expected.
| > | > For datasourcecontrol and databindig control, after the object data
| > source
| > | > pass the data bojects to data controls, it no longer hold the actual
| > | > refernce of the oringial data(nor does the data binding control),
also
| > | > since the actual backend datasource may vary (maybe database or xml
| > file
| > | > or
| > | > ...), it don't quite makesense to maintain such a in-memory
reference.
| > In
| > | > fact, this also somewhat related to the ASP.NET application's
runtime
| > | > model, it's request/response based, generally after page output
| > response
| > | > to
| > | > client, all the serverside objects will be disposed and in-memory
| > | > reference
| > | > will no longer make sense in the next page lifecycle. This is
| > diferent
| > | > from winform application. So in such scenario, we should update the
| > | > original object through its primary key value or retrieve the
| > reference
| > | > again from orginal datasource, e.g:
| > | >
| > | >
| > | > ======================
| > | >
| > | > private static List<Employee> _list;
| > | > ......
| > | >
| > | > public static void UpdateEmployee(Employee emp)
| > | > {
| > | > EmployeePredicate ep = new EmployeePredicate(emp.ID);
| > | >
| > | > Employee employee = _list.Find(ep.Assert);
| > | >
| > | > if (employee == null)
| > | > {
| > | > throw new Exception("Invalid Employee info in
| > | > UpdateEmployee()...");
| > | > }
| > | >
| > | > employee.Name = emp.Name;
| > | > employee.Email = emp.Email;
| > | > }
| > | >
| > | > public class EmployeePredicate
| > | > {
| > | > private long _id;
| > | >
| > | > public EmployeePredicate(long id)
| > | > {
| > | > _id = id;
| > | > }
| > | >
| > | > public bool Assert(Employee emp)
| > | > {
| > | > if (emp.ID == _id)
| > | > {
| > | > return true;
| > | > }
| > | > else
| > | > {
| > | > return false;
| > | > }
| > | > }
| > | >
| > | > }
| > | >
| > | > ============================
| > | >
| > | > Thanks,
| > | >
| > | > Steven Cheng
| > | > Microsoft Online Support
| > | >
| > | > Get Secure! www.microsoft.com/security
| > | > (This posting is provided "AS IS", with no warranties, and confers
no
| > | > rights.)
| > | >
| > | >
| > | >
| > | >
| > | > --------------------
| > | > | From: "Ole M" <ol**@community.nospam>
| > | > | Subject: ObjectDataSource in ASP.NET 2.0
| > | > | Date: Mon, 31 Oct 2005 22:03:59 +0100
| > | > | Lines: 113
| > | > | X-Priority: 3
| > | > | X-MSMail-Priority: Normal
| > | > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| > | > | X-RFC2646: Format=Flowed; Original
| > | > | Message-ID: <#a**************@TK2MSFTNGP10.phx.gbl>
| > | > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | > | NNTP-Posting-Host: fw.home.norlinux.com 213.187.183.210
| > | > | Path:
| > TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP10.phx.gbl
| > | > | Xref: TK2MSFTNGXA01.phx.gbl
| > | > microsoft.public.dotnet.framework.aspnet:135092
| > | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > | > |
| > | > | I'm having some trouble using the ObjectDataSource in ASP.NET 2.0.
| > | > |
| > | > | I have a wrapper that contains the static methods for Select and
| > Update.
| > | > The
| > | > | Update-method takes the business object as parameter.
| > | > |
| > | > | When the Update-method is invoked by the ObjectDataSource, the
| > object
| > | > | referenced is not the same object returned by the Select-method,
but
| > a
| > | > new
| > | > | object with only the values from the Edit-template. So basically I
| > get a
| > | > | reference to a useless object here because it cannot be used by
the
| > DAL.
| > | > |
| > | > | Is this a bug or a feature?
| > | > |
| > | > | Here is some code to illustrate:
| > | > |
| > | > | <asp:GridView ID="GridView1" runat="server"
| > AutoGenerateColumns="False"
| > | > | DataSourceID="ObjectDataSource1">
| > | > |
| > | > | <Columns>
| > | > |
| > | > | <asp:CommandField ShowEditButton="True" />
| > | > |
| > | > | <asp:BoundField DataField="Name" HeaderText="Name"
| > SortExpression="Name"
| > | > />
| > | > |
| > | > | </Columns>
| > | > |
| > | > | </asp:GridView>
| > | > |
| > | > | <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
| > | > | DataObjectTypeName="Customer"
| > | > |
| > | > | SelectMethod="GetCustomers" TypeName="MyWrapperObj"
| > | > | UpdateMethod="Save"></asp:ObjectDataSource>
| > | > |
| > | > | And the C#-code:
| > | > |
| > | > | public class Customer
| > | > |
| > | > | {
| > | > |
| > | > | private int id;
| > | > |
| > | > | public int Id
| > | > |
| > | > | {
| > | > |
| > | > | get { return id; }
| > | > |
| > | > | set { id = value; }
| > | > |
| > | > | }
| > | > |
| > | > | private string name;
| > | > |
| > | > | public string Name
| > | > |
| > | > | {
| > | > |
| > | > | get { return name; }
| > | > |
| > | > | set { name = value; }
| > | > |
| > | > | }
| > | > |
| > | > | public void Save()
| > | > |
| > | > | {
| > | > |
| > | > | // Do some things here...
| > | > |
| > | > | }
| > | > |
| > | > | }
| > | > |
| > | > | public class MyWrapperObj
| > | > |
| > | > | {
| > | > |
| > | > | public static IList<Customer> GetCustomers()
| > | > |
| > | > | {
| > | > |
| > | > | List<Customer> customers = new List<Customer>();
| > | > |
| > | > | for (int i = 0; i < 10; i++)
| > | > |
| > | > | {
| > | > |
| > | > | Customer customer = new Customer();
| > | > |
| > | > | customer.Id = i;
| > | > |
| > | > | customer.Name = string.Format("Name {0}", i);
| > | > |
| > | > |
| > | > | customers.Add(customer);
| > | > |
| > | > | }
| > | > |
| > | > | return customers;
| > | > |
| > | > | }
| > | > |
| > | > | public static void Save(Customer obj)
| > | > |
| > | > | {
| > | > |
| > | > | obj.Save(); // This object is NOT in the IList returned by
| > | > GetCustomers()
| > | > |
| > | > | }
| > | > |
| > | > | }
| > | > |
| > | > |
| > | > |
| > | >
| > |
| > |
| > |
| >
|
|
|

Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Jim Hammond | last post by:
I am passing the whole object instead or parameters in my select and update methods. I can get the updated object if I set UpdateMethod, let ASP.NET autogenerate an update button, and then press...
3
by: avezina | last post by:
We would like to use those new cool features of Asp.Net 2.0 like the ObjectDataSource in our project. I tried few basics examples and its work well. Let's say I have a page that displays a...
0
by: Eiriken | last post by:
Hello everyone, I am using ASP.NET 2 and trying to bind a objectdatasource to a gridview. By doing this the most common way by adding an objectdatasource to the page and by using the wizard to...
2
by: J055 | last post by:
Hi I've implemented caching for my ObjectDataSource. <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" EnableCaching="True" CacheDuration="10" CacheExpirationPolicy="Sliding" ...
0
by: Northern | last post by:
Hello, I have some trouble to declare and instantiate dynamically an ObjectDataSource in the codebehind file. The idea is to bind the objectdatasource to a gridview and have automate sorting and...
10
by: J055 | last post by:
Hi I've been trying out SqlCacheDependency using the ObjectDataSource and SQL Server 2005. It all works quite well with the minimum of configuration, e.g. <asp:ObjectDataSource...
14
by: Rolf Welskes | last post by:
Hello, I have an ObjectDataSource which has as business-object a simple array of strings. No problem. I have an own (custom) control to which I give the DataSourceId and in the custom-control...
5
by: Randy Smith | last post by:
Hi ALL, I wonder if anyone has been using n-tier to bind to a GridView control by using the ObjectDataSource. This is our first OOP web application, and we have no tables. Right now we are...
3
by: btreddy | last post by:
Hii all, I've a problem regarding passing select parameters to objectdatasource from code behind. i would like to pass the select parameters to a objectdatasource ..not in the selecting or...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.