culebrin 0 Junior Poster in Training

Hi,

I have a dataset build at design time (vs 2008) with several tables of my DB (MSSQL 2005).

I have a DataTable with its tableadapter... I need that the TableAdapter.Update function returns the key updated (always will be a single row updated at the time).

I know, I passed the primarykey to filter the update statement, so it means that I already have the value, but I really need the update function return the key value ...

I tried replacing the update commandtext value replacing the last select statement with "select @keyparameter" statement and executing it as Scalar... but no luck...

Please someone can help me with this... It would very apreciated...

Thanks

Dim dt As New DA.Repositorio.ItemMostradorDataTable
            ItemMostradorTA.FillByKey(dt, Me.id, Nothing)

            With dt.Rows(0)
                '.Item("itemm_id") = Me.id
                .Item("itemm_descripcion") = Me.descripcion
                .Item("unime_id") = Me.unime_id
                .Item("categ_id") = Me.categ_id
                .Item("itemm_codigo_barras") = Me.itemm_codigo_barras
                .Item("itemm_minimo_alerta") = Me.minimo_alerta
                .Item("itemm_costo") = Me.itemm_costo
                .Item("itemm_precio_venta") = Me.itemm_precio_venta
                .Item("mostr_id") = Me.repositorio_id
                .Item("itemm_estado_registro") = Me.estado_registro
                .Item("creado_usuario") = Me.creado_usuario
                .Item("creado_fecha") = IIf(Me.creado_fecha.Ticks = 0, System.DBNull.Value, Me.creado_fecha)
                .Item("modificado_usuario") = Me.modificado_usuario
                .Item("modificado_fecha") = IIf(Me.modificado_fecha.Ticks = 0, System.DBNull.Value, Me.modificado_fecha)
            End With

            response = ItemMostradorTA.Update(dt)

Dani AI

Generated

Quick answer for : the generated TableAdapter.Update method returns the count of rows affected — it will not return a primary key value by itself. To get the PK back after an UPDATE you must have the database command return it and instruct ADO.NET to apply that returned value to the DataRow (or read it in a RowUpdated handler).

A reliable pattern is to have the UPDATE statement return the updated row (SQL Server’s OUTPUT clause) and set the command’s UpdatedRowSource so ADO.NET can merge the returned values into the DataRow. For example, change the UpdateCommand to return the columns you need and set UpdateCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord. Make sure the returned column names/types match the DataTable schema so ADO.NET can map them back.

If you prefer parameters, add an output parameter to the UpdateCommand and handle the adapter’s RowUpdated event to copy that parameter value into the DataRow. The RowUpdated event gives you access to the command and its parameters and runs for each row, so it’s safe for single-row updates.

Troubleshooting tips: ensure UpdatedRowSource is not left at None; if triggers change values prefer OUTPUT inserted.* (works on SQL Server 2005+); when using strongly-typed TableAdapters you can edit the command text/UpdatedRowSource in the DataSet designer or in code via the Adapter property; finally, verify your parameter directions and SourceColumn mappings. Microsoft docs that explain these pieces: DataAdapter.Update, UpdatedRowSource enum and SQL Server OUTPUT clause.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.