private void btnSave_Click(object sender, EventArgs e) { ConnectionDB conn = new ConnectionDB(); bool isOpen = conn.Open(); if (isOpen) { string commandText = ""; if (customer_id == 0) { commandText = "INSERT INTO customer(name, last_name, address, cellphone) VALUES(@name, @last_name, @address, @cellphone)"; } else { commandText = "UPDATE customer SET name=@name, last_name=@last_name, address=@address, cellphone=@cellphone WHERE id=@id"; } SqlCommand command = new SqlCommand(commandText); command.Parameters.Add("@name", SqlDbType.NVarChar); command.Parameters["@name"].Value = txtName.Text; command.Parameters.Add("@last_name", SqlDbType.NVarChar); command.Parameters["@last_name"].Value = txtLastName.Text; command.Parameters.Add("@address", SqlDbType.NVarChar); command.Parameters["@address"].Value = txtAddress.Text; command.Parameters.Add("@cellphone", SqlDbType.NVarChar); command.Parameters["@cellphone"].Value = txtCellphone.Text; if (customer_id > 0) { command.Parameters.Add("@id", SqlDbType.Int); command.Parameters["@id"].Value = customer_id; } int result = conn.executeNonQuery(command); if (result >= 0) { if (customer_id == 0) { DialogResult dialogResult = MessageBox.Show("Cliente registrado. Desea registrar otro?", "Prueba", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { ClearForm(); } else { this.DialogResult = DialogResult.OK; this.Close(); } } else { this.DialogResult = DialogResult.OK; this.Close(); } } else { MessageBox.Show("Error al guardar el cliente", "Prueba"); } } else { MessageBox.Show(conn.errorMessage); } conn.Close(); }
private void Button1_Click(object sender, EventArgs e) { ConnectionDB conn = new ConnectionDB(); bool isOpen = conn.Open(); if (isOpen) { string commandText = ""; if (product_id == 0) { commandText = "INSERT INTO products(name, description, cost, price, status) VALUES(@name, @description, @cost, @price, @status)"; } else { commandText = "UPDATE products SET name=@name, description=@description, cost=@cost, price=@price, status=@status WHERE id=@id"; } SqlCommand command = new SqlCommand(commandText); command.Parameters.Add("@name", SqlDbType.NVarChar); command.Parameters["@name"].Value = txtNameProduct.Text; command.Parameters.Add("@description", SqlDbType.Text); command.Parameters["@description"].Value = txtDetailProduct.Text; command.Parameters.Add("@cost", SqlDbType.Float); command.Parameters["@cost"].Value = txtCost.Text; command.Parameters.Add("@price", SqlDbType.Float); command.Parameters["@price"].Value = txtPrice.Text; if (checkState.Checked) { command.Parameters.Add("@status", SqlDbType.Int); command.Parameters["@status"].Value = 1; } else { command.Parameters.Add("@status", SqlDbType.Int); command.Parameters["@status"].Value = 0; } if (product_id > 0) { command.Parameters.Add("@id", SqlDbType.Int); command.Parameters["@id"].Value = product_id; } int result = conn.executeNonQuery(command); if (result >= 0) { if (product_id == 0) { DialogResult dialogResult = MessageBox.Show("Producto registrado. Desea registrar otro?", "Prueba", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { ClearForm(); } else { this.DialogResult = DialogResult.OK; this.Close(); } } else { this.DialogResult = DialogResult.OK; this.Close(); } } else { MessageBox.Show("Error al guardar el Producto", "Prueba"); } } else { MessageBox.Show(conn.errorMessage); } conn.Close(); }