简体   繁体   中英

Binding dropdownlist with generic list in 3-tier architecture

/*data access layer */
public DataSet getdata(string procedurename, SqlParameter[] param)
 {
     try
     {
          SqlCommand command;
          command = new SqlCommand(procedurename, connection);
          command.CommandType = CommandType.StoredProcedure;
          SqlDataAdapter adapter = new SqlDataAdapter(command);
          DataSet set = new DataSet();
          if (param != null)
          {
             for (int i = 0; i < param.Length; i++)
             {
                 command.Parameters.Add(param[i]);
             }
          }
           adapter.Fill(set);
           return set;
       }
       catch (Exception ex)
       {
           throw ex;
       }
       finally
       {
           closeConnection();
       }
    }

Middle Tier:

public class dropdownbind
    {
        private string _itemName;
        private int _itemvalue;

        public int Itemvalue
        {
            get { return _itemvalue; }
            set { _itemvalue = value; }
        }

        public string ItemName
        {
            get { return _itemName; }
            set { _itemName = value; }
        }

        public List<dropdownbind> getDepartment()
        {
            DBlist obj = new DBlist();
            DataSet ds = obj.getdata("str_getdepartment",null);
            List<dropdownbind> departments = new List<dropdownbind>();
            foreach (DataRow orow in ds.Tables[0].Rows)
            {
                dropdownbind dlist = new dropdownbind();
                dlist.ItemName = orow["deparment_name"].ToString();
                dlist.Itemvalue = int.Parse(orow["id"].ToString());
                departments.Add(dlist);
            }
            return departments;
        }
    }

UI:-

 protected void BindDdlList()
    {
        dropdownbind dlist = new dropdownbind();
        List<dropdownbind> departments = dlist.getDepartment();
        ddlEmpDepatment.DataSource = departments;
        ddlEmpDepatment.DataTextField = dlist.ItemName;
        ddlEmpDepatment.DataValueField = dlist.Itemvalue.ToString();
        ddlEmpDepatment.DataBind();

    }

i am trying to bind departments to dropdownlist using 3-tier architecture. but this code is not working, it shows middletier.dropdownbind in text field.

You need to correct the DataTextField & DataValueField properties like this:-

ddlEmpDepatment.DataValueField = "Itemvalue";
ddlEmpDepatment.DataTextField = "ItemName";

You are specifying the property name rather you need to specify the property name as string .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM