0

How to pass value entered by User while login to another form without having System.NullReferenceException ERROR ??

I'm Using a Class to help in passing the value from one form to another, the class code is :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentFollowUp
{
    public class Student
    {
        public string username { get; set; }
        public string password { get; set; }
    }
}

and then i made a login page where the user can enter his username and password and i made the connection with the database and when i run the code below i get this sentence :"An unhandled exception of type 'System.NullReferenceException' occurred in StudentFollowUp.exe

Additional information: Object reference not set to an instance of an object."

Here is the code and thank in advance :

private void loginlable_click(object sender, EventArgs e)
        {
            using (SqlConnection Connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\StudentFollowup.mdf;Integrated Security=True;"))
            {
                SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) From Users Where Username = '" + username.Text + "' and Password = '" + password.Text + "'", Connection);
                DataTable dt = new DataTable();

                StudentInfo.username = username.Text;
                StudentInfo.password = password.Text;

                sda.Fill(dt);

                if (dt.Rows[0][0].ToString() == "1")
                {
                    this.Hide();
                    Main ss = new Main();
                    ss.Show();
                }

                else
                {
                    MessageBox.Show("Please Check Your Username and Password");
                }       

            }
}
  • And just a hint, always when you have a null reference exception, check what exactly is the null object in the debugger, my guess is that your DataTable is null when you try to call ToString() inside the if. – Rogério Carvalho Batista Dec 14 '17 at 17:23
  • 1
    `\''; DROP TABLE users; --` – Vadzim Dvorak Dec 14 '17 at 17:27
  • Debug your code and you will find what object Is null. It could be StudentInfo. It could be dt.Rows[0][0] – Ezin82 Dec 14 '17 at 17:28
  • Never concatenate user input into sql statements. [Always use parameters.](https://stackoverflow.com/questions/4892166/how-does-sqlparameter-prevent-sql-injection) Also, [never store passwords as plain text in your database.](https://stackoverflow.com/a/47691482/3094533) – Zohar Peled Dec 14 '17 at 17:32
  • read bobby-tables.com .. and then throw all this code away and read a good tutorial on Entity Framework, and never write an SQL statement into a string in your code again (please) – Caius Jard Dec 14 '17 at 17:36
  • The compiler show me that the null objects are at the code lines : StudentInfo.username = username.Text; StudentInfo.password = password.Text; – O.Andonese Dec 14 '17 at 17:43

0 Answers0