By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,637 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,637 IT Pros & Developers. It's quick & easy.

FormView change data before hitting database?

P: n/a

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";
}
}

Aug 15 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

"Yin99" wrote:
>
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";
}
}

Aug 15 '07 #2

P: n/a
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";
}
}

On Aug 15, 2:22 pm, Ladislav Mrnka
<LadislavMr...@discussions.microsoft.comwrote:
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

"Yin99" wrote:
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";
}
}- Hide quoted text -

- Show quoted text -

Aug 15 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.