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. Image for the cookie policy date

How to Connect DropDownList to SQL DataSource?

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.

1 Reply

PO Prince Oliver Syncfusion Team 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 


Loader.
Up arrow icon