Gridview edit not updating database

The Grid View allows editing on a row-by-row basis.

Public Shared Sub Update Product(By Val original_Product ID As Integer, _ By Val product Name As String, By Val unit Price As Decimal, _ By Val units In Stock As Integer) 'Updates the Products table Dim sql As String = "UPDATE Products SET Product Name = " & _ " @Product Name, Unit Price = @Unit Price, Units In Stock = " & _ "@Units In Stock WHERE Product ID = @Product ID" Using my Connection As _ New Sql Connection( _ Configuration Manager. Connection String) Dim my Command As New Sql Command(sql, my Connection) my Command. Add( _ New Sql Parameter("@Product Name", product Name)) my Command. Add(New Sql Parameter("@Unit Price", _ unit Price)) my Command. Add(New Sql Parameter("@Units In Stock", _ units In Stock)) my Command. Add(New Sql Parameter("@Product ID", _ original_Product ID)) my Connection. If you had configured the Object Data Source to Compare All Values you'd need an Update method with a signature like: ' Visual Basic .

When the end user clicks on an Edit button that row becomes editable, causing the Edit button to change to Update and Cancel buttons and the other columns to become Text Boxes.

The end user can then update one or more column values and click Update to save their changes. Our first demo will look at how to create an editable Grid View whose data comes from a Sql Data Source.

Instead of just using WHERE [Product ID] = @original_Product ID, with optimistic concurrency the WHERE clause would have read: This would prevent an UPDATE from happening if, between the time the user clicked a Grid View row's Edit button and having clicked the Update button, another user had modified that particular row.

Like deleting data from a Grid View bound to an Object Data Source, creating an editable Grid View bound to an Object Data Source requires very few changes in the creation of the ASP. Most of the work is required in adding a method to the data access layer class to handle the update.

Leave a Reply