4
sUsername.Trim();
sPassword.Trim();
string ConnectionString = WebConfigurationManager.ConnectionStrings["dbnameConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(ConnectionString);

Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Any ideas? I don't understand the error.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
tom
  • 81
  • 1
  • 2
  • 5
  • 2
    Well, I do understand it, but you miss line references. Where does the error occur? – TomTom Mar 22 '10 at 11:21
  • 1
    WebConfigurationManager.ConnectionStrings["dbnameConnectionString"] returned null – tom Mar 22 '10 at 11:23
  • 2
    Two upvotes for this question? Seriously? – Kirk Broadhurst Mar 22 '10 at 11:37
  • 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 Apr 04 '14 at 17:27
  • Will someone please close this as a duplicate of http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net? – John Saunders May 15 '14 at 02:43

5 Answers5

9

Well, you haven't shown which line it occurs on. It suggests that one of these occurred:

  • sUsername was null
  • sPassword was null
  • WebConfigurationManager.ConnectionStrings["dbnameConnectionString"] returned null

Btw, calling Trim() as a statement on its own like that is pointless. Strings are immutable - Trim() returns the trimmed version. You want something like:

sUsername = sUsername.Trim();
sPassword = sPassword.Trim();

... but only after checking whether they're null or not.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
4

Well, I do understand it, but you miss line references. Where does the error occur?

Line 30:         sUsername.Trim();
Line 31:         sPassword.Trim();
Line 32:         string ConnectionString = WebConfigurationManager.ConnectionStrings["dbnameConnectionString"].ConnectionString;
Line 33:         SqlConnection myConnection = new SqlConnection(ConnectionString);
Line 34:         try

if I assume that sPassword exists - and sUsername... ...then does the ConnectionString "dbNameConnectionString" exist in the web.config? If not- that is null, and the ".ConnectionString" naturally throws that error.

TomTom
  • 61,059
  • 10
  • 88
  • 148
1

It happens because of any one of the variable is NULL. You can check the value of sUserName and sPassword variables during debugging (runtime).

GHP
  • 11
  • 1
1

Line 30 and 31 don't do anything:

sUsername = sUsername.Trim();
sPassword= sPassword.Trim();

Post where the error occurs

RvdK
  • 19,580
  • 4
  • 64
  • 107
0

This just means that you're trying to access a member of a null reference; i.e. one of the variables here is null. Without knowing the line number it's difficult to say which, but I'd guess at either sUsername or sPassword.

Will Vousden
  • 32,488
  • 9
  • 84
  • 95