如何将nvarchar转换为uniqueidentifier [英] How to convert nvarchar to uniqueidentifier

查看:575
本文介绍了如何将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..

推荐答案

你可以试试

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


有点不清楚哪个值被定义为uniqueidentifier:ParentGroupId或ddlParentName.SelectedItem.Value的数据库列?我假设第一个是uniqueidentifier,第二个是字符串。因此我建议:

A little unclear which value is defined as a uniqueidentifier: the database column for ParentGroupId or ddlParentName.SelectedItem.Value? I assume here the first to be a uniqueidentifier, and the second a string. Hence I'd suggest:
cmd.Parameters.AddWithValue("@ParentGroupId", Guid.Parse(ddlParentName.SelectedItem.Value));


尝试在存储过程中将唯一ID参数设置为nvarchar或varchar。如果传递了正确的唯一标识符值,那么该值将存储在表中而没有任何错误
Try making the Unique ID parameter to nvarchar or varchar in the stored procedure. If proper unique identifier value is getting passed and then the value will be stored in the table without any errors


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