Load a Combobox from a consumed WebService list

0

Good I would like you to help me I'm doing is to consume a webservice that I return a list and those values add it in a combobox

This is my Webservice

public List<Combos> D_LlenarCombo(Int32 Opt, Int32 Id_Tipo)
        {
            SqlDataReader reader;

            SqlConnection cn = new SqlConnection(ObtenerCadenaConexion());
            cn.Open();
            SqlCommand cmd = new SqlCommand("llenarcombos", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@opt", SqlDbType.Int).Value = Opt;
            cmd.Parameters.Add("@Id_Tipo", SqlDbType.Int).Value = Id_Tipo;

            reader= cmd.ExecuteReader();
            List<Combos> Combo = new List<Combos>();
            try
            {
                while (reader.Read())
                {
                    Combo.Add(new Combos { Id = reader.GetInt32(0), Nombre = reader.GetString(1) });
                }
                reader.Close();
                cmd.Dispose();
                cn.Close();
                return Combo;
            }
            catch (Exception Ex)
            {
                throw new Exception(Ex.Message);
            }

        }

This is the Combo class.

 public class Combos
    {
        public Int32 Id { get; set; }
        public String Nombre { get; set; }
    }

This is the web page that will consume the webservice.

void LLenarCombo()
        {

         //Cargar al combobox ViaIncidencia
                var varViaIncidencia = Obj.Ws_LlenarCombo(4, 0).ToList();
     CboViaIncidencia.DataSource = varViaIncidencia;
                CboViaIncidencia.DataTextField = "ViaIncidencia";
                CboViaIncidencia.DataValueField = "Id_ViaIncidencia";
                CboViaIncidencia.DataBind();
                CboViaIncidencia.SelectedIndex = -1;

}

If you could help me please

    
asked by PieroDev 17.10.2017 в 04:43
source

1 answer

1

The problem is that you are trying to select the field As_Via_ID and as text the ViaIncidence, but that object does not have these properties.

I could like this:

void LLenarCombo()
        {

         //Cargar al combobox ViaIncidencia
                var varViaIncidencia = Obj.Ws_LlenarCombo(4, 0).ToList();
     CboViaIncidencia.DataSource = varViaIncidencia;
                CboViaIncidencia.DataTextField = "Nombre";
                CboViaIncidencia.DataValueField = "Id";
                CboViaIncidencia.DataBind();
                CboViaIncidencia.SelectedIndex = -1;

}
    
answered by 17.10.2017 / 16:27
source