We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to Connect DropDownList to SQL DataSource?

Thread ID:

Created:

Updated:

Platform:

Replies:

127440 Nov 16,2016 04:02 AM UTC Nov 17,2016 11:55 AM UTC ASP.NET Core 1
loading
Tags: DropDownList
Sid Childers
Asked On November 16, 2016 04:02 AM UTC

I'm using a database first approach and have a lookup table that needs to be used to populate a DropDownList in my ASP.NET Core application.  I could not find documentation or code samples that shows how to wire up the model's entity class to the DropDownList.  Is this feature available for ASP.NET Core?  Or, can I mix in the ASP.NET MVC DropdownList which has viable documentation?  

Thanks for your help and guidance.

Prince Oliver [Syncfusion]
Replied On November 17, 2016 11:55 AM UTC

Hi Sid,   
  
Thanks for contacting Syncfusion support.   
  
Query 1: How to Connect DropDownList to SQL DataSource? Is this feature available for ASP.NET Core?  Or, can I mix in the ASP.NET MVC DropdownList which has viable documentation?  
Yes, it is available in ASP.NET Core. There is no need to use MVC Dropdownlist in Core Project as Core Dropdownlist supports all the features available MVC Dropdownlist.   
  
  
In order to connect a SQL data source to dropdownlist, you have to create a list and add the required data from the data table to the list using SQL command and return the list to the view page. Have a look at the below code snippet.     
  
Controller page   
List<Orders> data = new List<Orders>();   
        public IActionResult Index()   
        {   
            string RootPath = _hostingEnvironment.ContentRootPath;   
            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + RootPath +"\\Data\\NORTHWND.MDF;Integrated Security=True;Connect Timeout=30");   
            using (con)   
            {   
               con.Open();   
                SqlDataReader myReader = null;   
                SqlCommand myCommand = new SqlCommand("select * From [Orders]",   
                                                         con);   
                myReader = myCommand.ExecuteReader();   
                while  (myReader.Read())   
                {   
   
                    data.Add(new Orders(myReader["ShipName"].ToString()));   
   
                }   
                con.Close();   
                ViewBag.datasource = data;   
   
            }   
            return View();   
  
  
In the view page, bind ViewBag list to the dropdownlist’s datasource API. Have a look at the following code snippet.   
  
View Page   
<ej-drop-down-list id="bikeList"  datasource="(IEnumerable<Orders>)ViewBag.datasource" watermark-text="Select ShipName"width="100%">   
            <e-drop-down-list-fields  text="ShipName"  />   
        </ej-drop-down-list>   
  
  
We have included a sample for your convenience. Please refer to the Sample link.   
  
Regards,   
Prince 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;