|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
ObjectDataSource won't perform updatesObjectDataSource. 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/reference/data/default.aspx?pull=/library/en-us/dnvs05/html/asp2objectdatasource.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> 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. Show quoteHide quote "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/reference/data/default.aspx?pull=/library/en-us/dnvs05/html/asp2objectdatasource.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> > > 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." Show quoteHide quote "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/reference/data/default.aspx?pull=/library/en-us/dnvs05/html/asp2objectdatasource.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> > > > > 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}" Show quoteHide quote "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/reference/data/default.aspx?pull=/library/en-us/dnvs05/html/asp2objectdatasource.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> > > > > > >
Other interesting topics
Access to the path is denied - trying to write a file
CheckBoxList problem. Want to add some javascript to each checkbox CSS Question Please help me with FxCOP 1.32 and ASP.NET 2.0 Property Feature For Comparing DateTime Error: Cannot use a leading .. to exit above the top directory. Can I have a global.asax just for an application? The type '_Default' conflicts with the imported type '_Default' 2005 Differences in a nutshell? Poping up a window AND redirecting the page thtat triggered the po |
|||||||||||||||||||||||