Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Help with GridView Update w/ Object Data Source needed

Reply
Thread Tools

Help with GridView Update w/ Object Data Source needed

 
 
anyeone
Guest
Posts: n/a
 
      01-17-2006
I've got a GridView bound to an Object Data Source and I'm having trouble
figuring out just how to set it up to update properly.

I have the object data source defined as so:

<asp:ObjectDataSource ID="objBatchData" runat="server"
SelectMethod="GetItems" TypeName="MyNamespace.BatchData"
DataObjectTypeName="MyNamespace.BatchItem" UpdateMethod="UpdateItem" >
<SelectParameters>
<asp:QueryStringParameter Name="BatchID" QueryStringField="BatchID"
Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asparameter Name="ItemID" Type="Int32" />
<asparameter Name="BatchID" Type="Int32" />
<asparameter Name="DataField1" Type="string" />
<asparameter Name="DataField2" Type="string" />
</UpdateParameters>
</asp:ObjectDataSource>

..

BatchItem is a business object that defines several constructors. The
BatchItem(DataRow row) constructor is the one that GetItems() uses to
instantiate individual BatchItems when the object data source binding occurs.
There is also a BatchItem(int ItemID) constructor and a default constructor
that doesn't actually do anything.

ItemID and BatchID are read only and brought in through BatchItem on the
original select statement. DataField1 and 2 are the two fields actually
being updated by the user through dropdownlists bound to BatchItem.DataList1
and BatchItem.DataList2 respectively

The static BatchData.GetItems method seems to be working properly as the
grid is being populated with the correct data.

The static BatchData.UpdateItem method takes in a BatchItem object (since I
got an error when I defined it to explicitly take in ItemID, BatchID,
DataField1, DataField2 saying it needed to take in a BatchItem as a
parameter). It then creates a collection of SqlParameters and calls a stored
proc to perform the update.

The problem is, when the row is updated, the application is instantiating a
default constructor version of BatchItem which is empty and passing that to
BatchData.UpdateItem().

So what I need to figure out is how to get the data from the GridView row
being updated (the four properties listed in "UpdateParameters" on the object
data source) into the BatchItem that is passed to BatchData.UpdateItem().

I've been using some of Phillip Williams' examples to model from (if you're
listening, Phillip), particularly the "DropdownList within a GridView"
example and where I see a disconnect is that the UpdateParameters don't seem
to be tied to anything in the grid data particularly. Unfortunately without
being able to view the source of his UpdateOrder method (the UpdateMethod on
his object data source) I can't compare what I'm doing to what he is doing to
see the problem.

Am I supposed to be overriding one of the GridView events to stuff a
ConfirmItem object with the correct field data? The UpdateItem method only
takes ConfirmItem so I don't seem to have access to a parameters collection.

Help is greatly appreciated, thanks! If you need more information please
let me know.
 
Reply With Quote
 
 
 
 
Phillip Williams
Guest
Posts: n/a
 
      01-17-2006

The update parameters pass scalar values to an object of type clsOrder.
Notice that the clsOrder definition,
http://www.webswapp.com/CodeSamples/...20\clsOrder.cs,
has a default constructor (without any parameters defined). It has public
properties matching the names of the parameters passed through the
objectDataSource.

The empty constructor allows the ObjectDatasource to instantiate the class
then the ObjectDataSource parameters set the public properties of the class
before passing it to the method specified by the UpdateMethod attribute.

So to answer your question

>… how to get the data from the GridView row being updated
>(the four properties listed in "UpdateParameters" on the object
>data source) into the BatchItem that is passed to
>BatchData.UpdateItem()”


Make sure that your definition of the BatchItem class contains a default
constructor that receives no parameters, e.g.

