Combo box unable load data from database

  1. #1

    Combo box unable load data from database

    i have problem with Ext net combo box. data table from database can not view in combo box

    code from menu.aspx
        <ext:Store ID="Store1" runat="server">
            <Reader>
             <ext:ArrayReader >
               <Fields>
                 <ext:RecordField Name="id" />
                 <ext:RecordField Name="namap" />
               </Fields>       
             </ext:ArrayReader>
            </Reader>
        </ext:Store>  
    
        <ext:ComboBox ID="cmbPenerbit" runat="server" StoreID="Store1" Width="250" DisplayField="namap" ValueField="id"></ext:ComboBox>
    C# Code behind
     string connStr = ConfigurationManager.ConnectionStrings["konekdb"].ToString();
     SqlConnection conn = new SqlConnection(connStr) ;
     conn.Open();
    
     SqlCommand cmd = new SqlCommand("SELECT id,namap FROM penerbit", conn);
     SqlDataReader rd = cmd.ExecuteReader();
     DataTable dtTab = new DataTable();
     dtTab.Load(rd);
     this.Store1.DataSource = dtTab;
     this.Store1.DataBind();
    please help me...
    Last edited by fabricio.murta; Apr 19, 2016 at 2:24 AM. Reason: Wrap [code] tags around code.
  2. #2
    Hello @indrabayu! Welcome to Ext.NET forums!

    The way you are trying to load your data does not seem really right, I believe you should be using a model in the store instead of the uses of RecordField on the reader.

    But you provided only parts of the sample, so we can't just reproduce it by our side here.

    Give these examples a look, they might help you figure out this issue:
    - Example using ArrayReader (to read from an static array): Kitchen Sink - Form - Register - the 'state' field uses it.
    - Example using DataTable type (it binds the data to a grid, but uses the same store concept than the combo box): GridPanel - Sytem.Data - DataTable
    - Data pulled from a SQL Database: GridPanel - DataSrouce Control - SQL DataSource

    If all these do not help, can you come up with a runnable and simple sample (maybe based on some of those examples) that we could run on our side.

    If in doubt on how to come up with a simple runnable example, please refer to these posts:
    - Forum Guidelines For Posting New Topics
    - More information required

    Additionally, when you write code in posts, could you wrap them in [code ] [/code ] tags? It makes it look clearer to read, indented and also provides a convenient "copy to clipboard" button.

    Hope this helps!
    Fabrício Murta
    Developer & Support Expert
  3. #3
    thanks u fabricio murta , i'ts work fine now
  4. #4
    Hello @indrabayu!

    Glad to hear the examples helped you write a working code!

    We'd love to hear back from you what was the solution that you adopted on the code above to have it working. Could be very useful for other people having the same issue.

    Notice your first post now, we'll add the code blocks around the code you pasted so it looks better on the forums!
    Fabrício Murta
    Developer & Support Expert
  5. #5
    after change code
    
    first load like this
    menu.aspx
    
    <asp:SqlDataSource ID="SqlData" runat="server" ConnectionString="<%$ ConnectionStrings:konekdb %>" SelectCommand="SELECT [id],[namap] FROM penerbit"></asp:SqlDataSource>   
    
    <ext:ComboBox ID="cmbPenerbit" runat="server" Width="250" DisplayField="namap" ValueField="id" SelectedIndex="0">
                     <Store>
                      <ext:Store runat="server" DataSourceID="SqlData">
                       <Reader>
                       <ext:JsonReader IDProperty="id">
                        <Fields>
                         <ext:RecordField Name="id"></ext:RecordField>
                         <ext:RecordField Name="namap"></ext:RecordField>
                        </Fields>
                       </ext:JsonReader>
                       </Reader>
                      </ext:Store>
                     </Store>
                    </ext:ComboBox>   
    
    
    i write  code behind when insert new data with command button
    C# code behind
    
     this.cmbPenerbit.Clear();
                string connStr = ConfigurationManager.ConnectionStrings["konekdb"].ToString();
                SqlConnection conn = new SqlConnection(connStr) ;
                conn.Open();
                SqlCommand cmd = new SqlCommand("SELECT namap,id FROM penerbit", conn);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read()) 
                  {
                      cmbPenerbit.Items.Add(new Ext.Net.ListItem(rd["id"].ToString(),rd["namap"].ToString()));
                  }
                cmbPenerbit.GetStore().DataBind();
                cmbPenerbit.SelectedIndex = 0;
               conn.Close(); conn.Dispose(); cmd.Dispose();
    Last edited by indrabayu; Apr 19, 2016 at 5:53 AM.

Similar Threads

  1. Replies: 2
    Last Post: Nov 08, 2013, 2:14 PM
  2. Replies: 5
    Last Post: Nov 12, 2012, 7:39 AM
  3. Replies: 1
    Last Post: May 16, 2010, 8:30 PM
  4. Replies: 1
    Last Post: Mar 24, 2010, 1:44 PM
  5. [CLOSED] Load data from the database in the grid.
    By flaviodamaia in forum 1.x Help
    Replies: 2
    Last Post: Oct 23, 2008, 9:00 AM

Posting Permissions