Q
Problem solve Get help with specific problems with your technologies, process and projects.

How can I fill data using DataReader and also store the ID in the database?

I want to fill the data using DataReader in listbox/combobox with data showing description, but at the time of save I want to store the ID in the database. How do I do that?
After you add a DropDownList to a form, set its DataTextField to the datareader field you want to display, and the DataValueField to the field you want stored. Filling the control is a matter of executing a command and databinding to the reader:

OleDbCommand cmd = new OleDbCommand("select EmployeeID, LastName from Employees", this.cnConnection);
this. cnConnection.Open();
OleDbDataReader reader =
 cmd.ExecuteReader(CommandBehavior.CloseConnection);
cbEmployees.DataSource = reader;
cbEmployees.DataBind();

Now, whenever you want to get the current value (not the text displayed), you ask for the control SelectedValue property. Alternatively, you can use the SelectedItem property which exposes Text, Value and Selected (for a list) properties about the current item.

Dig Deeper on Win Development Resources

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

TheServerSide.com

Close