CodeSmith Community
Your Code. Your Way. Faster!

Blank guid! URGENT

Latest post 12-11-2007 9:34 AM by Andrewiski. 40 replies.
  • 03-29-2006 8:17 PM

    • strazz
    • Top 100 Contributor
    • Joined on 03-16-2006
    • Chico, California
    • Posts 48
    • Points 1,363

    Blank guid! URGENT

       Sorry to put "urgent" on there I have a demo to give tomorrow morning and I am having some big problems.  I have a table which uses a uniqueidentifier as a primary key. The default value is:(newid()) and "RowGuid" is set to yes.  Whenever I make a new object and then insert it, .netTiers is making "00000000-0000-0000-0000-000000000000" the value of the column.  I want it to insert the row and then set the "ID" propery to whatever SQL Server generated, the way an ID field set to "int" would with an autoincrement.  How can I do this?

    Sorry again to put urgent, I know everyones questions are urgent but I really do need this ASAP.
    • Post Points: 35
  • 03-29-2006 8:24 PM In reply to

    • Crimper
    • Top 25 Contributor
    • Joined on 03-03-2005
    • Vancounver, BC Canada
    • Posts 172
    • Points 3,105

    Re: Blank guid! URGENT

    My recommendation would be to edit the Entity classes and initialize the Guid property to Guid.NewGuid().  This way the application is generating the new guid, not SQL Server.  Usually, when I am creating a new entity object, I initialize my guids to new a Guid.
    Phil Bolduc
    Vancouver, BC Canada -------------------------------------------------
    Former member of the .NetTiers team
    2007 MSDN Code Award - Team Developer Award winner
    -------------------------------------------------
    • Post Points: 35
  • 03-29-2006 8:31 PM In reply to

    • strazz
    • Top 100 Contributor
    • Joined on 03-16-2006
    • Chico, California
    • Posts 48
    • Points 1,363

    Re: Blank guid! URGENT

    ok, that was going to be my short term fix, just to make a new guid in the asp.net app....  is .netTiers not capable of returning the guid generated by sql server?  I guess its not a big deal if it cant....

    By the way, thank you so much for getting back to me so quickly, this forum is so awesome.  I would donate money to .netTiers if you all had a PayPal button, of course then there would be the headache of how everyone working on it would split it up.... oh well maybe I can donate some documentation when I get good at this stuff.
    • Post Points: 35
  • 03-29-2006 10:49 PM In reply to

    Re: Blank guid! URGENT

    Hi,

    If it's not too late - and for future reference for others... The answer to use NewGuid( ) is a good one. I'll take the answer a little further. When you default the DB column to (newid( )), you are basically doing the same thing that you would do in the following code:

    Employee myObj = new Employee( );

    myObj.EmployeeID = Guid.NewGuid( );

    DataRepository.EmployeeProvider.Save(myObj);

    The "problem" you are experiencing is that after the first line, myObj.EmployeeID is set to a real (but blank) Guid... Which is actually Guid.Empty - which is actually a good thing. But because it is not NULL, the default value in the DB doesn't actually get used.

    If you want a DB-side answer to this problem (but again it doesn't matter since the above code does the same thing), you would add an insert (and maybe update) trigger that checks the value of EmployeeID and then resets it to newid( ). I wont ellaborate on how to create such a trigger, because my buddy Google knows how to create a simple trigger that replaces a field that's coming in (before it actually gets saved to the row)...

    Cheers

    Clynton
    -------------------------------------
    Member of the .NetTiers team
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 65
  • 10-19-2006 8:05 AM In reply to

    • evolved
    • Top 50 Contributor
    • Joined on 12-27-2004
    • South River, NJ
    • Posts 96
    • Points 1,840

    Re: Blank guid! URGENT

    This is the solution? This is rubbish. What about the new sql method newsequentialid(), which keeps the table in order better under a clustered index, you're saying i have to create a trigger on my table now, for each table that i use that uses a guid as a p-key?

    http://www.jheidt.com
    ------------------------------
    Member of the .NetTiers team
    http://www.nettiers.com
    ------------------------------

    • Post Points: 35
  • 10-19-2006 8:28 AM In reply to

    Re: Blank guid! URGENT

    Hi

    That was a solution, not necessarily the best or only one available (you could also take a look at the following thread http://community.codesmithtools.com/forums/thread/18913.aspx where this was also discussed. 

    While newsequentialID is available for SQL2005, it's not available for previous versions so wouldn't suit/help everyone.  The original post was also asking for how to deal with the blank GUID being generated and all newsequentialID() does is to offer a different method for generating the new GUID server-side.  Also, part of the issue is how the entity is being initialized (i.e. it's being created with a guid.empty as opposed to null/nothing - this value is then being passed to the DB and as it's not a null value being passed to the proc, the default isn't used). 

    The other suggestion made (in the thread pointed to above) was to alter the generated code to remove the passing of the GUID field back to the proc (and naturally alter the proc accordingly) so that a) it would use the default value (namely NewID()), and b) that new value would be returned (as currently, the sprocs generated use SCOPE_IDENTITY() to return the value of the newly created PK and this doesn't work for GUID fields).

    HTH

    Martin

    • Post Points: 35
  • 01-12-2007 9:58 AM In reply to

    • evolved
    • Top 50 Contributor
    • Joined on 12-27-2004
    • South River, NJ
    • Posts 96
    • Points 1,840

    Re: Blank guid! URGENT

    I was just reading up on some SQL somewhere on a topic that instantly reminded me of this exact post. It's about getting back the ID from SQL Server when NEWID() or NewSequentialID() is used. In the case of SQL Server 2005 i think .netTiers should use the following tactic:

    <  From  http://sqljunkies.com/Article/4067A1B1-C31C-4EAF-86C3-80513451FC03.scuk >

    For SQL Server 2005, when inserting into a column with NewID() or NewSequentialID() as the primary (surrogate) key, you could retrieve the ID based on the natural key. But the exciting development in 2005 is the OUTPUT clause. Try this simple test:

    CREATE TABLE t1 (EntryID uniqueidentifier DEFAULT NewID())
    CREATE TABLE t2 (EntryID uniqueidentifier DEFAULT NewSequentialID())

    INSERT INTO t1 OUTPUT INSERTED.* DEFAULT VALUES
    INSERT INTO t2 OUTPUT INSERTED.* DEFAULT VALUES
     


     
    EntryID
    ------------------------------------
    FE06B4B9-0838-4AC5-AF45-827D570A9F75

    EntryID
    ------------------------------------
    53644D63-38F6-DA11-8F73-00096B6FFD84

     

     

    http://www.jheidt.com
    ------------------------------
    Member of the .NetTiers team
    http://www.nettiers.com
    ------------------------------

    • Post Points: 5
  • 01-30-2007 10:16 AM In reply to

    • jletts
    • Top 200 Contributor
    • Joined on 12-13-2006
    • Posts 22
    • Points 520

    Another question about defaulting to guid.empty

    the current (build 468) commonsqlcode.cs for handling declared defaults in a table (GetCSDefaultValueByType) is:

    if (defaultValue.Contains("()"))
    {
    	if ( defaultValue.ToLower() == "getdate()" )
    		defaultValue = "DateTime.Now";
    	else if ( defaultValue.ToLower() == "newid()" )
    		defaultValue = "new Guid()";
    	else if ( defaultValue.ToLower() == "getutcdate()" )
    		defaultValue = "DateTime.UtcNow";
    	else
    		return null;
    }
    
    The intent seems to be to initialize an entity to the same default 
    value that an null database column insert would produce.
    In the case of the guid though, the entity defaults to Guid.Empty. 
    Shouldn't it default to Guid.Newguid?
    The workaround is obvious: Always explicity set any guids in new entities. 
    But the code above seems like it is designed specifically to handle it automatically.
    This code has been like this since last September (build 378). 
    Anyone have an idea about why this is a feature, not a bug?
    -Jim
    Jim Letts ------------------------ Geek in Residence Contuity Group
    • Post Points: 35
  • 01-30-2007 12:03 PM In reply to

    Re: Another question about defaulting to guid.empty

    yes, that's correct it should be Guid.NewGuid().  Thanks for the note!

    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 95
  • 01-31-2007 8:50 AM In reply to

    • evolved
    • Top 50 Contributor
    • Joined on 12-27-2004
    • South River, NJ
    • Posts 96
    • Points 1,840

    Re: Another question about defaulting to guid.empty

    I think that in the case of 2005 you should use the above strategy, a wonderful new feature is the newsequentialid() function, which allows for clustered indexes to be more effective during insertion operations, without thrashing about to reindex the table. Click on the link in my post above for more details, ideally this would work for any type of field that had any type of default value. In the case of a primary key with a default value (ie: newid or newsequentialid) i think netteirs would make the pkey property read only to the application developer, and would set that property and propigate its values to children when the row was actually created.

     

    Thoughts? 

    http://www.jheidt.com
    ------------------------------
    Member of the .NetTiers team
    http://www.nettiers.com
    ------------------------------

    • Post Points: 5
  • 02-13-2007 9:22 AM In reply to

    • Gonzo
    • Top 75 Contributor
    • Joined on 07-21-2006
    • Glasgow, Scotland
    • Posts 57
    • Points 1,480

    Re: Another question about defaulting to guid.empty

    Hi Robert, I am having the same troubles with guids just now and I am really not sure how to get it working.  No matter what i do it always seems to assign a blank GUID to my entitiy on insert.

    An example of my code is as follows:

                Labels myItem = new Labels();
                myItem.Name = txtName.Text.Trim();
                myItem.StatusID = int.Parse(radStatus.SelectedValue.ToString());
                myItem.URL = txtURL.Text;
                myItem.Description = txtDescription.Value.Trim();
                myItem.Biography = txtBiography.Value.Trim();

                bool success = DataRepository.LabelsProvider.Insert(myItem);

                if (success)
                {
                    SetMode("INSERTED");
                }
                else
                {
                    // an error has ocurred
                }

    I am not sure where i am going wrong here and would appreciate all assistance offered as I have reached a key part of my application that i need to deliver this week.

    Cheers 

    David lawton 

    David Lawton Hyperion Technologies Ltd (UK)
    • Post Points: 35
  • 02-14-2007 8:45 AM In reply to

    • strazz
    • Top 100 Contributor
    • Joined on 03-16-2006
    • Chico, California
    • Posts 48
    • Points 1,363

    Re: Another question about defaulting to guid.empty

    David,

    I'm not sure exactly what the problem is from your post... this may or may not help.  You need to set your Guid field to:

     

    Guid.NewGuid();

     

    For example,

     

    Labels myItem = new Labels();

    myItem.property1 = Guid.NewGuid();

     

    That would be if the "property1" column in your database was a "uniqueidentifier"  Let me know if I am way off base here or I misunderstood your question.
     

    • Post Points: 5
  • 02-14-2007 2:35 PM In reply to

    • jletts
    • Top 200 Contributor
    • Joined on 12-13-2006
    • Posts 22
    • Points 520

    Re: Another question about defaulting to guid.empty

    It appears that update to GetCSDefaultValueByType was never put into the incorporated. As a result, new entities are always created with an all zeros guid, even if the schema has a default value of newid() set for the column. You must explicitly set any guid fields to guid.newguid() before you save the entity.

     As a matter of protocol, I'm not sure whether I should make this change myself, or leave it to the "nettiers team". It seems that most of the time, the team makes the changes with attributions to the contributor, but I'm not sure. What is the prefered mode for plain old community members like me? Should I:

    • update my working copy, make the patch and commit to the current SVN repository,
    • create a patch and attach it to an post in one of the forums
    • note the issue in one of the forums and wait for a member of the team to address it
    • note the issue on a master buglist somewhere

    I would think that since the SVN repository is on sourceforge, the tracker and forums would be as well. But it seems that Codesmith's support makes this a bit different from a typical open source project. Hence my confusion above.

    Any thoughts from the "team"?

    Jim Letts ------------------------ Geek in Residence Contuity Group
    • Post Points: 65
  • 02-14-2007 5:09 PM In reply to

    • bgjohnso
    • Top 10 Contributor
    • Joined on 09-15-2005
    • Spokane, WA
    • Posts 765
    • Points 22,535

    Re: Another question about defaulting to guid.empty

    Hi Jim,

    We always welcome contributions by the community.  Not to make excuses, but we are a small team with real jobs/lives/priorities so we can't always get to things as quickly as possible.  If you have a contribution, create a patch and post it in the Contributions and Patches forum.  It helps if you include a link to any related forum posts to help keep all of the information together.  The SVN repository is protected, so you won't be able to commit the patch yourself.

    I can't speak as to why the forums are here rather than on SourceForge as I wasn't really involved with that.  I know that the forum and tracker used to be setup on SourceForge, but we ended up with some bugs posted in the tracker, on the SF forums and on the CS forums.  It became very difficult to track anything as the information was spread in several different locations (kind of like it is now, I supposed :-)).

    Ben Johnson
    ------------------------------
     Member of the .NetTiers team
     Visit http://www.nettiers.com
    ------------------------------

    • Post Points: 65
  • 02-15-2007 12:57 AM In reply to

    • jletts
    • Top 200 Contributor
    • Joined on 12-13-2006
    • Posts 22
    • Points 520

    Re: Another question about defaulting to guid.empty

    Thanks for the clarification about how to contribute. It isn't much of a community if all of the tweaking falls on your (and the rest of the NT "team") shoulders. Still it is better to have moderated contributions than a free for all...

     As far as SF goes, it still might make sense to have a master "to-do" list somewhere. I've spent a lot of time meandering through the content on the CS/NT forums, and I still havce to do most of my research via the search box. For how-to and discussions, this is probabably as good as it gets, but it just seems too difficult to figure out when I'm up against an actual bug. A list of "known issues" would be helpful so I know when the problem is not just in my aging brain. I've learned that when NT is behaving strangely I should: first, check the NT documentation (:-?), then try to formulate a good search aginst the CS/NT forums, and finally proceed with figuring out where I've goofed. Of course, NT is not nearly as buggy as my own code, but when the issue is in NT, I can waste a lot of time....

    -Jim

    Jim Letts ------------------------ Geek in Residence Contuity Group
    • Post Points: 35
Page 1 of 3 (41 items) 1 2 3 Next > | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems