Usuario
Update no puede encontrar TableMapping['Table'] o DataTable 'Table'.

Pregunta
-
Update no puede encontrar TableMapping['Table'] o DataTable 'Table'. me sale este erro al quere hacer un udpate con un dataset en una trnasaccion le anexo el codigo para ver si me pueden ayudar en esto
DataSet ds_factura = new DataSet();
string sql = "Select * from factura_mov ; select * from productos ";
SqlDataAdapter datad_facmov = new SqlDataAdapter(sql, conexion.conn);DateTime fecha = DateTime.Today;
using (SqlConnection connection = new SqlConnection(conexion.connectionString))
DataTable factura_mov = new DataTable();
datad_facmov.Fill(ds_factura);
ds_factura.Tables[0].TableName = "factura_mov";
ds_factura.Tables[1].TableName = "productos";
{
connection.Open();
SqlCommand command = conexion.conn.CreateCommand();
SqlTransaction transaction;
transaction = conexion.conn.BeginTransaction(IsolationLevel.ReadCommitted);
command.Connection = conexion.conn;
command.Transaction = transaction;
SqlCommand comando_ins = conexion.conn.CreateCommand();
comando_ins.Connection = conexion.conn;
comando_ins.Transaction = transaction;
SqlCommand com_existencia = conexion.conn.CreateCommand();
com_existencia.Connection = conexion.conn;
com_existencia.Transaction = transaction;
try
{
comando_ins.CommandText = @"INSERT INTO factura (folio,id_cliente ,id_vendedor,iva , fecha,tipo_mov ,imp_total)
VALUES (" + txt_folio.Text + "," + txt_id_cliente.Text + "," + txt_id_vendedor.Text + "," + lbl_iva.Text + ",'" + fecha.ToString("d") + "','" + cb_tipo.Text + "','" + lbl_total.Text + "'" + ") SELECT SCOPE_IDENTITY()";
string folio = consulta.obtvalorcampo("select folio from folios where id_folio = 1");
command.CommandText = "update folios set folio = " + (Convert.ToInt16(folio) + 1) + " where id_folio = 1";// agregar 1 al folio para incrementarlo
command.ExecuteNonQuery();// hasta aqui hace el bloqueo
for (int i = 0; i < dg_productos.Rows.Count - 1; i++)// recorro el grid para insertar uno por uno al datatable fac_mov
{
DataRow nuevo_facmov = ds_factura.Tables[0].NewRow();
nuevo_facmov["id_factura"] = id_fac; //valor de la clave para obtener el id articulo
nuevo_facmov["id_arti"] = dg_productos.Rows[i].Cells[1].Value.ToString();
nuevo_facmov["cantidad"] = dg_productos.Rows[i].Cells[3].Value.ToString();
nuevo_facmov["importe"] = dg_productos.Rows[i].Cells[4].Value.ToString();
datad_facmov.SelectCommand.Transaction = transaction;//decimos que es parte de una transaction
SqlCommandBuilder inserta = new SqlCommandBuilder(datad_facmov);//commandbuilder se encargar de inserta
ds_factura.Tables[0].Rows.Add(nuevo_facmov);
inserta.RefreshSchema();
command.CommandText = "update productos set existencia = " + (Convert.ToInt16(dg_productos.Rows[i].Cells[5].Value.ToString()) - Convert.ToInt16(dg_productos.Rows[i].Cells[3].Value.ToString())) + " where id_arti = " + dg_productos.Rows[i].Cells[1].Value.ToString();
command.ExecuteNonQuery();
DataRow[] fila_arti = ds_factura.Tables[1].Select("clave = " + dg_productos.Rows[i].Cells[0].Value.ToString());
fila_arti[0]["existencia"] = Convert.ToInt16(dg_productos.Rows[i].Cells[5].Value.ToString()) - (Convert.ToInt16(dg_productos.Rows[i].Cells[3].Value.ToString()));
fila_arti[0]["fv_ultima"] = fecha;
datad_facmov.SelectCommand.Transaction = transaction;
SqlCommandBuilder modifica = new SqlCommandBuilder(datad_facmov);
ds_factura.AcceptChanges();
datad_facmov.Update(ds_factura);// aqui al momento del update me da el error
}
Todas las respuestas
-
No creo que el problema tenga nada que ver con la transacción. Más bien lo que parece es que el Update no encuentra por su nombre la tabla que tiene que actualizar, ya que has omitido el parámetro que contiene el nombre de la tabla:
datad_facmov.Update(ds_factura, "factura_mov");
Alternativamente, puedes pasarle al Update un DataTable en lugar del DataSet, en cuyo caso no se necesita el nombre de tabla:
datad_facmov.Update(ds_factura.Tables[0]); //O la que quieras en lugar de la cero
Por cierto, un fallo en tu código: El "Update" no grabará nada (una vez que resuelvas el error de falta del nombre de tabla), porque justo antes llamas a AcceptChanges, que lo que hace es marcar todos los cambios del dataset como "ya grabados", por lo que el Update encontrará que no tiene nada que grabar. El AcceptChanges se debe poner después del Update, no antes.