I have a dropdown list. I am getting data from oracle data base as a dataset I want to fill the dropdown list with dataset values(data text field and data value field). Data coming from the database as normal but I cant bind the values with my drop down list. "ds" is the dataset.
ddlDepartment.DataValueField = ds. Tables[0].Rows[0]["DEPARTMENT"].ToString();
ddlDepartment.DataTextField = ds. Tables[0].Rows[0]["DEPARTMENT_NAME"].ToString();
3
Answers
I was able to fill the dataset as follows at the page load
and then find the values as follows
I think you might be grasping this wrong.
The dropdown combo has a simple setting that allows you to "set" WHAT values from the datasource will be used from the data table you "feed" the drop down list.
So, you can have this markup:
So, you can set the two columns used – they are NOT for feeding data to the dropodown.
You can also set the above two columns in code – but LITTLE need exists to do that.
eg:
ONCE you set the above, you are now free to query the database, load up say a datatalbe, and then assign that "table" to the Dropdown list.
You do it this way:
So, note how the data table has 3 columns, but WHICH of the 3 do you want to use fo for the dropdown? You have two columns – typical the "ID" or "PK" value, and then the 2nd column is a text description.
Now, I am using the SqlProvider (for sql server). You have to replace SqlCommand with the OracleSQLcommand and also the connection. But the data table, and code that fills the dropdown list reamins the same as per above – regardless of what data provider you are using.
So those two settings (DataValueField, DataTextField) are NOT to be feed data, but are ONLY to set which columns to use from the data table. My example had 3 columns, but there could be 20 columns in that table – so those two settings determine which two columns to use. And often you might have a simple drop down to select a color or some such – and thus you ONLY need one column. In that case, set both Value/Text field to the one same column.
Adjusted version of your code, removing unnecessary operations:
And then GetDepartments might look like: