Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what i am doing is

if(emp.name!=this.name)
{

//I am adding it to audit trail .

}

but sometimes if emp.name=null and this.name="" the if statement gets executed as the condign is true and in my audit grid old value= blank and new value = blank gets speedily ... which i don't want to


how can i this condion makes fall
Posted

Change the condition to

if(!String.IsNullOrEmpty(emp.name) && (emp.name!=this.name))
 
Share this answer
 
C#
if (!string.IsNullOrEmpty(emp.name) && !string.IsNullOrEmpty(this.name) && (emp.name != this.name)) {
   // Whatever
}


[EDIT] Solution for specific case when one of the terms is null and the other is empty

C#
if (!((emp.name == null && this.name == String.Empty) || (emp.name == String.Empty && this.name == null)) && (emp.name != this.name)) {
   // Whatever
}


There should be a way to simplify these conditions but I do not have time for that now.

[/EDIT]
 
Share this answer
 
v2
Comments
Torakami 16-Sep-13 5:42am    
wither of them can be null or empty .. but they should not be equal if one of them is null and other is ""
phil.o 16-Sep-13 6:11am    
I do not have time to test that for now, bit I thought a comparison with any of both terms being null would return false.
If this is not the case for you, then see my updated solution.
I think best approach to your problem is convert.tostring()

as per my knowledge it handles null values


so your new comparison would be

C#
if(convert.tostring(emp.name)!=convert.tostring(this.name))
{

//I am adding it to audit trail .

}


regards...:)
 
Share this answer
 
Comments
Torakami 16-Sep-13 5:39am    
No .. convert.tostring doesnt work for me .. it takes null !="" only which is failing my logic ..
Dholakiya Ankit 16-Sep-13 5:42am    
why not working what your emp.name value and this.name value give me full data this should work this will convert null value to "" which you want
Torakami 16-Sep-13 6:14am    
if (emp.ReasonForLeaving != this.ReasonForLeaving)
{
objempAudit = new ActusLibrary.Linq.EmployeeAudit();
objempAudit.Module = "Employee";
objempAudit.ReferencedId = emp.Id;
objempAudit.OldValue = emp.ReasonForLeaving;
objempAudit.NewValue = this.ReasonForLeaving;
objempAudit.Operation = "Update";
objempAudit.Operation_status = "Success";
objempAudit.ModifiedOn = DateTime.Now;
objempAudit.ModifiedBy = ApConfig.CurrentUser.EmpId;
objempAudit.CompanyId = emp.Company_Id;
objempAudit.ColumnName = "Reason For Leaving";
lstEmpAudit.Add(objempAudit);

emp.ReasonForLeaving = this.ReasonForLeaving;
}

this is what my code is i am using in order to add it to audit trail .. I tried with your own way .. still if emp.ReasonForLeaving =null and this.ReasonForLeaving ="" both are not equating and i am falining to build my logic
Torakami 16-Sep-13 6:16am    
if any one is null or empty with other string to compare then will be fine ... coz i want to disply old value and new value ... so one can be blank and other can be value ... but not both should gets diplsyed blank coz both are not equating null = ""
Dholakiya Ankit 16-Sep-13 6:23am    
i think you want that previous reason for leaving and this reason both should not be equal or both should not be empty right?
C#
if (!((emp.name == null && this.name == String.Empty) || (emp.name == String.Empty && this.name == null)) && (emp.name != this.name)) 
{
   // do other things here
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900