Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Object reference not set to an instance of an object

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

Scheduled Pinned Locked Moved C#
csharphelp
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LAPEC
    wrote on last edited by
    #1

    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

    F L D 3 Replies Last reply
    0
    • L LAPEC

      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

      F Offline
      F Offline
      fjdiewornncalwe
      wrote on last edited by
      #2

      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.

      L 1 Reply Last reply
      0
      • L LAPEC

        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

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • L LAPEC

          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

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          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[^]

          1 Reply Last reply
          0
          • F fjdiewornncalwe

            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.

            L Offline
            L Offline
            LAPEC
            wrote on last edited by
            #5

            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 =
            
            R R 2 Replies Last reply
            0
            • L LAPEC

              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 =
              
              R Offline
              R Offline
              RobCroll
              wrote on last edited by
              #6

              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."

              1 Reply Last reply
              0
              • L LAPEC

                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 =
                
                R Offline
                R Offline
                Rob Philpott
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups