[CLOSED] Loading data from database to a combobox in Ext.Net

  1. #1

    [CLOSED] Loading data from database to a combobox in Ext.Net

    I tried to load the data from the database to the combo box. The combo box had to load the name of the cities from the database but the function
    "GetCities" was not called from the index file.


    here is the sample code

    @(Html.X().ComboBox()
            .ID("ComboBoxCity")
            .TypeAhead(true)
            .QueryMode(DataLoadMode.Local)
            .ForceSelection(true)
            .TriggerAction(TriggerAction.All)
            .DisplayField("name")
            .ValueField("id")
            .EmptyText("Loading...")
            .ValueNotFoundText("Loading...")
            .Store(Html.X().Store()
                .AutoLoad(false)
                .Model(Html.X().Model()
                    .IDProperty("Id")
                    .Fields(
                                new ModelField("id", ModelFieldType.String) { Mapping = "Id" },
                        new ModelField("name", ModelFieldType.String) { Mapping = "Name" }
                    )
                )
                .Proxy(Html.X().AjaxProxy()
                    .Url(Url.Action("GetCities"))
                    .Reader(Html.X().JsonReader().Root("data"))
                )
                .Parameters(ps =>
                    ps.Add(new StoreParameter("country", "App.ComboBoxCountry.getValue()", ParameterMode.Raw))
                )
                .Listeners(ls =>
                    ls.Load.Handler = @"var combo = App.ComboBoxCity;
                                        combo.setValue(records[0].get(combo.valueField));"
                )
            )
        )
    function at controller

    public ActionResult GetCities(string country)
            {
                return this.Store(City.GetCities(country));
               
            }
    
    code at city class in Model
     public static IEnumerable GetCities(string country)
            {
    
                List<object> data = new List<object>();
                string conString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    
                SqlConnection sqlCon = new SqlConnection(conString);
                SqlCommand sqlCmd = new SqlCommand("select Name from tblEmployee", sqlCon);
                sqlCon.Open();
                SqlDataReader dataReader = sqlCmd.ExecuteReader();
                Employee ee = new Employee();
                foreach (var v in dataReader)
                {
                    ee.Name = v.ToString();
                    data.Add(ee);
                }
    
    
    
                return data;
               
            }
    Last edited by Daniil; Sep 03, 2013 at 5:05 AM. Reason: Please use [CODE] tags, [CLOSED]
  2. #2
    Hi @pawangyanwali,

    Please read #3:
    http://forums.ext.net/showthread.php?10205
  3. #3
    The solution you provided din't fulfil my requirement. The scenario is;
    The combo box is loaded in the login form. The form that loads in the beginning of the application.
    Whenever the application is loaded, the data should be loaded in the combo box.
    But the problem is, the ajax proxy is not working. The action is not being called which in the controller when the form is loaded.

    View code;
    @model System.Collections.IEnumerable
    @using comboBoxxx.Content;

    @{
    ViewBag.Title = "Index";
    }

    @section section1{
     @(Html.X().ComboBox()
            .ID("ComboBoxCity")
            .TypeAhead(true)
            .QueryMode(DataLoadMode.Local)
            .ForceSelection(true)
            .TriggerAction(TriggerAction.All)
            .DisplayField("name")
            .ValueField("id")
            .EmptyText("Loading...")
            .ValueNotFoundText("Loading...")
            .Store(Html.X().Store()
                .AutoLoad(false)
                .Model(Html.X().Model()
                    .IDProperty("Id")
                    .Fields(
                                new ModelField("id", ModelFieldType.String) { Mapping = "Id" },
                        new ModelField("name", ModelFieldType.String) { Mapping = "Name" }
                    )
                )
                .Proxy(Html.X().AjaxProxy()
                    .Url(Url.Action("GetCities"))
                    
                    .Reader(Html.X().JsonReader().Root("data"))
                )
                .Parameters(ps =>
                    ps.Add(new StoreParameter("country", "App.ComboBoxCountry.getValue()", ParameterMode.Raw))
                )
                .Listeners(ls =>
                    ls.Load.Handler = @"var combo = App.ComboBoxCity;
                                        combo.setValue(records[0].get(combo.valueField));"
                )
            )
        )
    }
    Backend code;
      public ActionResult GetCities(string country)
            {
                return this.Store(City.GetCities(country));
               
            }
    Functions that collects the data from the database;
     public static IEnumerable GetCities(string country)
            {
                List<object> data = new List<object>();
                string conString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    
                SqlConnection sqlCon = new SqlConnection(conString);
                SqlCommand sqlCmd = new SqlCommand("select Name from tblEmployee", sqlCon);
                sqlCon.Open();
                SqlDataReader dataReader = sqlCmd.ExecuteReader();
                Employee ee = new Employee();
                foreach (var v in dataReader)
                {
                    ee.Name = v.ToString();
                    data.Add(ee);
                }
    
    
    
                return data;
            }
  4. #4
    Quote Originally Posted by pawangyanwali View Post
    The solution you provided din't fulfil my requirement.
    Well, I did not provide any solution. I just asked you to read something. Please confirm you did read. Seems, you didn't, because you didn't edit the initial post wrapping the code in [CODE] tags.

    Quote Originally Posted by pawangyanwali View Post
    The scenario is;
    The combo box is loaded in the login form. The form that loads in the beginning of the application.
    Whenever the application is loaded, the data should be loaded in the combo box.
    But the problem is, the ajax proxy is not working. The action is not being called which in the controller when the form is loaded.
    I think you should not set up this
    .AutoLoad(false)
    if you expect the Store loads on page load.
  5. #5
    The solution that you provided didn't solve the problem. I removed the AutoLoad(false) but still the combo box was not able to load the data from database. Here i have presented the screen-shot of the application.
    Attached Thumbnails Click image for larger version. 

Name:	login.png 
Views:	17 
Size:	5.3 KB 
ID:	6820  
  6. #6
    Please post a response of the GetCities action call.

Similar Threads

  1. [CLOSED] MVC DirectEvent/Method ComboBox - Loading Data
    By adelaney in forum 2.x Legacy Premium Help
    Replies: 5
    Last Post: Sep 28, 2012, 3:11 AM
  2. Error in Fetch Data from Database
    By Vaishali in forum 1.x Help
    Replies: 4
    Last Post: Jan 08, 2012, 3:52 AM
  3. Replies: 2
    Last Post: Dec 19, 2011, 1:54 AM
  4. retrieve data from database using jsf
    By Rukia in forum 1.x Help
    Replies: 0
    Last Post: Jun 02, 2011, 10:38 PM
  5. [CLOSED] Combobox not loading data
    By tdracz in forum 1.x Legacy Premium Help
    Replies: 4
    Last Post: Sep 22, 2009, 6:45 AM

Posting Permissions