Skip to content

Web app comparison is not correct

Sie Ste

Asked Jun 24, 2012, 8:50 PM EDTSolved
Share
LinkedInX

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

  }

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • ✓ Accepted answer0
    Just the check return type between e.NewValues["Cutoff_Date"]  and e.OldValues["Cutoff_Date"] ?  Convert the e.NewValues and e.OldValues to DateTime object and do the datetime comparision then it will work as expected.

    Try this,

    int result = DateTime.Compare(Convert.ToDateTime(e.NewValues["Cutoff_Date"] ), Convert.ToDateTime(e.OldValues["Cutoff_Date"] ));

    If the result is zero then both the dates are equal otherwise both are not-equal.

    Hope this helps you.
    0