473,215 Members | 1,271 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,215 software developers and data experts.

ObjectDataSource won't perform updates

I keep getting this error when trying to update records using an
ObjectDataSource. I have seen others post similar errors...but I haven't
found any solutions.

ObjectDataSource 'ObjectDataSource1' could not find a non-generic method
'UpdateProduct' that has parameters: ProductName, UnitPrice, ProductId.

I have been unable to work through this. Any ideas?

I’m using code pasted directly from this page:
http://msdn.microsoft.com/asp.net/re...asource_topic2

Here is code from the class I created:

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient

Public Class ProductInfo3

Const conString As String = _
"Server=localhost;Trusted_Connection=true;Database =Northwind"

Public Shared Function GetProducts() As SqlDataReader
Dim con As New SqlConnection(conString)
Dim selectString As String = "SELECT ProductId, " & _
"ProductName,UnitPrice FROM Products ORDER BY ProductId"
Dim cmd As New SqlCommand(selectString, con)
con.Open()
Dim dtr As SqlDataReader = _
cmd.ExecuteReader(CommandBehavior.CloseConnection)
Return dtr
End Function

Public Shared Sub UpdateProduct(ByVal original_productId _
As Integer, ByVal productName As String, _
ByVal unitPrice As Decimal)
Dim con As New SqlConnection(conString)
Dim updateString As String = "UPDATE Products " & _
"SET ProductName=@ProductName,UnitPrice=@UnitPrice " & _
"WHERE ProductID=@ProductID"
Dim cmd As New SqlCommand(updateString, con)
cmd.Parameters.AddWithValue("@ProductName", productName)
cmd.Parameters.AddWithValue("@UnitPrice", unitPrice)
cmd.Parameters.AddWithValue("@ProductId", original_productId)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub

Public Shared Sub DeleteProduct(ByVal original_productId _
As Integer)
Dim con As New SqlConnection(conString)
Dim deleteString As String = "DELETE Products " & _
"WHERE ProductID=@ProductID"
Dim cmd As New SqlCommand(deleteString, con)
cmd.Parameters.AddWithValue("@ProductId", original_productId)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub

End Class
Partial Class DirectoryLocs5
Inherits System.Web.UI.Page

End Class

Here is the .aspx page:

<html>
<head>
<title>Show Products</title>
</head>
<body>
<form id="form1" runat="server">

<asp:GridView
ID="GridView1"
DataSourceID="ObjectDataSource1"
DataKeyNames="ProductId"
AutoGenerateColumns="false"
AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true"
Runat="Server">
<Columns>
<asp:BoundField
DataField="ProductName"/>
<asp:BoundField
DataField="UnitPrice"
DataFormatString="{0:c}"/>
</Columns>
</asp:GridView>

<asp:ObjectDataSource
ID="ObjectDataSource1"
TypeName="ProductInfo3"
SelectMethod="GetProducts"
UpdateMethod="UpdateProduct"
DeleteMethod="DeleteProduct"
Runat="Server" />

</form>
</body>
</html>
Dec 15 '05 #1
3 1839
Change the function definition to look like that:
Public Shared Sub UpdateProduct(ByVal ProductId _
As Integer, ByVal productName As String, _
ByVal UnitPrice As Decimal)
and make changes to the variable name (ProductID instead of
original_productID) within the function body accordingly.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"mthomason" wrote:
I keep getting this error when trying to update records using an
ObjectDataSource. I have seen others post similar errors...but I haven't
found any solutions.

ObjectDataSource 'ObjectDataSource1' could not find a non-generic method
'UpdateProduct' that has parameters: ProductName, UnitPrice, ProductId.

I have been unable to work through this. Any ideas?

I’m using code pasted directly from this page:
http://msdn.microsoft.com/asp.net/re...asource_topic2

Here is code from the class I created:

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient

Public Class ProductInfo3

Const conString As String = _
"Server=localhost;Trusted_Connection=true;Database =Northwind"

