I have a problem updating the fields in my table. Within the form I have a comboBox
where I show the fields of my table of type Varchar
but when selecting one of them I save the id of that department. My problem is generated when updating, showing the following error:
"ErroSystem.SqlClient.SqlException (0x80131904): Conversion failed when converting the varchar value 'Purchases' to data type int".
To modify my record, first search using a SEARCH button where you read the person's control number. With the following method.
public void buscarNumeroControl()
{
// string cod = textBox1.Text;
try
{
string cadena = "SELECT nombre, apellidoP, depto FROM Personal INNER JOIN Departamento ON Personal.id_Depto=Departamento.id_Depto WHERE id_numControl='" + textBox1.Text + "' ";
SqlCommand cmd = new SqlCommand(cadena, con);
con.Open();
SqlDataReader registro = cmd.ExecuteReader();
if (registro.Read() == true)
{
textBox2.Text = registro["nombre"].ToString();
textBox3.Text = registro["apellidoP"].ToString();
comboDepto.Text = registro["depto"].ToString();
actualizar.Enabled = true;
}
else
{
MessageBox.Show("No existe un artículo con el código ingresado");
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex.ToString());
}
}
The comboBox filled it in the following way.
public void llenarcomboboxDepto()
{
try{
con.Open();
DataSet dsd = new DataSet();
SqlDataAdapter cmd = new SqlDataAdapter("SELECT id_Depto, depto FROM Departamento", con);
//se indica con que tabla se llena
cmd.Fill(dsd, "Departamento");
comboDepto.DataSource = dsd.Tables[0].DefaultView;
//indicamos el valor de los miembros
comboDepto.ValueMember = "id_Depto";
//se indica el valor a desplegar en el combobox
comboDepto.DisplayMember = "depto";
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex.ToString());
}
}
and to modify I have the UPDATE button invoking the following method.
public void modificarRegistro()
{
try
{
con.Open();
string cadena = "UPDATE Personal set nombre='" + textBox2.Text + "', apellidoP='" + textBox3.Text + "' , id_Depto='" + comboDepto.Text + "' WHERE id_numControl='" + textBox1.Text + "'";
SqlCommand comando = new SqlCommand(cadena, con);
int cant;
cant = comando.ExecuteNonQuery();
if (cant == 1)
{
MessageBox.Show("Se modificaron los datos correctamente");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
comboDepto.Text = "";
con.Close();
}
else
{
MessageBox.Show("No existe la persona con ese numero de control");
actualizar.Enabled = false;
}
// }
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex.ToString());
}
}
I do not know where this is wrong, I feel like I'm not recovering the id of the department I'm selected for. Could someone please help me?