0

I've tried searching around, but most of the answers were about instantiating a list that doesn't exist or otherwise isn't exactly what I'm looking for. Most of the questions I've come across weren't using the "new" keyword.

Here's what I've got:

using (SqlCmd cmd = new SqlCmd("SELECT * FROM [User] WHERE Login = @Login", false))
{
  cmd.AddIString....
}

My problem is on that first line. Visual Studio is fine with it until I try to run it. Then it throws an error Object reference not set to an instance of an object. It threw the error even when it looked like:

using (SqlCmd cmd = new SqlCmd(
  "SELECT * " +
  "FROM [User] " + 
  "WHERE Login = @Login", false))
{
  // Code here...
}

Any ideas? I've rewritten it, looked at other methods that do the exact same thing, used Google, etc for about 2 hours now with no real result. Closing Visual Studio didn't do anything either, except cause the same problem on code that was working previously.

EDIT: I did say first line, but here it is a bit more clearly (Line 74):

Line 72:     {
Line 73:         //using (SqlCmd cmd = new SqlCmd("SELECT * FROM [User]  WHERE Login = @Login", false))
Line 74:         using (SqlCmd cmd = new SqlCmd("SELECT * FROM [User] WHERE Login = @Login", true))
Line 75:         {
Line 76:             cmd.AddIString("@Login", 100, login);
ahwm
  • 672
  • 9
  • 23
  • 7
    Which line throws the error? Post the stack trace. – Blorgbeard Dec 10 '12 at 22:31
  • 11
    `SqlCmd` is not a framework class - if you've created it yourself look in the class constructor. – D Stanley Dec 10 '12 at 22:37
  • Where does SqlCmd come from? – Adam Straughan Dec 10 '12 at 22:37
  • `SqlCmd` is part of an assembly that our websites use. This is the only one it broke in, so was just checking if there was something I was missing. – ahwm Dec 10 '12 at 22:46
  • Your two initial 'attempts' are exactly the same, down to the compiler interning of concatenated strings. – Zev Spitz Dec 10 '12 at 22:49
  • 3
    Do you have access to the source code of SqlCmd? If not, use a decompiler such as Resharper Reflector or ILSpy to see what's going on. – Zev Spitz Dec 10 '12 at 22:51
  • @ZevSpitz Exactly. But just on the chance something wasn't concatenating right, I re-wrote it on a single line. – ahwm Dec 10 '12 at 22:52
  • I don't think it would really matter what the source code is doing as it's the exact same assembly in every website, and this is the only one that busted. – ahwm Dec 10 '12 at 22:54
  • If the exception is raised in line 74, since the `new` expression is never null, the `SqlCmd` constructor is probably guilty. It's not impossible for an assembly to work in other projects, yet raise an exception here. It'd be useful if you had its source code. – Theodoros Chatzigiannakis Dec 10 '12 at 23:03
  • The thing is it was working when I started, and it is working on other statements the exact same way. I believe we do have the source code, but our lead developer is the one with access to it. If I can't find an answer then I'll talk to him tomorrow about it. I just wanted to make sure I wasn't missing something obvious, though. – ahwm Dec 10 '12 at 23:11
  • Welcome to Stack Overflow! Almost all cases of NullReferenceException are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Dec 10 '12 at 23:45
  • Yeah, that was one of the first things I came across. But it didn't seem to fit my situation since what I had was working at one point with only minimal adjustments to make it work again. It's entirely probable I could replace it with a working base and have it still fail because I haven't figured out the root cause of the problem. But the exact same code _does_ work on the live version. – ahwm Dec 10 '12 at 23:52

0 Answers0