Integer to String Conversion

Asked

Viewed 486 times

0

I’m creating a cafeteria system.
I created the login system and it’s working, when I went to create a page to create the logins, I’m having difficulties in converting the int for string - the txtNivel.Text does not come out of the 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");
    }
}

Dice:

//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();
        }
    }

}
  • objCommand.Parameters.Addwithvalue("@Nivel", Nivel.Tostring());

  • Can you be a little more specific where I put this and why? I’m new to the subject

  • 1

    good ... Save(txtLogin.Text, txtSenha.Text, Int32.Parse( txtNivel.Text), txtInfAdicinais.Text);

  • https://answall.com/questions/212553/necessito-converter-uma-string-em-int-dentro-de-uma-hql

  • https://answall.com/questions/93866/converter-objeto-para-string/93870#93870

1 answer

4

You need to convert the value.

With the TryParse in addition to converting, it is possible to check if there has been success in converting as 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 previous versions it is necessary to declare the output variable outside the TryParse. Credits Marconcilio Souza

EDIT

If you want to know more about TryParse has this one great answer on the subject, it is worth reading.

  • remember to mention the C# version for this to work and the error handling if the conversion is not done.

  • Problem solved very obg

  • @Fernandorodrigues, if solved, marks the answer as correct ;)

  • @Marconciliosouza, the version I would not know answer, if I can add the answer to add value, I would be grateful.

  • https://stackoverflow.com/a/31724198/2740371

  • I give the +1 c/ the greatest pleasure, this is one of the first answers that teaches the right, even if you must have equal answers for the site and the question is duplicate.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.