Search code examples
c#streamreaderargumentnullexception

streamReader ArgumentNullException


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();

Solution

  • 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")