NullReferenceException

NullReferenceException

本文介绍了C#中的异常databaseSystem.NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我有例外:
"System.NullReferenceException:对象引用未设置为对象的实例."
当我调试此代码时:


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();
        }


有谁能帮助我,请ant thnks给予答复.

调试器在此行中停止

rs = comm.ExecuteReader();


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

the debugger stops in this line

rs = comm.ExecuteReader();

推荐答案

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


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



这篇关于C#中的异常databaseSystem.NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 16:06