Reputation: 14761
I want to insert a string into a table as a uniqueidentifier type. But when I insert it to the database, it throws an error. How can i convert a string
to a uniqueidentifier
?
Upvotes: 1
Views: 20625
Reputation: 1661
In .Net 4, there's a Guid.TryParse(string, out Guid)
which returns bool
on success.
Upvotes: 7
Reputation: 50855
This is a safe way to attempt parsing a string
into a Guid
. In my example, input
is a string
variable from the user:
var myGuid = new Guid();
if (Guid.TryParse(input, out myGuid)) {
// Parsed OK
}
Upvotes: 3
Reputation: 3442
You can try:
new Guid("string to convert");
But the string will need to be in Guid format already.
Upvotes: 11