Public class BatchItem
{
public BatchItem(){ //default constructor }

// the rest of the class implementation
}

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


"anyeone" wrote:

> I've got a GridView bound to an Object Data Source and I'm having trouble
> figuring out just how to set it up to update properly.
>
> I have the object data source defined as so:
>
> <asp:ObjectDataSource ID="objBatchData" runat="server"
> SelectMethod="GetItems" TypeName="MyNamespace.BatchData"
> DataObjectTypeName="MyNamespace.BatchItem" UpdateMethod="UpdateItem" >
> <SelectParameters>
> <asp:QueryStringParameter Name="BatchID" QueryStringField="BatchID"
> Type="Int32" />
> </SelectParameters>
> <UpdateParameters>
> <asparameter Name="ItemID" Type="Int32" />
> <asparameter Name="BatchID" Type="Int32" />
> <asparameter Name="DataField1" Type="string" />
> <asparameter Name="DataField2" Type="string" />
> </UpdateParameters>
> </asp:ObjectDataSource>
>
> .
>
> BatchItem is a business object that defines several constructors. The
> BatchItem(DataRow row) constructor is the one that GetItems() uses to
> instantiate individual BatchItems when the object data source binding occurs.
> There is also a BatchItem(int ItemID) constructor and a default constructor
> that doesn't actually do anything.
>
> ItemID and BatchID are read only and brought in through BatchItem on the
> original select statement. DataField1 and 2 are the two fields actually
> being updated by the user through dropdownlists bound to BatchItem.DataList1
> and BatchItem.DataList2 respectively
>
> The static BatchData.GetItems method seems to be working properly as the
> grid is being populated with the correct data.
>
> The static BatchData.UpdateItem method takes in a BatchItem object (since I
> got an error when I defined it to explicitly take in ItemID, BatchID,
> DataField1, DataField2 saying it needed to take in a BatchItem as a
> parameter). It then creates a collection of SqlParameters and calls a stored
> proc to perform the update.
>
> The problem is, when the row is updated, the application is instantiating a
> default constructor version of BatchItem which is empty and passing that to
> BatchData.UpdateItem().
>
> So what I need to figure out is how to get the data from the GridView row
> being updated (the four properties listed in "UpdateParameters" on the object
> data source) into the BatchItem that is passed to BatchData.UpdateItem().
>
> I've been using some of Phillip Williams' examples to model from (if you're
> listening, Phillip), particularly the "DropdownList within a GridView"
> example and where I see a disconnect is that the UpdateParameters don't seem
> to be tied to anything in the grid data particularly. Unfortunately without
> being able to view the source of his UpdateOrder method (the UpdateMethod on
> his object data source) I can't compare what I'm doing to what he is doing to
> see the problem.
>
> Am I supposed to be overriding one of the GridView events to stuff a
> ConfirmItem object with the correct field data? The UpdateItem method only
> takes ConfirmItem so I don't seem to have access to a parameters collection.
>
> Help is greatly appreciated, thanks! If you need more information please
> let me know.

 
Reply With Quote
 
 
 
 
Phillip Williams
Guest
Posts: n/a
 
      01-17-2006
Or I should have rather said: Make sure that your definition of the BatchItem
class contains a default constructor that receives no parameters and that it
has public properties to set the values of its data with names matching those
used by the scalar parameters that are defined in the ObjectDataSource.


"Phillip Williams" wrote:

>
> The update parameters pass scalar values to an object of type clsOrder.
> Notice that the clsOrder definition,
> http://www.webswapp.com/CodeSamples/...20\clsOrder.cs,
> has a default constructor (without any parameters defined). It has public
> properties matching the names of the parameters passed through the
> objectDataSource.
>
> The empty constructor allows the ObjectDatasource to instantiate the class
> then the ObjectDataSource parameters set the public properties of the class
> before passing it to the method specified by the UpdateMethod attribute.
>
> So to answer your question
>
> >… how to get the data from the GridView row being updated
> >(the four properties listed in "UpdateParameters" on the object
> >data source) into the BatchItem that is passed to
> >BatchData.UpdateItem()”

>
> Make sure that your definition of the BatchItem class contains a default
> constructor that receives no parameters, e.g.
>
> Public class BatchItem
> {
> public BatchItem(){ //default constructor }
>
> // the rest of the class implementation
> }
>
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "anyeone" wrote:
>
> > I've got a GridView bound to an Object Data Source and I'm having trouble
> > figuring out just how to set it up to update properly.
> >
> > I have the object data source defined as so:
> >
> > <asp:ObjectDataSource ID="objBatchData" runat="server"
> > SelectMethod="GetItems" TypeName="MyNamespace.BatchData"
> > DataObjectTypeName="MyNamespace.BatchItem" UpdateMethod="UpdateItem" >
> > <SelectParameters>
> > <asp:QueryStringParameter Name="BatchID" QueryStringField="BatchID"
> > Type="Int32" />
> > </SelectParameters>
> > <UpdateParameters>
> > <asparameter Name="ItemID" Type="Int32" />
> > <asparameter Name="BatchID" Type="Int32" />
> > <asparameter Name="DataField1" Type="string" />
> > <asparameter Name="DataField2" Type="string" />
> > </UpdateParameters>
> > </asp:ObjectDataSource>
> >
> > .
> >
> > BatchItem is a business object that defines several constructors. The
> > BatchItem(DataRow row) constructor is the one that GetItems() uses to
> > instantiate individual BatchItems when the object data source binding occurs.
> > There is also a BatchItem(int ItemID) constructor and a default constructor
> > that doesn't actually do anything.
> >
> > ItemID and BatchID are read only and brought in through BatchItem on the
> > original select statement. DataField1 and 2 are the two fields actually
> > being updated by the user through dropdownlists bound to BatchItem.DataList1
> > and BatchItem.DataList2 respectively
> >
> > The static BatchData.GetItems method seems to be working properly as the
> > grid is being populated with the correct data.
> >
> > The static BatchData.UpdateItem method takes in a BatchItem object (since I
> > got an error when I defined it to explicitly take in ItemID, BatchID,
> > DataField1, DataField2 saying it needed to take in a BatchItem as a
> > parameter). It then creates a collection of SqlParameters and calls a stored
> > proc to perform the update.
> >
> > The problem is, when the row is updated, the application is instantiating a
> > default constructor version of BatchItem which is empty and passing that to
> > BatchData.UpdateItem().
> >
> > So what I need to figure out is how to get the data from the GridView row
> > being updated (the four properties listed in "UpdateParameters" on the object
> > data source) into the BatchItem that is passed to BatchData.UpdateItem().
> >
> > I've been using some of Phillip Williams' examples to model from (if you're
> > listening, Phillip), particularly the "DropdownList within a GridView"
> > example and where I see a disconnect is that the UpdateParameters don't seem
> > to be tied to anything in the grid data particularly. Unfortunately without
> > being able to view the source of his UpdateOrder method (the UpdateMethod on
> > his object data source) I can't compare what I'm doing to what he is doing to
> > see the problem.
> >
> > Am I supposed to be overriding one of the GridView events to stuff a
> > ConfirmItem object with the correct field data? The UpdateItem method only
> > takes ConfirmItem so I don't seem to have access to a parameters collection.
> >
> > Help is greatly appreciated, thanks! If you need more information please
> > let me know.

 
Reply With Quote
 
anyeone
Guest
Posts: n/a
 
      01-17-2006
Hi Phillip,

I do have a default constructor defined (it is being called properly) and I
have properties for all of the parameters, however none of the set methods
are being called on any of the properties.

When I step through the code, first the BatchItem() constructor is called,
then the UpdateItem(BatchItem) method, but none of the setters are called at
all.

If I declare the objBatchData_Updating event just so I can take a look at
the BatchItem being created, I see it is totally empty (as it is when it
reaches the UpdateItem method).

So it seems that for some reason the application can't figure out to
automatically call the set methods even though the property names match the
name and type on the UpdateParameters.

The only thing I am seeing different is that I have other properties on the
object that I do not have explicitly listed as UpdateParameters that are
display only and not needed for the update stored proc - but I don't know why
that would be a problem. They all define set methods just in case though.

Any ideas?

Thanks,
Anye

 
Reply With Quote
 
Phillip Williams
Guest
Posts: n/a
 
      01-17-2006
I can simulate the problem you are describing if I replace the 2-way
databinding on the controls with one-way only, e.g, if I replace Bind by
Eval. In this case there are no parameters to be updated and the
ObjectDataSource would not update them. Could that be the reason in your
case?
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


"anyeone" wrote:

> Hi Phillip,
>
> I do have a default constructor defined (it is being called properly) and I
> have properties for all of the parameters, however none of the set methods
> are being called on any of the properties.
>
> When I step through the code, first the BatchItem() constructor is called,
> then the UpdateItem(BatchItem) method, but none of the setters are called at
> all.
>
> If I declare the objBatchData_Updating event just so I can take a look at
> the BatchItem being created, I see it is totally empty (as it is when it
> reaches the UpdateItem method).
>
> So it seems that for some reason the application can't figure out to
> automatically call the set methods even though the property names match the
> name and type on the UpdateParameters.
>
> The only thing I am seeing different is that I have other properties on the
> object that I do not have explicitly listed as UpdateParameters that are
> display only and not needed for the update stored proc - but I don't know why
> that would be a problem. They all define set methods just in case though.
>
> Any ideas?
>
> Thanks,
> Anye
>

 
Reply With Quote
 
anyeone
Guest
Posts: n/a
 
      01-18-2006
It doesn't look like it - I've tried it both ways (using Bind and Eval). Of
course - both fields that are changing are coming from a dropdown list so if
for some reason the grid isn't checking those dropdown lists for changed
values then it might think nothing changed? This wasn't something you were
doing in your example so I'm not sure I wired up the dropdownlists right.

Here's my GridView definition:

<asp:GridView ID="grdBatch" runat="server" DataSourceID="objBatchData"
EnableViewState="False" AutoGenerateColumns="False">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="ItemID" HeaderText="ID" Visible="false"
ReadOnly="True" />
<asp:BoundField DataField="BatchID" HeaderText="BatchID"
Visible="false" ReadOnly="True" />
<asp:BoundField DataField="BatchIndex" HeaderText="Batch #"
ReadOnly="True" />
<asp:BoundField DataField="ItemNum" HeaderText="Item"
ReadOnly="True" />
<asp:TemplateField HeaderText="Data Field 1">
<ItemTemplate>
<asp:Label ID="lblDF1" runat="server" Text='<%#
Bind("DataField1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<aspropDownList ID="ddlDF1" runat="server" DataSource='<%#
Bind("DF1List") %>' DataTextField="DF1Description"
DataValueField="DF1Code"></aspropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DataField2">
<ItemTemplate>
<asp:Label ID="lblDF2" runat="server" Text='<%#
Bind("DataField2") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<aspropDownList ID="ddlDF2" runat="server"
DataSource='<%# Bind("DF2List") %>' DataTextField="DF2Description"
DataValueField="DF2Code"></aspropDownList>
</EditItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>


I have tried it both as is and replacing the dropdown list Bind with "Eval"
in the EditItemTemplates. BatchIndex and ItemNum are for display only and
are not part of the update parameters.

Thanks again for the help!

 
Reply With Quote
 
anyeone
Guest
Posts: n/a
 
      01-18-2006
Ok, I tried for yucks making some of the read-only fields editable and as
soon as I did the set methods started being called - but still not for the
dropdown list fields (those properties stayed null).

So the problem has to be that the app doesn't know that it has to get the
new value for the DataField1 and DataField2 fields from the dropdownlists.

What else do I have to do to those particular templates?":
<asp:TemplateField HeaderText="Data Field 1">
<ItemTemplate>
<asp:Label ID="lblDF1" runat="server"
Text='<%# Bind("DataField1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<aspropDownList ID="ddlDF1" runat="server"
DataSource='<%#Bind("DF1List") %>'

DataTextField="DF1Description"
DataValueField="DF1Code"></aspropDownList>
</EditItemTemplate>
</asp:TemplateField>
 
Reply With Quote
 
Phillip Williams
Guest
Posts: n/a
 
      01-18-2006
Of course it does not. Your code shows that the dropdownlist is bound to a
list named DF1List whereas your DataField1 is bound to a label. You might
need to update your label with the SelectedValue of the dropdownlist on
SelectedIndexChanged if you want it to be saved in the BatchItem object.

In my demo I did not update the values that were browsed by the
dropdownlist. This is why I did not implement such a function. (But now I
might add this to the demo when I get some free time; tomorrow perhaps)
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


"anyeone" wrote:

> Ok, I tried for yucks making some of the read-only fields editable and as
> soon as I did the set methods started being called - but still not for the
> dropdown list fields (those properties stayed null).
>
> So the problem has to be that the app doesn't know that it has to get the
> new value for the DataField1 and DataField2 fields from the dropdownlists.
>
> What else do I have to do to those particular templates?":
> <asp:TemplateField HeaderText="Data Field 1">
> <ItemTemplate>
> <asp:Label ID="lblDF1" runat="server"
> Text='<%# Bind("DataField1") %>'></asp:Label>
> </ItemTemplate>
> <EditItemTemplate>
> <aspropDownList ID="ddlDF1" runat="server"
> DataSource='<%#Bind("DF1List") %>'
>
> DataTextField="DF1Description"
> DataValueField="DF1Code"></aspropDownList>
> </EditItemTemplate>
> </asp:TemplateField>

 
Reply With Quote
 
anyeone
Guest
Posts: n/a
 
      01-18-2006
Ok, after several google searches on various key concepts I've finally got it
working. There were a couple of things that had to be wired:

1) grdBatch_RowDataBound needed to select the proper item from the
dropdownlist when in edit mode

2) Set the hidden fields (ID, BatchID) that are needed for the update query
as key fields using DataKeyFieldNames property of the grid - if they aren't
DataKeyFields you can't find the values since the columns are hidden

3) grdBatch_RowUpdating needed to explicitly set the DataField1, DataField2,
and the hidden field values by finding the dropdownlist controls and getting
the values of the data key fields.

Part of my problem in finding the info on how to do this is that some of the
information seems tied to the object data source and some seems tied to the
GridView so the tidbits of knowledge needed to fully wire it were spread all
across the API. (Which is why good, thorough examples are worth their
weight in gold! Thanks to Phillip and others who take the time to provide
them.)

Phew. Time to go home.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
gridview databinding to object data source Allan Ebdrup ASP .Net Web Controls 1 10-09-2006 02:07 AM
Object data source, setting parameter source to Page.Partner.Guid Allan Ebdrup ASP .Net Web Controls 2 10-06-2006 10:24 AM
Two way sorting in GridView using Object Data Source Ram ASP .Net 1 05-23-2006 04:37 AM
Data Recovery SOURCE CODE ( SOURCE CODES of Professional Data Recovery Software ) Author Tarun Tyagi C Programming 0 12-29-2004 05:10 PM
Data Recovery SOURCE CODE ( SOURCE CODES of Professional Data Recovery Software ) Author Tarun Tyagi Cisco 0 12-29-2004 05:03 PM



Advertisments