470,591 Members | 1,832 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,591 developers. It's quick & easy.

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 1725
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Demetri | last post: by
2 posts views Thread by Jami Bradley | last post: by
4 posts views Thread by tim.cavins | last post: by
1 post views Thread by rote | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.