2

Hey there,
I have a program that uses a sql express local DB. I want to be able to update that DB using the program to run the necessary scripts. A text files has been added as an embedded resource to the project (VS2010), and the file contains text. It fails to open the file though. I get an "ArgumentNullException was unhandled" "Value cannot be null. Parametername: stream" here's the code...

Assembly assem;
StreamReader textReader;
assem = Assembly.GetExecutingAssembly();
//fails at this line below.
textReader = new StreamReader(assem.GetManifestResourceStream("projectName.sqlUpdates.txt"));

tReader.Peek() != -1)
  script = textReader.ReadToEnd();
dave k
  • 1,329
  • 4
  • 22
  • 44

2 Answers2

2

Is 'projectName' the full assembly and namespace where the file sqlUpdates.txt lives? Is the code running your example above in the 'projectName' assembly, or somewhere else?

If the sqlUpdates.txt file lives in a namespace below the projectName assembly, then you need to specify that in the call to GetManifiestResourceStream, ie:

Stream resource = assem.GetManifestResourceStream("projectName.nameSpace.sqlUpdates.txt")
rusty
  • 2,771
  • 1
  • 24
  • 23
  • stupid error on my part, i wasn't case sensitive on the name of the namespace. after looking at the assembly and namespace names and comparing that to what i had, i saw it. thanks – dave k May 05 '11 at 19:23
1

That basically means that

assem.GetManifestResourceStream("projectName.sqlUpdates.txt")

returned null... which it will do if it can't find that resource. Check that it's actually in the assembly, e.g. with Reflector or assem.GetManifestResourceNames().

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    honestly, i'm a fairly new prgrammer so i don't know what all that means... The text file was added by right clicking the project name in the solution explorer, add-new item-text file. then i set it's properties with 'Embedded Resource' and 'copy if newer'. The assembly name, and default namespace are both the same. so it should be in the assembly, right? – dave k May 05 '11 at 19:07
  • I was experiencing the same problems until I read your comment about the "embedded Resource" property. Thanks – Rober Jan 24 '17 at 17:31