本文介绍了如何将nvarchar转换为uniqueidentifier的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

Hello,

cmd.Parameters.AddWithValue("@ParentGroupId", ddlParentName.SelectedItem.Value);



在此我的parentGroupId是UniqueIdentifier,但我在插入数据时将值作为参数传递出现以下错误..




In this my parentGroupId is UniqueIdentifier but i am passing the value as parameters while inserting the data having the following error..

Error converting data type nvarchar to uniqueidentifier.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Error converting data type nvarchar to uniqueidentifier.

Source Error:
Line 39:                 }
Line 40:                 con.Open();
Line 41:                 int res = cmd.ExecuteNonQuery();
Line 42:                 if (res > 0)
Line 43:                 {





我该如何删除这个错误..



please how can i remove this error..

推荐答案

Guid guid;
if (Guid.TryParse(ddlParentName.SelectedItem.Value, out guid))
{
Parameters.Add("@ParentGroupId", SqlDbType.UniqueIdentifier).Value =  guid;
}


cmd.Parameters.AddWithValue("@ParentGroupId", Guid.Parse(ddlParentName.SelectedItem.Value));



这篇关于如何将nvarchar转换为uniqueidentifier的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 23:21