0

I've been searching and reading and trying different things but I can accomplish the most basic thing.

I have a custom object that has several nullable properties. The problem is that when this property already has data it won't set to null. The code is simplified for simplicity on this post.

This method takes data passed on to modify a record in a database. So in the line where I create the subscription object I use a paramter to know if it is a new record or I'm modifying an existing. When I want to modify an existing one, it look in the database for the record and the returns the object populated with the data from the database. Then I overwrite the object properties with the new data and save again. What it is happening is that if I want to overwrite an existing record that has data on the property FechaAlta, and set this date to null, the object property FechaData won't set to null. Hope it is clear enough.

public string ProcessSubscription(string cRMID, string suscripID, DateTime? fechaAlta = null)
{
  Subscription subscription = null;
  ....
  subscription = new Suscription(modify)
  ....
  subscription.CRMID = cRMID;
  subscription.SuscripID = suscripID;
  subscription.FechaAlta = fechaAlta;
  ....
  return _update.UpdateEntity(subscription);
}

This is what the debugger is showing after that line. I tried to just set it to null for test purposes . Image from the debugger

3
  • 1
    Not complete. What is the exact definiton (type) of Subscription.FechaAlta , what happens in the debugger and what value does the record have in the Db before/after etc. Commented Dec 23, 2014 at 19:15
  • 1
    Also consider that a null value in .net is very different to a NULL value on a database; the later is usually treated as DbNull in .net. Of course this depends on the DBMS and the type of integration you use. Commented Dec 23, 2014 at 19:20
  • @JoshPart Sorry Josh, I tried to explain everything i could without throwing the whole class definition. The definition for FechaAlta is, of course, private DateTime? _fechaAlta = null. With a Get/Set property of the same type. When the debugger reach the line in the code example, it just remains with the old data (the DB data that is not null). Commented Dec 29, 2014 at 13:10

2 Answers 2

1

I found the problem. It was a code problem in the set method of the class property. I was setting the property only if the value != null. I was really dumb Thanks to all.

Sign up to request clarification or add additional context in comments.

Comments

0

Your question is somewhat lacking. But from what I gather you want to set fechaAlta to null in your function when certain conditions are met? If that is the case, your function isn't even attempting to set fechaAlta to null. All I see is that you have fechaAlta set up as an 'optional parameter', which means that if fechaAlta IS NOT specified when ProcessSubscription() is called, only then it will be null. However, if fechaAlta IS specified when ProcessSubscription() is called, it will never be null because you are not setting it to null.

If you call this, and dateTime is not null, it will never be null:

ProcessSubscription("string1","string2", dateTime);

It will only be null if you call this:

ProcessSubscription("string1","string2");

or

ProcessSubscription("string1","string2", null);

You need to:

subscription.FechaAlta = null;

or...

fechaAlta = null;

...somewhere in your function.

Where is the logic in your function that sets it to null?

1 Comment

I understand what you are saying. I assume that when a parameter it is not being specified when calling ProcessSubscription, it would take its default value (null), and use that to set the calss property. What i take of what you're saying is that I should check if that parameter is in fact null, and set the property to null with the reserved word "null". I will try again, but I already did what your saying with the same result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.