Click here to Skip to main content
15,891,712 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have l'exception:
" System.NullReferenceException: Object reference not set to an instance of an object."
when I debug this code:


private void bt_valider_Click(object sender, System.EventArgs e)
        {
            OdbcConnection cn = new OdbcConnection("DSN=cp22");
            int rs1;
            try
            {
                cn.Open();
            }
            catch
            {
                MessageBox.Show("Failed to connect to data source");
            }
            finally
            {
                OdbcCommand comm;
                comm = new OdbcCommand("select * from utilisateur where login=?", cn);
                comm.Parameters.Add("login", login.Text);
                OdbcDataReader rs;
                try
                {
                    rs = comm.ExecuteReader();
                    if (rs.Read())
                    {
                        if (rs.GetString(1) == pass.Text)
                        {
                            rs.Close();
                            OdbcCommand cmd1 = new OdbcCommand("update utilisateur set login=?,mot_de_passe=?,niveau=? where login=?", cn);
                           // cmd1.Parameters.Add("login", login_new.Text)
                       cmd1.Parameters.Add( "mot_de_passe", pass_new.Text);
                            if (type_new.SelectedItem.ToString() == "")
                                cmd1.Parameters.Add("type", "u");
                            if (type_new.SelectedItem.ToString() == "Administrateur")
                                cmd1.Parameters.Add("type", "a");
                            if (type_new.SelectedItem.ToString() == "utilisateur")
                                cmd1.Parameters.Add("type", "u");
                            cmd1.Parameters.Add("", login.Text);
                            rs1 = cmd1.ExecuteNonQuery();
                            MessageBox.Show(this, "User modified", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            login.Text = "";
                            pass.Text = "";
                            login_new.Text = "";
                            pass_new.Text = "";
                            
                        }
                        else
                            MessageBox.Show(this, "Invalid old password  ", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                        MessageBox.Show(this, "User does not exist", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception eo)
                {
                   // MessageBox.Show(this, "Error System", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    login.Text = eo.ToString();
                }
            }
            cn.Close();
        }

Is anyone can help me please ant thnks for your reply.

the debugger stops in this line

rs = comm.ExecuteReader();
Posted

Change
comm = new OdbcCommand("select * from utilisateur where login=?", cn);
comm.Parameters.Add("login", login.Text);
To
comm = new OdbcCommand("select * from utilisateur where login=@LI", cn);
comm.Parameters.Add("@LI", login.Text);


While you are there, don't store passwords in clear - it is a big security risk. See Password Storage: How to do it.[^]
 
Share this answer
 
Comments
Monjurul Habib 8-Jun-11 15:40pm    
my 5 for "To" section.
Adding to other answer:

" System.NullReferenceException: Object reference not set to an instance of an object."
This simply means that you are trying to access a property of an object that is null.

Using Visual Studio DEBUGGER will tell you exact line and the object that is null and thus throwing an error.
 
Share this answer
 
Comments
Monjurul Habib 8-Jun-11 15:39pm    
nice advice...my 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900