Error: Object reference not set to an instance of an object

  • Thread starter Thread starter VA_er
  • Start date Start date
V

VA_er

Guest
The goal is: check existence of username. I have been dealing with the issue for multiple days. Spend more than 30 minutes each day, still have not figured out the code which works.

It is difficult for me (non IT professional, not familiar with syntax), keep modifying the code, now the new error "Object reference not set to an instance of an object" comes up for below code.

Thanks.

GlobalVariables.Username = lblUsername.Text;
//There is code: lblUsername.Text = Environment.UserName;
GlobalVariables.ConnectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)}; Dbq=" + @"C:\Users\JC\Desktop\Test.accdb;";


bool SuccessfulConnection;


OdbcConnection myConn = new OdbcConnection(GlobalVariables.ConnectionString);
OdbcCommand cmd = new OdbcCommand { Connection = myConn };
cmd.CommandText = "SELECT UserName FROM User_access WHERE UserName = '" + GlobalVariables.Username + "'";

myConn.Open();
var result = cmd.ExecuteScalar(); //Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.
if (result!= null)
{
SuccessfulConnection = true;
}
else
{
SuccessfulConnection = false;
}

Continue reading...
 
Back
Top