Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object reference not set to an instance of an object #5

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.

like image 866
tom Avatar asked Dec 09 '22 16:12

tom


1 Answers

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.

like image 150
Jon Skeet Avatar answered Dec 23 '22 11:12

Jon Skeet