Load a combobox from oracle Database in c# WPF


Load a combobox from oracle Database in c# WPF



I´m trying to fill a combobox with Oracle Database but I'm new in WPF.
What am I missing?
C# :


private void combonacionalidad_Loaded(object sender, RoutedEventArgs e)
{
ora.Open();
OracleCommand comm = new OracleCommand("select idnacionalidad, nacionalidad from nacionalidad ", ora);
comm.CommandType = System.Data.CommandType.Text;

OracleDataAdapter oda = new OracleDataAdapter(comm);

DataSet ds = new DataSet();

oda.Fill(ds);
combonacionalidad.DisplayMemberPath = "nacionalidad";
combonacionalidad.SelectedValuePath = "idnacionalidad";
}



XAML :


<ComboBox x:Name="combonacionalidad" HorizontalAlignment="Left" Margin="520,76,0,0" VerticalAlignment="Top" Width="110" Loaded="combonacionalidad_Loaded"/>




1 Answer
1



You have to bind the combonacionalidad.ItemSource property to the data you want to use first in order to use DisplayMemberPath and SelectedValuePath properties.
Using this question as a source of information I've slightly modified your code, see if it works like this:


combonacionalidad.ItemSource


DisplayMemberPath


SelectedValuePath


private void combonacionalidad_Loaded(object sender, RoutedEventArgs e)
{
ora.Open();
OracleCommand comm = new OracleCommand("select idnacionalidad, nacionalidad from nacionalidad ", ora);
comm.CommandType = System.Data.CommandType.Text;

OracleDataAdapter oda = new OracleDataAdapter(comm);

DataTable dt = new DataTable();
oda.Fill(dt);
combonacionalidad.ItemsSource = dt.AsDataView();
combonacionalidad.DisplayMemberPath = "nacionalidad";
combonacionalidad.SelectedValuePath = "idnacionalidad";
}





it still does not work :'(
– Ernesto Andres Cabello Venegas
Jun 30 at 7:22





I noticed that you are missing the cmdd.ExecuteNonQuery(); and OracleDataReader dr = cmdd.ExecuteReader(); take a look at the answer HERE
– greyhame
Jun 30 at 7:26


cmdd.ExecuteNonQuery();


OracleDataReader dr = cmdd.ExecuteReader();





idk why but its work to me adding what you say and this :using (OracleConnection conn = new OracleConnection(Connectionn.ConnectionString))
– Ernesto Andres Cabello Venegas
Jun 30 at 7:47






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Render GeoTiff to on browser with leaflet

How to get chrome logged in user's email id through website

using states in a react-navigation without redux