Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IENumerator Error

Status
Not open for further replies.

Bell1991

Programmer
Aug 20, 2003
386
US
I am getting the following error:

Cannot find the tye 'IENumerator', becuase it does not exist or you do not have permission

Any ideas?

I am attempting to get an example i found online to work.
Below is the code:

using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.IO;
using System.Collections;
using System.Security.Principal;

namespace SqlServerProject1CSharp
{
// The record that holds each row returned by the TVF.
public class FileRecord
{
public FileStream fContents;
public string fName;

public FileRecord(string fn, FileStream fc)
{
this.fName = fn;
this.fContents = fc;
}
}

public class MultiFileAccess
{
public MultiFileAccess()
{
}

/// <summary>
/// The fill row method that cracks the FileRecord and returns the individual columns.
/// </summary>
/// <param name="Obj"></param>
/// <param name="FName"></param>
/// <param name="FContents"></param>
public static void FillRow(object obj, ref SqlString fName, ref SqlBytes fContents)
{
if ( obj != null )
{
FileRecord record1 = (FileRecord)obj;
fName = (SqlString)record1.fName;
fContents = new SqlBytes(record1.fContents);
}
else
{
fName = SqlString.Null;
fContents = SqlBytes.Null;
}
}

/// <summary>
/// Root method that returns MultiFileLoader which does the bulk of the work.
/// </summary>
/// <param name="PathName"></param>
/// <param name="Pattern"></param>
/// <returns></returns>
[SqlFunction(FillRowMethodName = "FillRow", TableDefinition = "FName nvarchar(300), FContents varbinary(max)")]
public static IEnumerator GetFilesInDirectory(string pathName, string pattern)
{
return new MultiFileLoader(pathName, pattern);
}
}

// The core of the TVF's implementation that implements IEnumerator.
public class MultiFileLoader : IEnumerator
{
private WindowsIdentity callerIdentity;
private int currentFile;
private FileStream currentFileStream;
private string[] fileNames;

public MultiFileLoader(string pathName, string pattern)
{
this.currentFile = -1;
this.callerIdentity = SqlContext.WindowsIdentity;
WindowsImpersonationContext context1 = this.callerIdentity.Impersonate();
try
{
this.fileNames = Directory.GetFiles(pathName, pattern);
}
catch
{
this.fileNames = null;
}
finally
{
if ( context1 != null )
{
context1.Undo();
}
}
}

/// <summary>
/// Retrieves the FileStream for the current file as pointed to by the CurrentFile array index.
/// Constructs a FileRecord with the current file's name and the contents (as FileStream) and
/// returns it.
/// Impersonates caller before opening the file and unimpersonates after getting the filestream.
/// </summary>
public object Current
{
get
{
WindowsImpersonationContext context1 = this.callerIdentity.Impersonate();
try
{
this.currentFileStream = new FileStream(this.fileNames[this.currentFile], FileMode.Open);
}
catch
{
this.currentFileStream = null;
}
finally
{
if ( context1 != null )
{
context1.Undo();
}
}
return new FileRecord(this.fileNames[this.currentFile], this.currentFileStream);
}
}

/// <summary>
/// Advances the CurrentFile position in the FileNames array.
/// Also makes sure to close the currently open FileStream before advancing to the next File.
/// </summary>
/// <returns></returns>
public bool MoveNext()
{
if ( this.fileNames == null )
{
return false;
}
if ( this.currentFile < (this.fileNames.Length - 1) )
{
this.currentFile++;
if ( this.currentFileStream != null )
{
this.currentFileStream.Close();
}
return true;
}
if ( this.currentFileStream != null )
{
this.currentFileStream.Close();
return false;
}
return false;
}

public virtual void Reset()
{
this.currentFile = -1;
}
}
}
 
Try replacing this

Code:
public static IEnumerator GetFilesInDirectory(string pathName, string pattern)
{
return new MultiFileLoader(pathName, pattern);
}

with

Code:
public static MultiFileLoader GetFilesInDirectory(string pathName, string pattern)
{
return new MultiFileLoader(pathName, pattern);
}

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
Now i have getting the same error as above except for: MultiFileLoader does not exists or you do not have permission
 
Do you get this in runtime or designtime? If in runtime, on exactly which line does the code throw the exception?

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
I am not really that familiar with how to debug the code. I know when i "build" i get no errors. Then when i go to deploy it is when i get the error..

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top