Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 .NET Inside SQL Server (2005)
 Data access not allowed in current context?
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Posting Yak Master

129 Posts

Posted - 04/12/2012 :  11:27:17  Show Profile  Reply with Quote
Hi. totally new to the SQL CLR but extremely happy with the progress I am making.

I am trying to create a UDF. However everytime I try to execute the CLR UDF I get this error:


Msg 6260, Level 16, State 1, Line 2
An error occurred while getting new row from user defined Table Valued Function :
System.InvalidOperationException: Data access is not allowed in this context. Either the context is a function or method not marked with DataAccessKind.Read or SystemDataAccessKind.Read, is a callback to obtain data from FillRow method of a Table Valued Function, or is a UDT validation method.
at System.Data.SqlServer.Internal.ClrLevelContext.CheckSqlAccessReturnCode(SqlAccessApiReturnCode eRc)
at System.Data.SqlServer.Internal.ClrLevelContext.AllocateNativeRequest(SmiEventSink sink, UrtCacheOptions ucoOption, Char* pwchSql, Int32 cwchSql, UrtCommandType uctType, UInt64 cbMaxSize, UInt32 cFields, UrtNativeRequest** ppNativeRequest, SqlTaskMem& stmMemory)
at Microsoft.SqlServer.Server.InProcRecordBuffer..ctor(ClrLevelContext clrlvlctxt, SmiExtendedMetaData[] columnMetaData, SmiEventSink eventSink)
at System.Data.SqlServer.Internal.ClrLevelContext.CreateRecordBuffer(SmiExtendedMetaData[] rgsmdDefinition, SmiEventSink eventSink)
at Microsoft.SqlServer.Server.SqlDataRecord..ctor(SqlMetaData[] metaData)
at DDSQLCLRWrapper.DegreeDaysSQLWrapper.<GetDegreesDayData>d__0.MoveNext()

Not sure where to go from here? The assembly is marked as EXTERNAL_ACCESS and ive also tried UNSAFE but but result in the same behavior.



public static void FillDegreeDayRow(object item, out SqlString date, out SqlString value)
KeyValuePair<string, string> kvp = (KeyValuePair<string, string>)item;
date = new SqlString(kvp.Key);
value = new SqlString(kvp.Value);

[SqlFunction(DataAccess = DataAccessKind.Read, SystemDataAccess = SystemDataAccessKind.Read, FillRowMethodName = "FillDegreeDayRow", TableDefinition = "date nvarchar(200) null, value nvarchar(200) null")]
public static IEnumerable GetDegreesDayData(......)

DatedDataSet hddData = GetWebResponse(....);

SqlMetaData dateMeta = new SqlMetaData("Date", System.Data.SqlDbType.VarChar, 20);
SqlMetaData dateValue = new SqlMetaData("Value", System.Data.SqlDbType.VarChar, 20);
SqlDataRecord dataRecord = new SqlDataRecord(new[] { dateMeta, dateValue });

for (int counter = 0; counter < hddData.Values.Count; counter++)
yield return new KeyValuePair<string, string>(hddData.Values[counter].FirstDay.ToString(), hddData.Values[counter].Value.ToString());

I tried obtaining all the data, put it into a local collection then iterate through that in the same manner but still get the error.

if however I just do a return of the collection, I get the result displayed but only the single result, not the multiple results that exist.

Edited by - tech_1 on 04/12/2012 11:33:08

Posting Yak Master

129 Posts

Posted - 04/12/2012 :  12:34:17  Show Profile  Reply with Quote

silly mistake on my part. I needed to remove the SqlMetaData and SqlDataRecord and all is now working.
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000