PDA

View Full Version : Combo box unable load data from database



indrabayu
Apr 18, 2016, 6:15 AM
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...

fabricio.murta
Apr 18, 2016, 6:15 PM
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 (http://examples4.ext.net/#/Kitchen_Sink/Forms/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 (http://examples4.ext.net/#/GridPanel/System.Data/DataTable/)
- Data pulled from a SQL Database: GridPanel - DataSrouce Control - SQL DataSource (http://examples4.ext.net/#/GridPanel/DataSource_Controls/SqlDataSource/)

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 (http://forums.ext.net/showthread.php?3440)
- More information required (http://forums.ext.net/showthread.php?10205)

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!

indrabayu
Apr 18, 2016, 11:04 PM
thanks u fabricio murta , i'ts work fine now

fabricio.murta
Apr 19, 2016, 2:23 AM
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!

indrabayu
Apr 19, 2016, 5:47 AM
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();