ObjectDataSource won't perform updates

G

Guest

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/r...ectdatasource.asp#asp2objectdatasource_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>
 
G

Guest

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.
 
G

Guest

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."
 
G

Guest

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}"
 

Ask a Question

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

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

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top