這是我的存儲過程的代碼錯誤轉換數據類型爲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