Public Shared Function GetProducts() As SqlDataReader
Dim con As New SqlConnection(conString)
Dim selectString As String = "SELECT ProductId, " & _
"ProductName,UnitPrice FROM Products ORDER BY ProductId"
Dim cmd As New SqlCommand(selectString, con)
con.Open()
Dim dtr As SqlDataReader = _
cmd.ExecuteReader(CommandBehavior.CloseConnection)
Return dtr
End Function

Public Shared Sub UpdateProduct(ByVal original_productId _
As Integer, ByVal productName As String, _
ByVal unitPrice As Decimal)
Dim con As New SqlConnection(conString)
Dim updateString As String = "UPDATE Products " & _
"SET ProductName=@ProductName,UnitPrice=@UnitPrice " & _
"WHERE ProductID=@ProductID"
Dim cmd As New SqlCommand(updateString, con)
cmd.Parameters.AddWithValue("@ProductName", productName)
cmd.Parameters.AddWithValue("@UnitPrice", unitPrice)
cmd.Parameters.AddWithValue("@ProductId", original_productId)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub

Public Shared Sub DeleteProduct(ByVal original_productId _
As Integer)
Dim con As New SqlConnection(conString)
Dim deleteString As String = "DELETE Products " & _
"WHERE ProductID=@ProductID"
Dim cmd As New SqlCommand(deleteString, con)
cmd.Parameters.AddWithValue("@ProductId", original_productId)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub

End Class
Partial Class DirectoryLocs5
Inherits System.Web.UI.Page

End Class

Here is the .aspx page:

<html>
<head>
<title>Show Products</title>
</head>
<body>
<form id="form1" runat="server">

<asp:GridView
ID="GridView1"
DataSourceID="ObjectDataSource1"
DataKeyNames="ProductId"
AutoGenerateColumns="false"
AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true"
Runat="Server">
<Columns>
<asp:BoundField
DataField="ProductName"/>
<asp:BoundField
DataField="UnitPrice"
DataFormatString="{0:c}"/>
</Columns>
</asp:GridView>

<asp:ObjectDataSource
ID="ObjectDataSource1"
TypeName="ProductInfo3"
SelectMethod="GetProducts"
UpdateMethod="UpdateProduct"
DeleteMethod="DeleteProduct"
Runat="Server" />

</form>
</body>
</html>

Dec 15 '05 #2
Thanks Phillip. That does fix the problem. However, it seems to contradict
the MSDN article. Here is the paragraph which explains why 'original_' is
added...

"When you click the Update link to update a product, the ObjectDataSource
control calls the UpdateProduct method. The GridView control automatically
creates three parameters when calling the UpdateProduct method:
original_productID, productName, and unitPrice. The productName and unitPrice
parameters correspond to the two BoundFields displayed by the GridView. One
parameter requires additional discussion. We need to pass the value of the
ProductID column of the row being updated to the UpdateProduct method. Since
we are not displaying the ProductID column in the GridView, we must assign
the ProductID column to the GridView control's DataKeyNames property. The
name of this column becomes original_productId instead of productId since we
are passing the unedited version of the ProductID column to the update
method."

"Phillip Williams" wrote:
Change the function definition to look like that:
Public Shared Sub UpdateProduct(ByVal ProductId _
As Integer, ByVal productName As String, _
ByVal UnitPrice As Decimal)
and make changes to the variable name (ProductID instead of
original_productID) within the function body accordingly.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"mthomason" wrote:
I keep getting this error when trying to update records using an
ObjectDataSource. I have seen others post similar errors...but I haven't
found any solutions.

ObjectDataSource 'ObjectDataSource1' could not find a non-generic method
'UpdateProduct' that has parameters: ProductName, UnitPrice, ProductId.

I have been unable to work through this. Any ideas?

I’m using code pasted directly from this page:
http://msdn.microsoft.com/asp.net/re...asource_topic2

Here is code from the class I created:

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient

Public Class ProductInfo3

Const conString As String = _
"Server=localhost;Trusted_Connection=true;Database =Northwind"

