Embedded Resource File in Code Library called from ASP.Net web pag

Discussion in 'ASP .Net' started by Jay Pondy, Feb 16, 2009.

  1. Jay Pondy

    Jay Pondy Guest

    I have a class library project that is being used in an ASP.Net web site.

    In the library class I have several files containing SQL statements (in a
    sql subdirectory) with their 'Build Action' property set to Embedded Resource.

    I use the following code to retrieve the SQL statements from these embedded
    resources at runtime:

    protected string SQLFetch(string fileName)
    {
    Assembly assembly = Assembly.GetEntryAssembly();

    Trace.Assert(assembly != null);

    string assemblyName = assembly.GetName().Name;
    string resourceName = string.Format("{0}.sql.{1}", assemblyName, fileName);
    // Assembly.directory.filename

    Trace.Assert(resourceName != null);

    Stream stream = assembly.GetManifestResourceStream(resourceName);
    string sql = string.Empty;

    if (stream == null)
    throw new ApplicationException(string.Format("Missing resource '{0}'.",
    resourceName));
    else
    {
    using (StreamReader rdr = new StreamReader(stream))
    {
    sql = rdr.ReadToEnd();
    }
    return sql;
    }
    }

    When I attempt to call into this library from an ASP.Net web site the
    assembly is being returned as NULL.

    What do I need to do to either correct this problem or work around it?
     
    Jay Pondy, Feb 16, 2009
    #1
    1. Advertising

  2. Jay Pondy

    Jay Pondy Guest

    Do it this way:

    JobItems junk = new JobItems();

    Assembly assembly = Assembly.GetAssembly(junk.GetType());




    "Jay Pondy" wrote:

    > I have a class library project that is being used in an ASP.Net web site.
    >
    > In the library class I have several files containing SQL statements (in a
    > sql subdirectory) with their 'Build Action' property set to Embedded Resource.
    >
    > I use the following code to retrieve the SQL statements from these embedded
    > resources at runtime:
    >
    > protected string SQLFetch(string fileName)
    > {
    > Assembly assembly = Assembly.GetEntryAssembly();
    >
    > Trace.Assert(assembly != null);
    >
    > string assemblyName = assembly.GetName().Name;
    > string resourceName = string.Format("{0}.sql.{1}", assemblyName, fileName);
    > // Assembly.directory.filename
    >
    > Trace.Assert(resourceName != null);
    >
    > Stream stream = assembly.GetManifestResourceStream(resourceName);
    > string sql = string.Empty;
    >
    > if (stream == null)
    > throw new ApplicationException(string.Format("Missing resource '{0}'.",
    > resourceName));
    > else
    > {
    > using (StreamReader rdr = new StreamReader(stream))
    > {
    > sql = rdr.ReadToEnd();
    > }
    > return sql;
    > }
    > }
    >
    > When I attempt to call into this library from an ASP.Net web site the
    > assembly is being returned as NULL.
    >
    > What do I need to do to either correct this problem or work around it?
    >
    >
     
    Jay Pondy, Feb 16, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. craig dicker
    Replies:
    9
    Views:
    704
    Juan T. Llibre
    Jul 7, 2005
  2. Dirc Khan-Evans
    Replies:
    1
    Views:
    933
    Karl Seguin
    Oct 17, 2005
  3. Heinrich Moser
    Replies:
    1
    Views:
    477
    Heinrich Moser
    Mar 27, 2008
  4. Savas Ates
    Replies:
    1
    Views:
    304
    Patrice
    Aug 17, 2004
  5. Roundy
    Replies:
    6
    Views:
    225
    teknohippy
    Feb 4, 2005
Loading...

Share This Page