从另一个表单中搜索datagridview时出现[C#] nullreferenceexception [英] [C#] nullreferenceexception when searching in datagridview from another form

查看:89
本文介绍了从另一个表单中搜索datagridview时出现[C#] nullreferenceexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道造成这个问题的原因。我在帐户表单中有两个表单(搜索表单和帐户表单),有一个搜索按钮可以打开搜索。

打开搜索表单的代码:

I do not know what is causing this problem. I have 2 forms (Search form & Accounts form) in Accounts Form there is a Search button which opens the Search.
Code for opening the search form:

private void btnSearch_Click(object sender, EventArgs e) {
            SearchForm sf = new SearchForm();
            sf.ShowDialog();
        }





在搜索表单中,这些是代码:



In the search form, these are the codes:

public partial class SearchForm : Form {
        public static string search;
        private readonly AcctsForm rf;

        public SearchForm(AcctsForm actform) {
            rf = actform;
        }

        public SearchForm() {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, System.EventArgs e) {
            search = textBox1.Text;
            rf.SearchedList("SELECT * FROM Employees_Info WHERE Emp_ID = @Search");
            this.Close();
        }
    }





来自账户表格(搜索确认按钮引用SearchedList事件):



From Accounts Form (The SearchedList event is referenced by the Search OK button):

public void SearchedList(string x) {
            try {
                using (sqlSelect = new SqlDataAdapter(x, conn)) {
                    sqlSelect.SelectCommand.Parameters.AddWithValue("@Search", SearchForm.search);
                    sqldt = new DataTable();
                    sqlSelect.Fill(sqldt);
                    if (sqldt.Rows.Count >= 1) {
                        dgvEmployees.DataSource = sqldt;
                    } else {
                        MessageBox.Show("not results found.");
                    }
                }
            } catch (SqlException err) {
                MessageBox.Show(err.Message);
            } finally {
                if (conn.State == ConnectionState.Open) {
                    conn.Close();
                }
            }
        }





我尝试了什么:



当我在搜索文本框中搜索未在数据库中注册的值/数据时,找不到结果消息框可以正常工作,但是当正确的时候输入它会给出NullReferenceException。我已经谷歌搜索了所有内容并尝试实例化帐户论坛,但它没有做任何事情(修饰符已经设置为公共)。有任何想法吗?提前谢谢谁能提供帮助!



What I have tried:

When I search a value/data in the search textbox that is not registered in the database, the "no results found" messagebox works, but when correct a correct one is inputted it will give a NullReferenceException. I already googled everything and tried "instantiating" the Accounts Forum but it doesn't do anything (Modifiers are already set to Public). Any ideas? Thank you in advance whoever can help!

推荐答案

谷歌搜索这不会给你任何答案。您必须在此代码上使用调试器,并在抛出异常时查找为null的变量。然后返回代码并找出变量为何包含null的原因。


我们不能为你做这个。我们无法运行代码而且我们没有您正在使用的数据。
Googling this isn't going to give you any answers. You MUST use the debugger on this code and find the variable that is null when the exception is thrown. Then go backwards through the code and find out why the variable contains null.

We can't do this for you. We can't run the code and we don't have the data you're code is using.


据我所知,当您创建SearchForm时,您调用默认值构造函数,而不是具有定义为参数的帐户形式的构造函数。稍后在代码中,您尝试使用变量 rf ,这是 null ,因为您使用了错误的构造函数。 />


如果btnSearch位于帐户表单上,请尝试以下内容

As far as I can see, when you create the SearchForm, you call the default constructor, not the one with account form defined as parameter. Later in the code you try to use the variable rf, which is null because you used the wrong constructor.

If the btnSearch is located on the account form, try the following
private void btnSearch_Click(object sender, EventArgs e) {
            SearchForm sf = new SearchForm(this);
            sf.ShowDialog();
        }



请记住使用调试器来查看代码的执行方式。


Remember to use the debugger to see using how the code is executed.


这篇关于从另一个表单中搜索datagridview时出现[C#] nullreferenceexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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