这是我的存储过程的代码错误转换数据类型为varchar到int
ALTER procedure [Proc_Add_User]
(@UserId varchar(20),
@UserName varchar(100),
@Page_Name varchar(20),
@AccessIndicator int,
@CreatedBy varchar(50),
@returnStatus varchar(50) output)
as
DECLARE @intErrorCode INT
DECLARE @Page_Indicator INT
begin
BEGIN TRAN
Set @Page_Indicator = (select Page_Indicator from Pages where [email protected]_Name);
if (select count(*) from Users where [email protected] and [email protected]) > 0 begin
if (select count(*) from User_Credentials where [email protected]_Indicator and
[email protected] ) > 0
set @returnStatus='User already has access'
else
insert into User_Credentials(UserId,Page_Indicator,Access_Indicator,CreatedBy)
values (@UserId,@Page_Indicator,@AccessIndicator,@CreatedBy)
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
end
else begin
insert into Users(UserId,UserName,CreatedBy)
values(@UserId,@UserName,@CreatedBy)
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
insert into User_Credentials(UserId,Page_Indicator,Access_Indicator,CreatedBy)
values (@UserId,@Page_Indicator,@AccessIndicator,@CreatedBy)
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
end
COMMIT TRAN
if(@returnStatus is null)
set @returnStatus='Success';
PROBLEM:
IF (@intErrorCode <> 0) BEGIN
set @returnStatus= 'Unexpected error occurred!'
ROLLBACK TRAN
end
end
,我来自粘贴下面的代码调用此:
Con.Open();
cmd = new OleDbCommand();
cmd.Connection = Con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Proc_Add_User";
cmd.Parameters.Clear();
cmd.Parameters.Add("@UserId", SqlDbType.VarChar).Value = userLanId;
cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = userName;
cmd.Parameters.Add("@Page_Name", SqlDbType.VarChar).Value = pageName;
cmd.Parameters.Add("@AccessIndicator", SqlDbType.Int).Value = accessIndicator;
cmd.Parameters.Add("@CreatedBy", SqlDbType.VarChar).Value = createdBy;
OleDbParameter output = new OleDbParameter("@returnStatus", SqlDbType.VarChar);
output.Direction = ParameterDirection.Output;
cmd.Parameters.Add(output);
int result = cmd.ExecuteNonQuery();
我得到的ExecuteNonQuery
提到的错误声明。我感到困惑的是我能够在SSMS中执行存储过程,但不能从我的应用程序(前端)中执行。我也提供了相同的值,但它从我的应用程序中失败。
我仔细检查,以确保传递参数的顺序匹配,并具有相同的数据类型,但仍会引发此错误。我可以粘贴我的存储过程代码,如果想要,让我知道..提前感谢!
编辑
OOPS!我刚刚意识到所有的插入都发生了,并且在数据库中得到了很好的保证。只是这个错误在我的应用程序中被catch块捕获。有任何想法吗?
我不能忽略它,因为基于ExecuteNonQuery()的返回值,我有一些语句,也没有经过ExecuteNonQuery()后面的代码。
很难帮助,如果你的代码比说我的'代码here'以外的东西。 – 2013-05-10 21:12:13
什么数据类型是代码中的'accessIndicator'?可能试图将一个'string'转换为'int'并失败。 – 2013-05-10 21:14:47
真的UserID是VARCHAR吗? – Steve 2013-05-10 21:19:28