Public Shared Function GetProducts() As SqlDataReader
Dim con As New SqlConnection(conString)
Dim selectString As String = "SELECT ProductId, " & _
"ProductName,UnitPrice FROM Products ORDER BY ProductId"
Dim cmd As New SqlCommand(selectString, con)
con.Open()
Dim dtr As SqlDataReader = _
cmd.ExecuteReader(CommandBehavior.CloseConnection)
Return dtr
End Function

Public Shared Sub UpdateProduct(ByVal original_productId _
As Integer, ByVal productName As String, _
ByVal unitPrice As Decimal)
Dim con As New SqlConnection(conString)
Dim updateString As String = "UPDATE Products " & _
"SET ProductName=@ProductName,UnitPrice=@UnitPrice " & _
"WHERE ProductID=@ProductID"
Dim cmd As New SqlCommand(updateString, con)
cmd.Parameters.AddWithValue("@ProductName", productName)
cmd.Parameters.AddWithValue("@UnitPrice", unitPrice)
cmd.Parameters.AddWithValue("@ProductId", original_productId)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub

Public Shared Sub DeleteProduct(ByVal original_productId _
As Integer)
Dim con As New SqlConnection(conString)
Dim deleteString As String = "DELETE Products " & _
"WHERE ProductID=@ProductID"
Dim cmd As New SqlCommand(deleteString, con)
cmd.Parameters.AddWithValue("@ProductId", original_productId)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub

End Class
Partial Class DirectoryLocs5
Inherits System.Web.UI.Page

End Class

Here is the .aspx page:

<html>
<head>
<title>Show Products</title>
</head>
<body>
<form id="form1" runat="server">

<asp:GridView
ID="GridView1"
DataSourceID="ObjectDataSource1"
DataKeyNames="ProductId"
AutoGenerateColumns="false"
AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true"
Runat="Server">
<Columns>
<asp:BoundField
DataField="ProductName"/>
<asp:BoundField
DataField="UnitPrice"
DataFormatString="{0:c}"/>
</Columns>
</asp:GridView>

<asp:ObjectDataSource
ID="ObjectDataSource1"
TypeName="ProductInfo3"
SelectMethod="GetProducts"
UpdateMethod="UpdateProduct"
DeleteMethod="DeleteProduct"
Runat="Server" />

</form>
</body>
</html>

Dec 16 '05 #3
1- In order to have the original_ProductID strategy working, you needed to
define another attribute within the objectDataSource:
OldValuesParameterFormatString="original_{0}"
2- You were not editing the ProductID (it was a readonly field), therefore
there was no need to keep the OldValuesParameterFormatString

Basically the error message that you posted at the beginning was that there
was no parameter defined as original_ProductID (as your function was).
Another solution to the error you got was to add the
OldValuesParameterFormatString="original_{0}"

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"mthomason" wrote:
Thanks Phillip. That does fix the problem. However, it seems to contradict
the MSDN article. Here is the paragraph which explains why 'original_' is
added...

"When you click the Update link to update a product, the ObjectDataSource
control calls the UpdateProduct method. The GridView control automatically
creates three parameters when calling the UpdateProduct method:
original_productID, productName, and unitPrice. The productName and unitPrice
parameters correspond to the two BoundFields displayed by the GridView. One
parameter requires additional discussion. We need to pass the value of the
ProductID column of the row being updated to the UpdateProduct method. Since
we are not displaying the ProductID column in the GridView, we must assign
the ProductID column to the GridView control's DataKeyNames property. The
name of this column becomes original_productId instead of productId since we
are passing the unedited version of the ProductID column to the update
method."

"Phillip Williams" wrote:
Change the function definition to look like that:
Public Shared Sub UpdateProduct(ByVal ProductId _
As Integer, ByVal productName As String, _
ByVal UnitPrice As Decimal)
and make changes to the variable name (ProductID instead of
original_productID) within the function body accordingly.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"mthomason" wrote:
I keep getting this error when trying to update records using an
ObjectDataSource. I have seen others post similar errors...but I haven't
found any solutions.

ObjectDataSource 'ObjectDataSource1' could not find a non-generic method
'UpdateProduct' that has parameters: ProductName, UnitPrice, ProductId.

I have been unable to work through this. Any ideas?

