-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Updating a foreign key to Null value.
PostPosted: Mon Jan 31, 2011 6:38 am 
Newbie

Joined: Mon Jan 31, 2011 6:13 am
Posts: 2
I have two table BuildGroup and table DocumentTemplate. DocumentTemplate table has BuildGroupId as foreign key which is nullable. I certain senario I update BuildGroupId in DocumentTemplate table.
Code:
public bool EditDocTempForBldGrp(int docId, int bldGrpId)
        {
            try
            {
                using (ISession session = Document.OpenSession())
                {
                    using (ITransaction transaction = session.BeginTransaction())
                    {
                        HSDocumentTemplate objDocBO = new HSDocumentTemplate();

                        objDocBO = GetDocumentDetailsById(docId);
                        HSBuildGroup objBldGrp = new HSBuildGroup();

                        if (bldGrpId != 0)
                        {                           
                            objBldGrp.Id = bldGrpId;
                        }
                        else
                        {
                            //int ? bldid = null;
                            //objDocBO.HSBuildGroup.Id = null;
                            //objDocBO.HSBuildGroup.Id = DBNull.Value;
                            //objDocBO.HSBuildGroup.Id = -1;

                        }
                        objDocBO.HSBuildGroup = objBldGrp;
                        session.Update(objDocBO);
                        transaction.Commit();
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
           
        }


In certain other senario I need to set BuildGroupId in DocumentTemplate table again to dbnull.value. I tried with different cases as in else block. It giving the erro : Cannot implicitly convert type 'System.DBNull' to 'int'
How can I update a forgein key value with NULL?
Any help will be appriciated.


Top
 Profile  
 
 Post subject: Re: Updating a foreign key to Null value.
PostPosted: Mon Jan 31, 2011 12:10 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
objDocBO.HSBuildGroup = null should do the trick

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.