Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C#
private void btnSearch_Click(object sender, EventArgs e) {
            SearchForm sf = new SearchForm();
            sf.ShowDialog();
        }


In the search form, these are the codes:
C#
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();
        }
    }


From Accounts Form (The SearchedList event is referenced by the Search OK button):
C#
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();
                }
            }
        }


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!
Posted
Updated 15-Jan-17 17:45pm

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.
 
Share this answer
 
Comments
Member 12951338 15-Jan-17 22:25pm    
The NullReferenceException is at "rf.SearchedList("SELECT * FROM Employees_Info WHERE Emp_ID = @Search");" when you click the OK button. It also throws the same error when using any actions referencing to "rf" like rf.close(), so I'm wondering why it does that.
Dave Kreskowiak 15-Jan-17 23:43pm    
Because you never set rf to be anything before that code was called.

The debugger would have easily told you that.
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900