I’m using code pasted directly from this page:
http://msdn.microsoft.com/asp.net/re...asource_topic2

Here is code from the class I created:

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient

Public Class ProductInfo3

Const conString As String = _
"Server=localhost;Trusted_Connection=true;Database =Northwind"

Public Shared Function GetProducts() As SqlDataReader
Dim con As New SqlConnection(conString)
Dim selectString As String = "SELECT ProductId, " & _
"ProductName,UnitPrice FROM Products ORDER BY ProductId"
Dim cmd As New SqlCommand(selectString, con)
con.Open()
Dim dtr As SqlDataReader = _
cmd.ExecuteReader(CommandBehavior.CloseConnection)
Return dtr
End Function

Public Shared Sub UpdateProduct(ByVal original_productId _
As Integer, ByVal productName As String, _
ByVal unitPrice As Decimal)
Dim con As New SqlConnection(conString)
Dim updateString As String = "UPDATE Products " & _
"SET ProductName=@ProductName,UnitPrice=@UnitPrice " & _
"WHERE ProductID=@ProductID"
Dim cmd As New SqlCommand(updateString, con)
cmd.Parameters.AddWithValue("@ProductName", productName)
cmd.Parameters.AddWithValue("@UnitPrice", unitPrice)
cmd.Parameters.AddWithValue("@ProductId", original_productId)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub

Public Shared Sub DeleteProduct(ByVal original_productId _
As Integer)
Dim con As New SqlConnection(conString)
Dim deleteString As String = "DELETE Products " & _
"WHERE ProductID=@ProductID"
Dim cmd As New SqlCommand(deleteString, con)
cmd.Parameters.AddWithValue("@ProductId", original_productId)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub

End Class
Partial Class DirectoryLocs5
Inherits System.Web.UI.Page

End Class

Here is the .aspx page:

<html>
<head>
<title>Show Products</title>
</head>
<body>
<form id="form1" runat="server">

<asp:GridView
ID="GridView1"
DataSourceID="ObjectDataSource1"
DataKeyNames="ProductId"
AutoGenerateColumns="false"
AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true"
Runat="Server">
<Columns>
<asp:BoundField
DataField="ProductName"/>
<asp:BoundField
DataField="UnitPrice"
DataFormatString="{0:c}"/>
</Columns>
</asp:GridView>

<asp:ObjectDataSource
ID="ObjectDataSource1"
TypeName="ProductInfo3"
SelectMethod="GetProducts"
UpdateMethod="UpdateProduct"
DeleteMethod="DeleteProduct"
Runat="Server" />

</form>
</body>
</html>

Dec 17 '05 #4

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

Similar topics

2
by: Demetri | last post by:
Here is what I want and I can't seem to find it: In asp.net 2.0 I want to create a generic collection that will perform sorting and is capable of being used by the ObjectDataSource and eventually...
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...
0
by: Scott R | last post by:
I have a data layer that is set up to pass datasets to and from the presentation layer. In migrating to 2.0, I would like to use the objectdatasource. The problem I am having is that it seems...
5
by: Trail Monster | last post by:
Ok, I've been searching the net now for several days and can't find how to do this anywhere. Version: VS 2005 Professional Release, 2.0 Framework Background: I have a complex business object...
3
by: CWWong | last post by:
I am using DetailsView using ObjectDataSource with DataSourceTypeName assigned to the specific class. SelectMethod, UpdateMethod and DeleteMethod is working successfully, except InsertMethod. The...
7
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function...
2
by: Jami Bradley | last post by:
I'm in need of some design suggestions! We have a fairly large DB with thousands of users accessing data throughout the day. I have been given the task of restructuring a core part of the web...
4
by: tim.cavins | last post by:
I have a GridView populated by an ObjectDataSource. I am having issues passing the parameters to the objectdatasource. I have verified that the method is being called but none of the parameters...
1
by: rote | last post by:
I was looking into ObjectDatasource and i could do a sort of batchupdate by using a gridview i looped through the controls and updated multiple values. But when i look at the updates in sql it did...
1
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...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.