Mahdi_Nine
Mahdi_Nine

Reputation: 14761

How to convert string to uniqueidentifier?

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

Answers (4)

juFo
juFo

Reputation: 18587

use one of these:

Guid.TryParse

or

Guid.TryParseExact

Upvotes: 2

Mark Menchavez
Mark Menchavez

Reputation: 1661

In .Net 4, there's a Guid.TryParse(string, out Guid) which returns bool on success.

Upvotes: 7

Yuck
Yuck

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

Francis Gilbert
Francis Gilbert

Reputation: 3442

You can try:

new Guid("string to convert");

But the string will need to be in Guid format already.

Upvotes: 11

Related Questions