473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Wrong parameter being passed from an ObjectDataSource's SelectMethod

Help! Need this fixed ASAP.

I have a GridView/DetailsView master/details form set up, with both
bound to separate ObjectDataSource objects. Both the GridView and the
DetailsView have a DataKeyNames parameter of "UserCompany,
BillableCompany". The DetailsView has control parameters for its
select method pointing to these two keys on the GridView. But the
select method function receives two copies of the UserCompany field!
The BillableCompany field never gets passed correctly.

What gives?

A simplified version of my ASP.NET code follows:

<%@ Page Language="VB" AutoEventWireup="true"
CodeFile="EditBillableCompanies.aspx.vb"
Inherits="EditBillableCompanies" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Billable Companies</title>
</head>
<body style="font-size: 12pt">
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:GridView ID="GridView1"
AutoGenerateColumns="False"
DataSourceID="MasterSource1"
DataKeyNames="UserCompany, BillableCompany"
runat="server">
<Columns>
<asp:CommandField ShowSelectButton="True"
/>
<asp:BoundField DataField="UserCompany"
HeaderText="User Company" />
<asp:BoundField DataField="BillableCompany"
HeaderText="Billable Company" />
</Columns>
</asp:GridView>
</td>
<td valign="top">
<asp:DetailsView ID="DetailsView1"
AutoGenerateRows="False"
DataSourceID="DetailsSource1"
DataKeyNames="UserCompany, BillableCompany"
EmptyDataText="No records."
runat="server">
<Fields>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="ContactName"
HeaderText="Contact Name"
SortExpression="ContactName" />
<asp:BoundField DataField="ContactPhone"
HeaderText="Contact Phone"
SortExpression="ContactPhone" />
<asp:BoundField DataField="ContactEmail"
HeaderText="Contact Email"
SortExpression="ContactEmail" />
</Fields>
</asp:DetailsView>
</td>
</tr>
</table>
&nbsp;
&nbsp;
<asp:ObjectDataSource
ID="DetailsSource1"
TypeName="BillableCompaniesDetailView"
ConflictDetection="CompareAllValues"
SelectMethod="SelectBillableCompanies"
UpdateMethod="UpdateBillableCompanies"
runat="server">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1"
Name="UserCompany"
PropertyName="SelectedValue"
Type="String" />
<asp:ControlParameter ControlID="GridView1"
Name="BillableCompany"
PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
<UpdateParameters>
...
</UpdateParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource
ID="MasterSource1"
TypeName="BillableCompaniesMasterView"
SelectMethod="SelectBillableCompanies"
runat="server">
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>

The BillableCompaniesDetailView class includes the following function:

Public Function SelectBillableCompanies(ByVal UserCompany As String, _
ByVal BillableCompany As String) As MySqlDataReader

BillableCompany is always the same value as UserCompany, and when this
function is called, they are both from the UserCompany field of my
MySql database.

Help!

Damon

Feb 2 '06 #1
2 3801
> I have a GridView/DetailsView master/details form set up, with both
bound to separate ObjectDataSource objects. Both the GridView and the
DetailsView have a DataKeyNames parameter of "UserCompany,
BillableCompany". The DetailsView has control parameters for its
select method pointing to these two keys on the GridView. But the
select method function receives two copies of the UserCompany field!
The BillableCompany field never gets passed correctly.


I hope that a simple solution exists for your problem and someone will
provide you with it but having similar problems with parameters that should
be passed to Insert/Update methods, I would suggest you to write a handler
for ObjectDataSource's Selecting event. inside the event you get the
collection of parameters that are passed to the Select method. however, you
are free to modify this collection. In particular, you can clear the whole
collection and put inside any names/values you wish. I use this techique
constantly for inserting/updating so I have one generic inserting/updating
method in the data source type and I am able to call this method from any
context that needs to insert/modify data no matter how many attributes are
actually modified by the user interface.

regards,
Wiktor Zychla

Feb 2 '06 #2
Well, I finally found a solution. After much laborious web searching,
I found that using a ControlParameter to point back to the GridView
with the selected value pointed to by the PropertyName field only works
if there is only one ControlParameter! The selected value will always
be from the first column of the GridView.

