Integer to String Conversion [duplicate]

0

I'm creating a cafeteria system. I created the login system and it is working, when I was creating a page to create the logins, I am having difficulty converting the level from int to string - the txtNivel.Text does not go out of red , follow the codes:

public int Codigo = 0;

private void Gravar(string Login, string Senha, int Nivel, string InfAdicionais)
{
    try
    {
        Dados objDados = new Dados();
        objDados.Gravar(Login, Senha, Nivel, InfAdicionais);

        txtLogin.Clear();
        txtSenha.Clear();
        txtNivel.Clear();
        txtInfAdicinais.Clear();

        string menssagem = "Seus dados foram gravados com sucesso.";

        MessageBox.Show(menssagem);
    }
    catch (Exception ex)
    {
        MessageBox.Show("Ocorreu um erro: " + ex.Message);
    }
}

private void btn_Adicionar_Click(object sender, EventArgs e)
{
    {
        if(!String.IsNullOrEmpty(txtLogin.Text) &&
           !String.IsNullOrEmpty(txtSenha.Text) &&
           !String.IsNullOrEmpty(txtNivel.Text))
            Gravar(txtLogin.Text, txtSenha.Text, txtNivel.Text**, txtInfAdicinais.Text);
        else
            MessageBox.Show("Ocorreu um erro");
    }
}

Data:

//Usuarios

public class Usuarios

{
    public int IdUsuario { get; set; }
    public string Login { get; set; }
    public string Senha { get; set; }
    public int Nivel { get; set; }
    public string InfAdicionais { get; set; }
}

public void Gravar(string Login, string Senha, int Nivel, string InfAdicionais)
{
    using (SqlConnection objConexao = new SqlConnection(strConexao))
    {
        using (SqlCommand objCommand = new SqlCommand(strInsert, objConexao))
        {
            objCommand.Parameters.AddWithValue("@Login", Login);
            objCommand.Parameters.AddWithValue("@Senha", Senha);
            objCommand.Parameters.AddWithValue("@Nivel", Nivel);
            objCommand.Parameters.AddWithValue("@InfAdicionais", InfAdicionais);

            objConexao.Open();

            objCommand.ExecuteNonQuery();

            objConexao.Close();
        }
    }

}
    
asked by anonymous 27.02.2018 / 17:56

1 answer

4

You need to convert the value.

With TryParse in addition to converting, you can check if the conversion succeeded because it returns a bool .

private void btn_Adicionar_Click(object sender, EventArgs e)
{

    {
        if
            (!String.IsNullOrEmpty(txtLogin.Text) &&
            !String.IsNullOrEmpty(txtSenha.Text) &&
            !String.IsNullOrEmpty(txtNivel.Text))

             if(!int.TryParse(txtNivel.Text, out int nivel))
             {
                  MessageBox.Show("o valor do nível não é númerico");
                  return;
             }

            Gravar(txtLogin.Text, txtSenha.Text, nivel, txtInfAdicinais.Text);

        else
        {
            MessageBox.Show("Ocorreu um erro");
        }
    }
}

EDIT

This feature is available from C # 7, in earlier versions it is necessary to declare the output variable out of TryParse . Credits Marconcilio Souza

EDIT

If you want to know more about TryParse you have this great answer on the subject, it's worth reading.

    
27.02.2018 / 18:07