C#中的异常databaseSystem.NullReferenceException [英] Exception databaseSystem.NullReferenceException in c#

查看:86
本文介绍了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.[^]


添加到其他答案:

"System.NullReferenceException:对象引用未设置为对象的实例."
这仅表示您正在尝试访问null的对象的属性.

使用Visual Studio DEBUGGER会告诉您确切的行和为null的对象,从而引发错误.
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.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