Gavin Miller
Gavin Miller

Reputation: 43815

Why would Guid.NewGuid() be generating an empty guid?

I have a Guid.NewGuid() call that is creating an Empty Guid.

What would cause such a problem and how can I fix it?

Edit: The code:

<WebMethod()> _
Public Function CreateRow(rowValue As String) as String
    Dim rowPointer As Guid = System.Guid.NewGuid()
    Dim rowPointerValue As String = rowPointer.ToString()

    Try
        Dim result as Integer = SqlHelper.ExecuteNonQuery(ConnectionString, "Sproc_Name", rowValue, rowPointer)

        Return result
    Catch ex as Exception
        Throw ex
    End Try
End Function

Edit: Turns out that rowPointer was originally being passed to the SqlHelper and not rowPointerValue - This of course is passed as empty, as pointed out in the answers. Changing it to rowPointerValue/rowPointer.ToString() fixed the problem.

Upvotes: 3

Views: 10231

Answers (5)

eric
eric

Reputation:

Try this

Dim g As New Guid();

Dim whereDoYouWantToSeeIt As String = g.ToString();

Upvotes: 0

darryl
darryl

Reputation: 11

I've only experiened this problem in VB.NET and only while debugging. Thanks 48494, for that info.

Upvotes: 1

user48494
user48494

Reputation: 122

This is an old problem in VB.NET. It is only the debug visualizer that is broken.

http://www.thesoftwaredevotional.com/2008/12/guid-visualizer-broken-in-vbnet.html

Upvotes: 6

TGnat
TGnat

Reputation: 4001

I tested this code in VS2008 and the results are not what I expected. It seems that the new guid is not created until the toString method is called. After stepping through the code rowPointerValue does hold a string representation of the guid.

It seems to be using defferred execution?

Upvotes: 1

abatishchev
abatishchev

Reputation: 100268

I had the same thing. Debugging of Guid.NeGuid() was showing that it's empty. Calling .ToString() fixed the situation.

Upvotes: 2

Related Questions