In a C# 2010 web form that uses a formview control, I am comparing 2 dictionary objects to see if their values are different, right before the actual change occurs in the database. The code is comparing the old dictionary value to the new dictionary value
to see if the value has changed. If so an 'invalid' date field is being set.
However the problem is the values are actually the same. By stepping through the code, the application thinks the values are different. Can you tell me what is wrong with the code listed below:
If your values are DateTime or another reference type other than a string, your comparison will always fail. You can modify your comparison to something like either of these:
if (e.NewValues["Cutoff_Date"].ToString() != e.OldValues["Cutoff_Date"].ToString())
{
e.NewValues["Invalid_Date"] = "Y";
}
if ((DateTime.Compare((DateTime)e.NewValues["Cutoff_Date"], (DateTime)e.OldValues["Cutoff_Date"])) != 0)
{
e.NewValues["Invalid_Date"] = "Y";
}
Either of these alternate comparison methods should solve your problem.
"What I hear, I forget; What I see, I remember; What I do, I understand." --Confucius
Remeber to Mark as Answer if this post helped you.
It's because you are comparing reference types (like an object). when you compare them, you are not really comparing their values. Instead you are comparing the reference that points to those values. In a semi-abstract analogy, think of it as if you have
two email addresses. Each one will direct email to you, but if you compare the email addresses, they are different. Each datetime reference points to the same value, but the references themselves are different.
"What I hear, I forget; What I see, I remember; What I do, I understand." --Confucius
Remeber to Mark as Answer if this post helped you.
Marked as answer by wendy elizabeth on Jun 26, 2012 01:14 AM
wendy elizab...
Member
321 Points
390 Posts
C# formview.itemupate issue
Jun 25, 2012 04:36 AM|LINK
In a C# 2010 web form that uses a formview control, I am comparing 2 dictionary objects to see if their values are different, right before the actual change occurs in the database. The code is comparing the old dictionary value to the new dictionary value to see if the value has changed. If so an 'invalid' date field is being set.
However the problem is the values are actually the same. By stepping through the code, the application thinks the values are different. Can you tell me what is wrong with the code listed below:
protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e) { if (e.NewValues["Cutoff_Date"] != e.OldValues["Cutoff_Date"]) { e.NewValues["Invalid_Date"] = "Y"; } }grundebar
Contributor
4515 Points
726 Posts
Re: C# formview.itemupate issue
Jun 25, 2012 02:10 PM|LINK
If your values are DateTime or another reference type other than a string, your comparison will always fail. You can modify your comparison to something like either of these:
if (e.NewValues["Cutoff_Date"].ToString() != e.OldValues["Cutoff_Date"].ToString()) { e.NewValues["Invalid_Date"] = "Y"; } if ((DateTime.Compare((DateTime)e.NewValues["Cutoff_Date"], (DateTime)e.OldValues["Cutoff_Date"])) != 0) { e.NewValues["Invalid_Date"] = "Y"; }Either of these alternate comparison methods should solve your problem.
Remeber to Mark as Answer if this post helped you.
wendy elizab...
Member
321 Points
390 Posts
Re: C# formview.itemupate issue
Jun 25, 2012 04:35 PM|LINK
I changed my type to datetime and the logic work! However can you tell me why this type of dictionary comparison does not work?
grundebar
Contributor
4515 Points
726 Posts
Re: C# formview.itemupate issue
Jun 25, 2012 07:53 PM|LINK
It's because you are comparing reference types (like an object). when you compare them, you are not really comparing their values. Instead you are comparing the reference that points to those values. In a semi-abstract analogy, think of it as if you have two email addresses. Each one will direct email to you, but if you compare the email addresses, they are different. Each datetime reference points to the same value, but the references themselves are different.
Remeber to Mark as Answer if this post helped you.