I've been trying to insert some data on a database and at the same time get the identifier.
The identifier type is a Guid.
I already tried some resolutions.
I was searching but I couldn't found any that worked with a Guid in C#.
The last one I tried was like this:
dbAccess db = new dbAccess();
SqlConnection con = db.openConnection();
Guid retVal = Guid.Empty;
try
{
SqlCommand cmd = new SqlCommand(@"insert into Comment(IdDiscussion,UserId,Description,DateTime)
values(@IdDiscussion,@UserId,@description,@dateTime);
set @ret=SCOPE_IDENTITY();", con);
cmd.Parameters.AddWithValue("@IdDiscussion", discussionId);
cmd.Parameters.AddWithValue("@UserId", userId);
cmd.Parameters.AddWithValue("@description", comment);
cmd.Parameters.AddWithValue("@dateTime", dateTime);
SqlParameter retParameter = new SqlParameter("@ret", SqlDbType.UniqueIdentifier);
retParameter.Direction = ParameterDirection.Output;
cmd.Parameters.Add(retParameter);
cmd.ExecuteNonQuery();
retVal = (Guid)retParameter.Value;
}
catch (Exception e)
{
//doesn't really matter
}
db.closeConnection();
return retVal;
In this case I get the following error on executeNonQuery(): "Operand type clash: numeric is incompatible with uniqueidentifier"
.
Any suggestion that can help?