2

I have ASP.net website. In App_Code folder I have class that has extension methods. When running this site from visual studio, everything works. However on my IIS 7 server, i received the following error:

CS1061: 'System.Data.SqlClient.SqlDataReader' does not contain a definition for 'SafeGetString' and no extension method 'SafeGetString' accepting a first argument of type 'System.Data.SqlClient.SqlDataReader' could be found (are you missing a using directive or an assembly reference?)

But the file containing this extention methods is in App_Code folder. Am I missing something important?

ExtentionMethods Class:

public static class ExtentionMethods
{
    public static string SafeGetString(this SqlDataReader reader, int colIndex)
    {
        if (!reader.IsDBNull(colIndex))
            return reader.GetString(colIndex);
        return "NULL VALUE";
    }

    public static int SafeGetInt32(this SqlDataReader reader, int colIndex)
    {
        if (!reader.IsDBNull(colIndex))
            return reader.GetInt32(colIndex);
        return -1;
    }

    public static DateTime SafeGetDateTime(this SqlDataReader reader, int colIndex)
    {
        if (!reader.IsDBNull(colIndex))
        {
            try
            {
            }
            catch
            {
                return new DateTime(1800, 1, 1);
            }
        }
        return new DateTime(1800, 1, 1);
    }
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
seeker
  • 3,255
  • 7
  • 36
  • 68
  • Is it in the parent folder of the app_code folder or in a sub-directory? – Tim Schmelter Jul 18 '12 at 21:37
  • 2
    One important thing you may be missing is that it's better to use Web Application Projects instead of Web Sites. Web Sites behave strangely in many cases, perhaps including this one. – John Saunders Jul 18 '12 at 21:38
  • 1
    @seeker: Then this might be helpful: http://stackoverflow.com/questions/155105/how-come-classes-in-subfolders-in-my-app-code-folder-are-not-being-found-correct – Tim Schmelter Jul 18 '12 at 21:47

2 Answers2

2

Thanks to @AnthonyWJones: How come classes in subfolders in my App_Code folder are not being found correctly?

You need to add codeSubDirectories to your compilation element in web.config

<configuration>
    <system.web>
      <compilation>
         <codeSubDirectories>
           <add directoryName="Extensions"/>
         </codeSubDirectories>
      </compilation>
   </system.web>
</configuration>
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

did you add 'this' to your extension method parameter? is the class and method public static? without seeing the code I'm just stabbing in the dark here.

your first argument must be the type being extended, if I recall correctly.

bluelightning1
  • 279
  • 2
  • 6