Asp.Net Drop Down List

1

How do I pass%% by parameter? I tried to pass, for example:

public void preencheDrop(string SQL, WebControl x)
{
    método aqui. 
}

But I could not. This method I just want to make it easier for me to fill up more than a dropdownlist and not have to spend all the time copying code.

    
asked by anonymous 01.07.2015 / 15:04

1 answer

2

See if that really caters to you. It is only you to make the necessary changes.

public bool preencheTipo_Usuario(DropDownList dl)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("select ");
            sb.AppendLine("codtipo, descricao_tipo");
            sb.AppendLine("from tbl_tipo_usuario ");

            SqlConnection conexao = new SqlConnection();
            conexao.ConnectionString = ConfigurationManager.ConnectionStrings["conectDarf"].ConnectionString;
            this.cmd = new SqlCommand(sb.ToString(), conexao);

            try
            {
                conexao.Open();
                cmd.ExecuteNonQuery();

                SqlDataReader dr = cmd.ExecuteReader();

                dl.DataSource = dr;
                dl.DataTextField = "descricao_tipo";
                dl.DataValueField = "codtipo";
                dl.DataBind();

                dl.Items.Insert(0, new ListItem("--- SELECIONE ---", "-1"));
            }
            catch (Exception excecao)
            {
                Erro = excecao.Message;
                return false;
            }
            finally
            {
                conexao.Close();

            }

            return true;
        }
    
01.07.2015 / 15:20