Oscar Perez

Oscar Perez

  • NA
  • 20
  • 506

How to transfer data from a Class to an SQL Serer Table?

Nov 1 2019 10:32 AM
Im buildin an app for creating registers in an sql table, i have to select the data from datagriviews and put it on another datagridview, then i took the from the final datagriview to a class and im trying to send the data from the class to a SQL Server table, the code doens not show me any errors but i can not see the new row on the table
  1. private void button1_Click_1(object sender, EventArgs e) {  
  2.  int rowindex3 = dataGridView3.CurrentCell.RowIndex;  
  3.   
  4.  Class1 d1 = new Class1();  
  5.  d1.CodigoEmpresa = dataGridView3.Rows[rowindex3].Cells[0].FormattedValue.ToString();  
  6.  d1.Inventario = dataGridView3.Rows[rowindex3].Cells[1].FormattedValue.ToString();  
  7.  d1.CodigoArticulo = dataGridView3.Rows[rowindex3].Cells[2].FormattedValue.ToString();  
  8.  d1.Partida = dataGridView3.Rows[rowindex3].Cells[3].FormattedValue.ToString();  
  9.  d1.CodigoAlmacen = dataGridView3.Rows[rowindex3].Cells[4].FormattedValue.ToString();  
  10.  d1.TipoUnidadMedida_ = dataGridView3.Rows[rowindex3].Cells[5].FormattedValue.ToString();  
  11.  d1.UnidadesStock = dataGridView3.Rows[rowindex3].Cells[6].FormattedValue.ToString();  
  12.  d1.UnidadesStock1_ = dataGridView3.Rows[rowindex3].Cells[7].FormattedValue.ToString();  
  13.  d1.PrecioMedio = dataGridView3.Rows[rowindex3].Cells[8].FormattedValue.ToString();  
  14.  d1.FechaCreacion = dataGridView3.Rows[rowindex3].Cells[9].FormattedValue.ToString();  
  15.  d1.PrecioNuevo = dataGridView3.Rows[rowindex3].Cells[10].FormattedValue.ToString();  
  16.  d1.FechaCaducidad = dataGridView3.Rows[rowindex3].Cells[11].FormattedValue.ToString();  
  17.  d1.PrecioNuevo1_ = dataGridView3.Rows[rowindex3].Cells[12].FormattedValue.ToString();  
  18.  d1.PrecioMedio1_ = dataGridView3.Rows[rowindex3].Cells[13].FormattedValue.ToString();  
  19.  d1.DescripcionArticulo = dataGridView3.Rows[rowindex3].Cells[14].FormattedValue.ToString();  
  20.  d1.Fecha = dataGridView3.Rows[rowindex3].Cells[15].FormattedValue.ToString();  
  21.  try {  
  22.   
  23.   using(SqlConnection openCon = new SqlConnection("Data Source=OscarJCPL;Initial Catalog=Zthotel;Integrated Security=True")) {  
  24.    string saveStaff = "INSERT into Inventarios (CodigoEmpresa,Inventario,CodigoArticulo,Partida,CodigoAlmacen,TipoUnidadMedida_,UnidadesStock,UnidadesStock1_,PrecioMedio,FechaCreacion,PrecioNuevo,FechaCaducidad,PrecioNuevo1_,PrecioMedio1_,DescripcionArticulo,Fecha) VALUES (@CodigoEmpresa,@Inventario,@CodigoArticulo,@Partida,@CodigoAlmacen,@TipoUnidadMedida_,@UnidadesStock,@UnidadesStock1_,@PrecioMedio,@FechaCreacion,@PrecioNuevo,@FechaCaducidad,@PrecioNuevo1_,@PrecioMedio1_,@DescripcionArticulo,@Fecha)";  
  25.   
  26.    using(SqlCommand querySaveStaff = new SqlCommand(saveStaff)) {  
  27.     querySaveStaff.Connection = openCon;  
  28.     querySaveStaff.Parameters.Add("@CodigoEmpresa", SqlDbType.SmallInt, 30).Value = CodigoEmpresa;  
  29.     querySaveStaff.Parameters.Add("@Inventario", SqlDbType.VarChar, 30).Value = Inventario;  
  30.     querySaveStaff.Parameters.Add("@CodigoArticulo", SqlDbType.VarChar, 30).Value = CodigoArticulo;  
  31.     querySaveStaff.Parameters.Add("@Partida", SqlDbType.VarChar, 30).Value = Partida;  
  32.     querySaveStaff.Parameters.Add("@CodigoAlmacen", SqlDbType.VarChar, 30).Value = CodigoAlmacen;  
  33.     querySaveStaff.Parameters.Add("@TipoUnidadMedida_", SqlDbType.VarChar, 30).Value = TipoUnidadMedida_;  
  34.     querySaveStaff.Parameters.Add("@UnidadesStock", SqlDbType.Decimal, 30).Value = UnidadesStock;  
  35.     querySaveStaff.Parameters.Add("@UnidadesStock1_", SqlDbType.Decimal, 30).Value = UnidadesStock1_;  
  36.     querySaveStaff.Parameters.Add("@PrecioMedio", SqlDbType.Decimal, 30).Value = PrecioMedio;  
  37.     querySaveStaff.Parameters.Add("@FechaCreacion", SqlDbType.DateTime, 30).Value = FechaCreacion;  
  38.     querySaveStaff.Parameters.Add("@PrecioNuevo", SqlDbType.Decimal, 30).Value = PrecioNuevo;  
  39.     querySaveStaff.Parameters.Add("@FechaCaducidad", SqlDbType.DateTime, 30).Value = FechaCaducidad;  
  40.     querySaveStaff.Parameters.Add("@PrecioNuevo1_", SqlDbType.Decimal, 30).Value = PrecioNuevo1_;  
  41.     querySaveStaff.Parameters.Add("@PrecioMedio1_", SqlDbType.Decimal, 30).Value = PrecioMedio1_;  
  42.     querySaveStaff.Parameters.Add("@DescripcionArticulo", SqlDbType.VarChar, 30).Value = DescripcionArticulo;  
  43.     querySaveStaff.Parameters.Add("@Fecha", SqlDbType.DateTime, 30).Value = Fecha;  
  44.     openCon.Open();  
  45.    }  
  46.   }  
  47.   MessageBox.Show("Registro Correcto");  
  48.  } catch {  
  49.   MessageBox.Show("Recuerde Seleccionar el Registro");  
  50.  }  
  51. }

Answers (2)