In order to address this, we need to use an event handler to catch the
select event of the GridView object and then manipulate the parameters
that are passed to the ObjectDataSource control for the DetailsView
object. An event handler for the DetailsView or the ObjectDataSource
control won't work (and I found this out the hard way!) because the
parameter values are modified by the select event handler for the
GridView afterwards.

Put the following event handler at the top of the asp page, right after
the Page and Doctype definitions, as follows:

<script runat="Server">
Sub GridView1_Select(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles GridView1.SelectedIndexChanged
Dim gv As GridView
gv = sender
If Not gv.SelectedRow() Is Nothing Then

DetailsSource1.SelectParameters("UserCompany").Def aultValue() =
gv.SelectedRow().Cells.Item(1).Text()

DetailsSource1.SelectParameters("BillableCompany") .DefaultValue() =
gv.SelectedRow().Cells.Item(2).Text()
End If
End Sub
</script>

Then, tell the GridView object about this event handler:

<asp:GridView ID="GridView1"
AutoGenerateColumns="False"
DataSourceID="MasterSource1"
DataKeyNames="UserCompany, BillableCompany"
OnSelectedIndexChanged="GridView1_Select" <--
Add this
runat="server">
<Columns>
<asp:CommandField ShowSelectButton="True"
/>
<asp:BoundField DataField="UserCompany"
HeaderText="User Company" />
<asp:BoundField DataField="BillableCompany"
HeaderText="Billable Company" />
</Columns>
</asp:GridView>

Finally, make sure that the parameters for the ObjectDataSource object
associated with the DetailsView are NOT ControlParameters, like so:

<asp:ObjectDataSource
ID="DetailsSource1"
TypeName="BillableCompaniesDetailView"
DataObjectTypeName="BillableCompaniesType"
ConflictDetection="CompareAllValues"
OldValuesParameterFormatString="original_{0}"
SelectMethod="SelectBillableCompanies"
UpdateMethod="UpdateBillableCompanies"
runat="server">
<SelectParameters>
<asp:Parameter <-- not a ControlParameter!
Name="UserCompany"
Type="String" />
<asp:Parameter <-- not a ControlParameter!
Name="BillableCompany"
Type="String" />
</SelectParameters>
<UpdateParameters>
...
</UpdateParameters>
</asp:ObjectDataSource>

And that's it. Now the parameter values will be correct.

Damon

Feb 3 '06 #3

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

Similar topics

0
by: Kestas | last post by:
Is there a way to use an Attribute for ObjectDataSource's SelectMethod, and not a method ? An attribute returns some a datatable, but I can't set this attribute as value of SelectMethod. I...
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...
0
by: David Hubbard | last post by:
I am using a GridView to display a set of objects that have a parent-child relationship. Each object, MyBO, has an ID property that is used to get the children of that object. class MyBO { ...
1
by: Jürgen Bayer | last post by:
Hi, I just tried out the ObjectDataSource of ASP.NET 2.0. A simple application works with a GridView bound to an ObjectDataSource. The ObjectDataSource is set to a (factory) class...
2
by: SJL | last post by:
I have a GridView (ASP.NET 2.0 + SQL Sever 2005) control that is bound to an Object Data Source (ODS). The ODS has a SelectParameter for the sort expression. The SelectParameter has a DefaultValue...
1
by: Roger | last post by:
Hi all On a page I am using a repeater which is linked to an ObjectDataSource (same page): <asp:Repeater ID="VideoListNewestRepeater" runat="server" DataSourceID="VideoListNewestDataSource">...
0
by: Froefel | last post by:
I'm using ObjectDataSources to access my data, and while I gathered very useful code from a variety of sources to get started, I keep running into an issue. First off, everything actually works,...
0
by: =?Utf-8?B?UGF1bA==?= | last post by:
I have a ListBox server control named "lb_dates" with a SelectionMode of "Multiple". The user can select multiple dates from the listbox. I have ObjectDataSource named "ods_rfq" with a...
1
by: elka | last post by:
I 'm going to populate a gridview with data from stored procedure in SQL server. I wrote a method which returns a dataset. than I added an object datasource with all parameters. onbutton click I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.