Ask a questionAsk a question
 

QuestionHow we can set an integer field to null

  • Tuesday, October 06, 2009 8:47 PMmehrdad aboudizadeh Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     


    Hi nall,

    I am having trouble to set my filed value to null in this code,

    Just to say if the value for this ebank ID does not exist then set it null

    right now we set this to zero because we could not find a solution yet,

    Thanks,


    pending.eBankID= null if not valid id

     

    IQueryable<tbl_PendingChange> QResul = null;

    QResul =

     

    from p in myTable

     

    orderby p.PendingChangeKey

     

    where p.eBankID == RuningEbankID && p.EffectiveDate == RuningEffectiveDate && p.Proccessed == false

     

    select p;

     



    foreach



    foreach

     

    (tbl_PendingChange pending in QResul)

    {

     

    // if(!String.IsNullOrEmpty(pending.NewValue))

    emp.IsCurrentKey = 2;

    emp =

    new AssignPendding().mySwith(pending.Fieldname, pending.NewValue, emp);

     

    pending.ProccessedDate =

    DateTime.Now;

    pending.Proccessed =

    true;

    pending.eBankID= null if not valid id

    }


    mehrdad in mcsdn

All Replies

  • Sunday, October 11, 2009 9:43 PMIdo Flatow. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Is the field you're trying to set to null the key that identifies the entity type? if so, you won't be able to set it to null, because key properties must have values for existing entities.

    Can you please explain what you want to do, ellaborate on how your model looks (entity types, relations, keys) and specify what you mean by "having trouble to set" - is the property of int type so it doesn't accept nulls? is it nullable and you get an exception upon saving (if so, what exception)? and if you can, indent your code so it can read (current code is hard to understand)
    Please mark posts as answers/helpful if it answers your question
  • Tuesday, October 13, 2009 5:09 AMDiego B VegaMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Mehrdad,

    I agree with Ido that it would help if you could elaborate more on what you are trying to accomplish and what kind of trouble you are having. But, just in case I can guess this right and eBankID is just a regular property that you use to store the ID of a bank, then you should make sure the property is nullable in the model and the corresponding column in the database can aslo accept null values. 

    Thanks,
    Diego 
    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Thursday, October 15, 2009 2:53 PMmehrdad aboudizadeh Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Ido,


    I elaborate more on this,

     

    I have an Integer filed in my table which is nullable called eBankID

     

    In my loop

     

    Current value of eBankID is 100

     

    I’d like to set this value to NULL in my code

    foreach (tbl_PendingChange pending in

    QResul)
    {
    pending.eBankID=null ------> here I get error is there a way to set this to null?
    }

     


    mehrdad in mcsdn
  • Thursday, October 15, 2009 2:54 PMmehrdad aboudizadeh Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     


    Hi Diego
    Please see my response to Ido


    mehrdad in mcsdn
  • Thursday, October 15, 2009 9:43 PMIdo Flatow. Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I assume you get a compilation error that you can't use a null value for int type?

    You mentioned that the table has a column which is nullable, have you made sure in the model that the property is also marked as nullable? open the edmx in the designer, select the property eBankID in the tbl_PendingChange entity and see in the properties if the nullable is set to true.

    You can also use right-click->"go to definition" on the eBankID in your code and see if the generated code made this property a nullable int (int?) or is it just an int value - I assume the problem resided there.


    Please mark posts as answers/helpful if it answers your question