C# Object reference not set to an instance of an object
-
Hello Everyone I have created a login form and when I try to login I get error saying Object reference not set to an instance of an object. The error I get is in this line of code...
dtAdapter.SelectCommand.CommandText = "SELECT * FROM Users WHERE ID = '" + txtUserId + "' AND Password = '" + txtPassword + "'";
Can someone tell me please if I'm missing something... thanks in advance kind regards lapeci
-
Hello Everyone I have created a login form and when I try to login I get error saying Object reference not set to an instance of an object. The error I get is in this line of code...
dtAdapter.SelectCommand.CommandText = "SELECT * FROM Users WHERE ID = '" + txtUserId + "' AND Password = '" + txtPassword + "'";
Can someone tell me please if I'm missing something... thanks in advance kind regards lapeci
You're missing a ton of the code relevant to us answering your question. Show us how you initialize your adapter and your command. Also ensure that txtUserId and txtPassword are not null objects.
I wasn't, now I am, then I won't be anymore.
-
Hello Everyone I have created a login form and when I try to login I get error saying Object reference not set to an instance of an object. The error I get is in this line of code...
dtAdapter.SelectCommand.CommandText = "SELECT * FROM Users WHERE ID = '" + txtUserId + "' AND Password = '" + txtPassword + "'";
Can someone tell me please if I'm missing something... thanks in advance kind regards lapeci
Object reference not set to an instance of an object means some reference type variable or property, that is followed by a period, still is null. There are two candidates in the line shown. Most IDE's have debug capabilities to watch the current value, try it and learn to help yourself, we can't guess what exactly you did wrong. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Hello Everyone I have created a login form and when I try to login I get error saying Object reference not set to an instance of an object. The error I get is in this line of code...
dtAdapter.SelectCommand.CommandText = "SELECT * FROM Users WHERE ID = '" + txtUserId + "' AND Password = '" + txtPassword + "'";
Can someone tell me please if I'm missing something... thanks in advance kind regards lapeci
1. Either dtAdapter or dtAdapter is null. 2. txtUserId and txtPassword look like names of textboxes rather than string. If they are textboxes, you will need to add .Text as well. If they are string, you may want to revisit the naming convention guidelines you are using.
"Your code will never work, Luc's always will.", Richard MacCutchan[^]
-
You're missing a ton of the code relevant to us answering your question. Show us how you initialize your adapter and your command. Also ensure that txtUserId and txtPassword are not null objects.
I wasn't, now I am, then I won't be anymore.
Hi Marcus Here is the full code of login button click event...
public partial class ClientLogin : Window { public Client client = new Client(); OleDbConnection conn = new OleDbConnection(); OleDbDataAdapter dtAdapter = new OleDbDataAdapter(); OleDbCommand command = new OleDbCommand(); OleDbDataReader dataReader;// = new OleDbDataReader(); //public TextBox txtIPaddress = new System.Windows.Controls.TextBox(); //public TextBox txtPortNumber = new System.Windows.Controls.TextBox(); public ClientLogin() { InitializeComponent(); conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\AP\_AE\\Desktop\\DTPOS\_ClientServer\\DataBase\\DtposMenu.accdb"; //command.CommandText.ToString(); } public void infoChecker(Client formOne) { this.client = formOne; this.Show(); } // IsNumeric Function static bool IsNumeric(object Expression) { // Variable to collect the Return value of the TryParse method. bool isNum; // Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero. double retNum; // The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent. // The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned. isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum); return isNum; } private void btnLogin\_Click(object sender, RoutedEventArgs e) { // Using Try-Catch-Finally block structure try { if (IsNumeric(txtUserId.Text) & IsNumeric(txtPassword.Text)) { // Open database connection conn.Open(); // Set SQL select command to check User\_ID and User\_Password dtAdapter.SelectCommand.CommandText = "SELECT \* FROM Users WHERE ID = '" + txtUserId.Text + "' AND Password = '" + txtPassword.Text + "'"; dataReader =
-
Hi Marcus Here is the full code of login button click event...
public partial class ClientLogin : Window { public Client client = new Client(); OleDbConnection conn = new OleDbConnection(); OleDbDataAdapter dtAdapter = new OleDbDataAdapter(); OleDbCommand command = new OleDbCommand(); OleDbDataReader dataReader;// = new OleDbDataReader(); //public TextBox txtIPaddress = new System.Windows.Controls.TextBox(); //public TextBox txtPortNumber = new System.Windows.Controls.TextBox(); public ClientLogin() { InitializeComponent(); conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\AP\_AE\\Desktop\\DTPOS\_ClientServer\\DataBase\\DtposMenu.accdb"; //command.CommandText.ToString(); } public void infoChecker(Client formOne) { this.client = formOne; this.Show(); } // IsNumeric Function static bool IsNumeric(object Expression) { // Variable to collect the Return value of the TryParse method. bool isNum; // Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero. double retNum; // The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent. // The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned. isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum); return isNum; } private void btnLogin\_Click(object sender, RoutedEventArgs e) { // Using Try-Catch-Finally block structure try { if (IsNumeric(txtUserId.Text) & IsNumeric(txtPassword.Text)) { // Open database connection conn.Open(); // Set SQL select command to check User\_ID and User\_Password dtAdapter.SelectCommand.CommandText = "SELECT \* FROM Users WHERE ID = '" + txtUserId.Text + "' AND Password = '" + txtPassword.Text + "'"; dataReader =
Hi lapeci, Just as a side note regarding connections. .NET uses a connection pool so there is no need to have a class variable which represents a connection. Best practises recommends connections should be created, used and disposed of on-the-fly. Use a class string variable which represents the connection string by all means. Also your problem is that you need to set the SelectCommand
dtAdapter.SelectCommand = command
"You get that on the big jobs."
-
Hi Marcus Here is the full code of login button click event...
public partial class ClientLogin : Window { public Client client = new Client(); OleDbConnection conn = new OleDbConnection(); OleDbDataAdapter dtAdapter = new OleDbDataAdapter(); OleDbCommand command = new OleDbCommand(); OleDbDataReader dataReader;// = new OleDbDataReader(); //public TextBox txtIPaddress = new System.Windows.Controls.TextBox(); //public TextBox txtPortNumber = new System.Windows.Controls.TextBox(); public ClientLogin() { InitializeComponent(); conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\AP\_AE\\Desktop\\DTPOS\_ClientServer\\DataBase\\DtposMenu.accdb"; //command.CommandText.ToString(); } public void infoChecker(Client formOne) { this.client = formOne; this.Show(); } // IsNumeric Function static bool IsNumeric(object Expression) { // Variable to collect the Return value of the TryParse method. bool isNum; // Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero. double retNum; // The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent. // The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned. isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum); return isNum; } private void btnLogin\_Click(object sender, RoutedEventArgs e) { // Using Try-Catch-Finally block structure try { if (IsNumeric(txtUserId.Text) & IsNumeric(txtPassword.Text)) { // Open database connection conn.Open(); // Set SQL select command to check User\_ID and User\_Password dtAdapter.SelectCommand.CommandText = "SELECT \* FROM Users WHERE ID = '" + txtUserId.Text + "' AND Password = '" + txtPassword.Text + "'"; dataReader =
Slightly off topic, but the code snippet is vulnerable to a form of attack called SQL Injection and leaves a wide open security flaw. A user could stick a user name plus the characters '-- to simply bypass the password check, or something far more malicious. A bit vague description but look it up!
Regards, Rob Philpott.