9
Answers

Exception database

sawass sawass

sawass sawass

13y
1.6k
1
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.

Answers (9)
0
Jiteendra Sampathirao

Jiteendra Sampathirao

NA 6.9k 1.5m 13y

Hi,
Trace the every line of the code...then you can definitely found where is the problem is?
I think the problem is when assigning the value to column name....
value might be null and that column doen't accept the null...


0
sawass sawass

sawass sawass

NA 36 19.5k 13y

Attachment pagaa edit.rar

Here is my project
When debugging, you access the menu 'File' then 'Connection'.
Login: hammami
Password: aa
You access the menu 'User'and then 'modify user '.
I have the same exeption. Can you help me et thanks in advance for your help.
0
Sam Hobbs

Sam Hobbs

NA 28.7k 1.3m 13y
Except I should have looked at the code like Andrew did. I am embarrassed that I did not.
0
Andrew Fenster

Andrew Fenster

NA 2.2k 1.5m 13y
I'm with Sam on this one.

You have this code, which is blowing up:

OdbcCommand cmd1 = new OdbcCommand("update utilisateur set login=?,mot_de_passe=?,niveau=? where login=?", cn);
cmd1.Parameters.Add( "mot_de_passe", pass_new.Text);



cmd1 isn't null.  So it seems likely that cn or pass_new.Text is null.  I don't see the code where these are assigned values, so I can't tell.  Only you can tell.  You should be able to figure it out as you step through with the debugger.  

0
Sam Hobbs

Sam Hobbs

NA 28.7k 1.3m 13y
Either cmd1 is not allocated or pass_new is not. When you get the error in the debugger you can see which one is not allocated. It is easy to determine which one is the problem and I think the guys here want you to figure it out. When you determine which one is the problem then you know you can rely on the guys here to eagerly help some more.
0
sawass sawass

sawass sawass

NA 36 19.5k 13y
Thanks for your reply .
the DSN is my machine  is  valid.What is the object null.The exception starts with the line:" 
cmd1.Parameters.Add( "mot_de_passe", pass_new.Text);"
Will you tell me where I must correct in my code and thank you very much.
0
Andrew Fenster

Andrew Fenster

NA 2.2k 1.5m 13y
The error indicates you are trying to do something with an object, but the ojbect is null.

You should easily be able to figure out which line the exception occurs on and which object is null just by stepping through the code with the debugger. 

Do you know how to set a breakpoint and step through the code line by line?  If not, you should have someone demonstrate, because being able to walk through your code line by line and look at all the values is a very basic, very important skill to have.  
0
Abhimanyu K Vatsa

Abhimanyu K Vatsa

NA 50.9k 12.4m 13y
this error usually appears when you use database entities/attributes incorrectly on form. check the database and its entities/attributes with your code-behind. or post the error code?
0
Muralidharan Deenathayalan

Muralidharan Deenathayalan

NA 11.9k 1.5m 13y

where you're getting the null reference error?
Do you have valid DSN is your machine ?