lankapala
asked on
T-sql C# CLR Error
I have created CLR using C# and i have added to successfully to database to my CLR.
But when i'm try to run error message is showin
Error
Msg 6522, Level 16, State 1, Procedure SendOrdersAPI, Line 9
A .NET Framework error occurred during execution of user-defined routine or aggregate "SendOrdersAPI":
System.InvalidOperationExc eption: Invalid attempt to read when no data is present.
System.InvalidOperationExc eption:
at System.Data.SqlClient.SqlD ataReader. CheckDataI sReady(Int 32 columnIndex, Boolean allowPartiallyReadColumn, Boolean permitAsync, String methodName)
at System.Data.SqlClient.SqlD ataReader. TryReadCol umn(Int32 i, Boolean setTimeout, Boolean allowPartiallyReadColumn)
at System.Data.SqlClient.SqlD ataReader. GetValueIn ternal(Int 32 i)
at System.Data.SqlClient.SqlD ataReader. GetValue(I nt32 i)
at ManCLR.dbConnection.userna me()
at ManCLR.SendOrderOMS.SendOr dersTOAPI( String OrderNumber, Int32 TotalNumberLine)
at ManCLR.DenManMain.sendOMS( String OrderNumber, String username)
And also i used following commands in the Sql server to add assembly
CREATE ASSEMBLY SMdiagnostics AUTHORIZATION dbo FROM 'C:\Windows\Microsoft.NET\ Framework6 4\v4.0.303 19\SMdiagn ostics.dll ' WITH permission_set = unsafe
CREATE ASSEMBLY [System.Web] AUTHORIZATION dbo FROM 'C:\Windows\Microsoft.NET\ Framework6 4\v4.0.303 19\System. Web.dll' WITH permission_set = unsafe
--CREATE ASSEMBLY [System.Runtime.Serializat ion] AUTHORIZATION dbo FROM 'C:\Windows\Microsoft.NET\ Framework6 4\v4.0.303 19\System. Runtime.Se rializatio n.dll' WITH permission_set = unsafe
CREATE ASSEMBLY [System.IdentityModel.Sele ctors] FROM 'C:\Windows\Microsoft.NET\ Framework6 4\v4.0.303 19\System. IdentityMo del.Select ors.dll' with permission_set = unsafe
CREATE ASSEMBLY [System.Messaging] AUTHORIZATION dbo FROM 'C:\Windows\Microsoft.NET\ Framework6 4\v4.0.303 19\System. Messaging. dll' WITH permission_set = unsafe
CREATE ASSEMBLY [Microsoft.Transactions.Br idge] FROM 'C:\Windows\Microsoft.NET\ Framework6 4\v4.0.303 19\Microso ft.Transac tions.Brid ge.dll' with permission_set = unsafe
Please attached Assembly Details form
select * from sys.assemblies
if anyone can let me know how to solve much appreciated.
Many Thanks
Assembly.xlsx
But when i'm try to run error message is showin
Error
Msg 6522, Level 16, State 1, Procedure SendOrdersAPI, Line 9
A .NET Framework error occurred during execution of user-defined routine or aggregate "SendOrdersAPI":
System.InvalidOperationExc
System.InvalidOperationExc
at System.Data.SqlClient.SqlD
at System.Data.SqlClient.SqlD
at System.Data.SqlClient.SqlD
at System.Data.SqlClient.SqlD
at ManCLR.dbConnection.userna
at ManCLR.SendOrderOMS.SendOr
at ManCLR.DenManMain.sendOMS(
CREATE PROCEDURE SendOrdersAPI
(
@OrderNumber nvarchar(MAX),
@username nvarchar(MAx)
)
AS
EXTERNAL NAME ManCLR.[ManCLR.ManMain].sendAPI
GO
EXEC SendOrdersAPI 'Test123','sa'
And also i used following commands in the Sql server to add assembly
CREATE ASSEMBLY SMdiagnostics AUTHORIZATION dbo FROM 'C:\Windows\Microsoft.NET\
CREATE ASSEMBLY [System.Web] AUTHORIZATION dbo FROM 'C:\Windows\Microsoft.NET\
--CREATE ASSEMBLY [System.Runtime.Serializat
CREATE ASSEMBLY [System.IdentityModel.Sele
CREATE ASSEMBLY [System.Messaging] AUTHORIZATION dbo FROM 'C:\Windows\Microsoft.NET\
CREATE ASSEMBLY [Microsoft.Transactions.Br
Please attached Assembly Details form
select * from sys.assemblies
if anyone can let me know how to solve much appreciated.
Many Thanks
Assembly.xlsx
ASKER
My program is working in perfectly in the C# but when it's transferred to CLR (created complete different project using C# CLR) not working.
C# reader is working perfectly in the console application.
C# reader is working perfectly in the console application.
Create some much simpler CLR SP which reads data and returns just the number of rows read. If this will work for you then you may go to the more complex next step.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Great you've fixed the problem. Was it caused by the missed SqlContext.Pipe.Send?
ASKER
Yes Reader need to pass
Like this
SqlContext.Pipe.Send(reade r);
Like this
SqlContext.Pipe.Send(reade
ASKER
I found the issue and resolved
You should test your C# code outside SQL Server first. Then you have to test it under the user which is used to run SQL Server service and then you are safe to implement it as CLR SP in SQL Server.
I suppose you are not using the C# code to read data from the SQL Server on which is the SP implemented as it would be rather contraproductive.