" 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.
Is anyone can help me please ant thnks for your reply.
Jiteendra SampathiraoPosted Jun 8, 2011, 12:42 PM
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...
sawass sawassPosted Jun 8, 2011, 10:03 AM
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.
Sam HobbsPosted Jun 7, 2011, 4:40 PM
Andrew FensterPosted Jun 7, 2011, 4:23 PM
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.
Sam HobbsPosted Jun 7, 2011, 4:18 PM
sawass sawassPosted Jun 7, 2011, 2:23 PM
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.
Andrew FensterPosted Jun 7, 2011, 12:39 PM
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.
Abhimanyu K VatsaPosted Jun 7, 2011, 10:16 AM
Posted Jun 7, 2011, 9:54 AM
Do you have valid DSN is your machine ?