Help with Textbox / string parameters

Asked

Viewed 280 times

3

I have this method where I would like to bring the IBGE. When I put Textbox as a parameter, it usually brings it, but the problem is that I am using a Library and cannot import Textbox. So I replaced Textbox with a string, but that way it can’t bring... Someone can help me?

public bool recuperarIBGE(string Cidade, string IBGE)
    {
        conexao = new Conexao();

        try
        {
            conexao.conectar();
            conexao.abrirConexao();

            NpgsqlCommand sql = new NpgsqlCommand("SELECT codibge FROM cidade WHERE nome ilike SEM_ACENTO('" + Cidade + "');", conexao.conexao);

            NpgsqlDataReader dr = sql.ExecuteReader();

            while (dr.Read())
            {
                //IBGE.Text = (string)dr["codibge"].ToString();
                IBGE = (string)dr["codibge"].ToString();
                retorno = true;

            }
            conexao.fecharConexao();
        }
        catch (Exception erro)
        {
            throw erro;
        }

        return retorno;
    }
  • where is your error? is in your query?

  • Actually there is no error, the only doubt is that it cannot return anything when the parameter is a string. Already when it is a Textbox, it returns normally... Some solution to this or I did something wrong?

  • I understood it is because I saw an ilike in your query and I just found out that you use the postgre bank. Try to put %SEM_ACENTO('" + City + "');%. Note that add the %

  • @Marconi, so did not... It would have to be done this way then: SEM_ACENTO('%" + City + "%');. I tested direct on Postgres and so it returns me several city codes =/

  • Got it, weird that more if it worked out all right.

  • But I can’t let him bring any. I put Jaú as an example in the city and he returned me several cities that have Jaú in the middle...

  • Join the chat there.

  • I entered, but I can’t send

Show 4 more comments

2 answers

1


To change the value in the parameter IBGE is visible in the code that called the method you must define this parameter as out.

Simply change the method signature to

public bool recuperarIBGE(string Cidade, out string IBGE)

and make sure to assign a value to all method code branchs.

When calling the method you also need to pass the modifier out in the parameter IBGE:

string ibge;

if (recuperarIBGE("Goiânia", out ibge))
{
    Console.WriteLine("O código IBGE da cidade é" + ibge);
}

Thus, after calling the method recuperarIBGE the attribute IBGE will have the value that was assigned to it within the method.

  • It worked Buddy, thank you very much! ^^

1

  • Thanks for the links friend, ^^

Browser other questions tagged

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