Click here to Skip to main content
15,887,405 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
See more:
Hi,
i want to bind the 2 columns data into a dropdownlist using entity frame work
example

C#
using (testingModel.testingEntities TE = new testingModel.testingEntities())
       {
           var query = from d in TE.tbl
                       where d.tblid == "Tbl1"
                       orderby d.seq_num
                       select new
                       {
                           d.item_desc,
                           d.seq_num
                       };


i want the two columns item_desc and seq_num into dropdown list and it should be
like sequence items of dropdown
Posted

Hi ,
This will Give you idea
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
            {
                con.Open();
                   //Change with your select statement .
                using (SqlCommand cmd = new SqlCommand("select * from test1", con))
                {
                    DataTable dt = new DataTable();
                    SqlDataAdapter adpt = new SqlDataAdapter(cmd);
                    adpt.Fill(dt);
                    Dictionary<int,> lst = new Dictionary<int,>();
                    foreach (DataRow row in dt.Rows)
                    {
                        //Add values to Dictionary
                        string val = row[1].ToString() + " , " + row[2].ToString() + " , " + row[3].ToString();
                        lst.Add(Convert.ToInt32(row[0]), val);
                    }
                    DropDownList1.DataSource = lst;
                    DropDownList1.DataTextField = "Value";
                    DropDownList1.DataValueField = "Key";
                    DropDownList1.DataBind();
                }
            } 
        }
    }


XML
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>



Best Regards
M.Mitwalli
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900