FormView change data before hitting database?

Y

Yin99

I have the code below and get the error "cannot change read only
object insied a foreach loop" I am hoping someone could graciously
provide code example of how I could do this? Thanks! - Yin

foreach (DictionaryEntry entry in e.Values)
{

if (String.Compare(entry.Key.ToString(),"CustomerID")==0) {
entry.Value = "5";
}
}
 
G

Guest

Hi Yin,
DictionaryEntry is a structure = value type. You cannot modify any of its
properties when it is returned by Enumerator (the way how the foreach command
is handled internally).

Where do you call this block of code? What type is the e? What type of data
source are you using? Data source controls generally provides set of events
like Inserting, Updating etc. which allows you to update values before you
send them to data source.

Regards,
Ladislav
 
Y

Yin99

Thanks for the reply. Actually I found solution to my problem... I
was trying to set value of a field in the formview
(CustomerID) before it writes the record to the database. I had to
problems, 1) not putting the correct ID for the control in
FindControl call so I was getting null 2) not calling in right
place. So I now have this code and it works-
(I used ItemCommand because this event seems to happen before anything
touches the database, but maybe there is a better event to put it in?)


protected void FormView1_ItemCommand(object sender,
FormViewCommandEventArgs e)
{
TextBox txtbox = ((TextBox)
(FormView1.FindControl("CustomerID")));
if (txtbox != null) {
txtbox.Text = "5";
}
}
 

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,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top