I am Configuration A Search button of my Client Form and I have a Problems with a Variable

1

Hi Guys, I'm a Rookie in this and I'm doing a Small Final Degree Project on a Medical Office.

private void btnbuscar_Click_1(object sender, EventArgs e)
    {
        BuscarUsuario bc = new BuscarUsuario();
        bc.Show();
        if (bc.ClienteSelecionado != null)
        {
            txtnombre.Text = bc.ClienteSelecionado.nombre;
            txtapellido.Text = bc.ClienteSelecionado.apellido;
            txtiduser.Text = bc.ClienteSelecionado.user;
            txtpass.Text = bc.ClienteSelecionado.contraseña;
            txttel.Text = bc.ClienteSelecionado.tel;
            txtpais.Text = bc.ClienteSelecionado.pais;
            txtsexo.Text = bc.ClienteSelecionado.sexo;
            txtcedula.Text = bc.ClienteSelecionado.cedula;
            txtedad.Text = Ibc.ClienteSelecionado.edad;
            txtdir.Text = bc.ClienteSelecionado.dir;

This is the code in the chain txtedad I get an error saying that you can not convert an int in String iplicitamente, I need to know what I have to put, I hope, that you can help me, any help would greatly appreciate it

    
asked by Casper I Ortega Melendez 18.12.2017 в 22:08
source

3 answers

2

Explicitly you can convert an integer to a string, for example:

txtedad.Text = bc.ClienteSelecionado.edad.ToString();

but if you do not have a value there it will generate another error. Check if all of the SearchUser properties return value to you,

    
answered by 18.12.2017 в 22:12
0

You can convert it like this (Both work):

txtedad.Text = bc.ClienteSelecionado.edad.ToString();

txtedad.Text = Convert.ToString(bc.ClienteSelecionado.edad);
  

You can also check the documentation of the Convert Class , so you can look at all the types of data you can convert.

    
answered by 23.12.2017 в 14:18
0

The Text property of the Text boxes receives String, so you must explicitly convert the int to String so that you can place it in the Text property.

Alternative 1:

txtedad.Text = Ibc.ClienteSelecionado.edad.ToString(); 

ToString (), converts Ibc.ClientSelecionado.dad to its equivalent in string. This can generate an exception of type NullReferenceException if the value of age is null

Alternative 2:

txtedad.Text = Convert.ToString(Ibc.ClienteSelecionado.edad); 

If the value of Ibc.ClienteSelecionado.dad is null, it will not generate an error but it will show null.

Alternative 3:

txtedad.Text = (String)Ibc.ClienteSelecionado.edad;  

(String) here we apply a cast in runtime.

    
answered by 11.10.2018 в 22:58