Формуляр ОБНОВЛЯТЬ
namespace Fujisto_proyect{
public partial class Actualizar : Form
{
// MySqlConnection conec = new MySqlConnection("server=127.0.0.1; database=fujitsu; Uid=root; pwd=;");
// BdComun c=new BdComun();
Conexion c = new Conexion();
public Actualizar()
{
InitializeComponent();
}
private void txtCodigo_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
if (c.PersonaRegistrada(txtCodigo.Text) > 0)
{
c.llenarConsulta(txtCodigo.Text, cbxNumFamilia, txtNumParte, txtCantCaja, txtCantPallet, txtNumEmpleado, txtuAlmacen, dtpHora, dtpFecha);
}
}
}
private void btnActualizar_Click(object sender, EventArgs e)
{
DialogResult resul = MessageBox.Show("Seguro que quiere actualizar el Registro?", "Eliminar Registro", MessageBoxButtons.YesNo);
if (resul == DialogResult.Yes)
{
}
}
private void btnSalir_Click(object sender, EventArgs e)
{
this.Close();
}
}
КЛАСС СВЯЗЬ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
namespace Fujisto_proyect
{
public class Conexion
{
MySqlConnection cn;
MySqlCommand cmd;
MySqlDataReader dr;
public Conexion()
{
try
{
cn = new MySqlConnection("server=127.0.0.1; database=fujitsu; Uid=root; pwd=;");
cn.Open();
MessageBox.Show("conectado");
}
catch (Exception ex)
{
MessageBox.Show("No se conectó a la base de datos :" + ex.ToString());
}
}
public string Insertar(string NumQr, string NumFamilia, string NumParte, int CantCaj, int CantPallet, string NumEmpleado, string fecha, string hora, string uAlmacen)
{
string salida = "Se inserto correctamente";
try
{
cmd = new MySqlCommand("insert into producto (NUmQr,NumFamilia,NumParte,CantCaj,CantPallet,NumEmpleado,fecha,hora,uAlmacen) values('" + NumQr + "','" + NumFamilia + "','" + NumParte + "'," + CantCaj + "," + CantPallet + ",'" + NumEmpleado + "','" + fecha + "','" + hora + "','" + uAlmacen + "')", cn);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
salida = "No se conecto " + ex.ToString();
}
return salida;
}
public int PersonaRegistrada(string NumQr)
{
int contador = 0;
try
{
cmd = new MySqlCommand(" select * from producto where NumQr='" + NumQr + "'", cn);
dr = cmd.ExecuteReader();
while (dr.Read())
{
contador++;
}
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show("No se pudo consultar bien :" + ex.ToString());
}
return contador;
}
public void llenarConsulta(string NumQr, ComboBox cbxNumFamilia, ComboBox cbxNumParte, TextBox txtCantCaja, TextBox txtCantPallet, TextBox txtNumEmpleado, ComboBox cbxuAlmacen, DateTimePicker dtpHora, DateTimePicker dtpFecha)
{
try {
cmd = new MySqlCommand("select * from producto where NumQr="+NumQr+"",cn);
dr = cmd.ExecuteReader();
if(dr.Read()){
cbxNumFamilia.Text = dr["NumFamilia"].ToString();
cbxNumParte.Text = dr["NumParte"].ToString();
txtCantCaja.Text = dr["CantCaj"].ToString();
txtCantPallet.Text = dr["CantPallet"].ToString();
txtNumEmpleado.Text = dr["NumEmpleado"].ToString();
cbxuAlmacen.Text = dr["uAlmacen"].ToString();
dtpHora.Text = dr["hora"].ToString();
dtpFecha.Text = dr["fecha"].ToString();
}
dr.Close();
}
catch(Exception ex) {
MessageBox.Show("No se pudo llenar los campos: "+ex.ToString());
}
}
}
Код, который ты используешь, не правилен, ты был бы должен trabjar с организациями и параметрами. Для этого ты определяешь организацию
public class Producto
{
public string NumFamilia{get;set;}
public string NumParte{get;set;}
public string CantCaj{get;set;}
//otras propeidades
public DateTime Fecha{get;set;}
}
Тогда в твоем коде загружаешь организацию ты происходите инстанция
public Producto llenarConsulta(string NumQr)
{
Producto prod = null;
try
{
MySqlCommand cmd = new MySqlCommand("select * from producto where NumQr = ?NumQr",cn);
cmd.Parameters.AddWithValue("?NumQr", NumQr);
MySqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())
{
prod = new Producto();
prod.NumFamilia = dr["NumFamilia"].ToString();
prod.NumParte = dr["NumParte"].ToString();
//otras propiedades
prod.CantCaj = dr["CantCaj"].ToString();
prod.Fecha = Convert.ToDateTime(dr["fecha"]);
}
dr.Close();
}
catch(Exception ex) {
MessageBox.Show("No se pudo llenar los campos: "+ex.ToString());
}
return prod;
}
и ты это используешь
Conexion c = new Conexion();
Producto prod = c.llenarConsulta(txtCodigo.Text);
if(prod != null)
{
cbxNumFamilia.Text = prod.NumFamilia;
cbxNumParte.Text = prod.NumParte;
txtCantCaja.Text = prod.CantCaj;
//otros controles
dtpFecha.Value = prodi.Fecha;
}
ты не был бы должен посылать контроль как параметр в методе, который возвращает данные