SelectedItem.Value

M

Martyn Fewtrell

I want to update a database record with a number of fields - doesn't sound
to difficult at this stage!

I have a system to select the correct record from the database and load the
data into a form across a number of text boxes and a single drop down list
(the idea being that the text boxes can be edited and the drop down listed
altered to reflect a new value if necessary). The drop down list in the form
is loaded from the appropriate database table with the various options and
then set to the correct value to match the record.

daGroups.Fill(dsLocalInfo, "tblGroups")
ddlGroup.DataBind
ddlGroup.SelectedItem.Text = litGroup.Text
ddlGroup.Visible = True
txtName.Text = litName.Text
txtDescription.Text = litName.Text
etc etc etc

I then use an update command to update the record in the database

cmdUpdateLocalInfo.Parameters("GroupID").Value = ddlGroup.SelectedItem.Value
cmdUpdateLocalInfo.Parameters("Name").Value = txtName.Text
etc etc

The bit I don't understand is that if I call the update without having first
used the drop down list control the update succeeds but updates the GroupID
to be the value which naturally would be at the top of the list if I hadn't
set it in the earlier statement (i.e. ddlGroup.SelectedItem.Text =
litGroup.Text). Where as if I call the update after having changed the
selection on the drop down list control the update works and accepts the
correct value from the drop down list control. Almost as if the selected
item has to be selected for it to register, where as I have selected in the
code previously (i.e. most records wont require changing groups on the drop
down list).

Hopefully this might ring a bell with somebody as I have done this (or
similar) many times before but cant work out what is wrong here!

Martyn Fewtrell
(e-mail address removed)
 
G

Guest

Are you checking that the page has not been posted back before selecting the
DDL item?
 
M

Martyn Fewtrell

Thanks Saleek but I'm not quite sure where you are coming from with that
one.

When the page originally loads, the record is displayed using literals and
then the user has the option to Delete the Record, Update the Record etc. If
the user selects the Update option the page posts back, the values from the
literal are loaded into the text boxes and the drop down list, visibility is
turned off on the literals and on for the text boxes and the drop down list.
The value for the drop down list is set to the correct value for that
record.

The user can then change any of the values before calling the Update routine
with another button.

The drop down list isn't loaded with any data until the user elect to Update
the record.

What are you thinking?

Martyn Fewtrell
(e-mail address removed)
 
G

Guest

Okay, that rules out what I was thinking. And you have viewstate turned on
for the DDL yes?

I would edit out your execution of the query and output the DDL value to a
label on potsback to check which values are being selected.
 
M

Martyn Fewtrell

I've worked it out and as usual its human error!!!!

ddlGroup.SelectedItem.Text = litGroup.Text - changes the value of the text
on the SelectedItem rather than Selecting the item to the required text
value.

I now use ddlGroup.SelectedValue = Session("GroupID") - where
Session("GroupID") holds the value of the GroupID.

This sets the drop down list correctly and therefore saves the correct value
during the update!

Thanks anyway

Martyn Fewtrell
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top