comboBox 的空异常

Null exception with comboBox

我正在尝试显示从主窗体传递到此窗体的字符串列表中的数据,但问题是我一直在获取 "System.ArgumentNullException" 即使数据已正确传递到新声明的列表.我错过了什么吗?

       public LoginPage()
    {
        InitializeComponent();
    }
    WelcomePage secondForm = new WelcomePage();
    SqlConnection con;
    DataTable dt1 = new DataTable();
    public static DataRow dRow2 = null;
    public List<string> list = new List<string>();
    private void btnSubmit_Click(object sender, EventArgs e)
    {
        string sql = @"SELECT * FROM [employeeAccount] WHERE [User Name] = @UserName AND [Password] = @Password ";
        using (var cmd = new SqlCommand(sql, con))
        {
            con.Open();
            cmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
            cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
            SqlDataReader reader = cmd.ExecuteReader();
            dt1.Load(reader);
            ListTransfer();
            InputChecker();
            con.Close();
        }
    }
    private void LoginPage_Load(object sender, EventArgs e)
    {
        SqlDataAdapter da = new SqlDataAdapter();
        con = new System.Data.SqlClient.SqlConnection();
        con.ConnectionString = (@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\employeeDatabase.mdf;Integrated Security=True");
    }
    private void InputChecker()
    {
        if (dt1.Rows.Count > 0)
        {
            this.Hide();
            secondForm.Closed += (s, args) => this.Close();
            secondForm.Show();
        }
        else
        {
            MessageBox.Show("Invalid input data!");
        }
    }
    private void ListTransfer()
    {
        SqlDataAdapter da = new SqlDataAdapter("SELECT [Department] FROM [employeeTable]", con);
        DataSet ds = new DataSet();
        da.Fill(ds, "employeeTable");
        //List<string> list = new List<string>();
        foreach(DataRow row in ds.Tables["employeeTable"].Rows)
        {
            list.Add(row["Department"].ToString());
        }
        Department_wise_Employee_Details dep = new Department_wise_Employee_Details(list);
    }

欢迎页面表单代码:

    public WelcomePage()
    {
        InitializeComponent();
    }
    HomePage thirdForm = new HomePage();
    private void btnContinue_Click(object sender, EventArgs e)
    {
        this.Hide();
        thirdForm.Closed += (s, args) => this.Close();
        thirdForm.Show();
    }

然后进入主页表单:

    List<string> list = new List<string>();
    public HomePage()
    {
        InitializeComponent();
    }
    public HomePage(List<string>list)
    {

        InitializeComponent();
        this.list = list;
    }
    private void btn2_Click(object sender, EventArgs e)
    {
        Department_wise_Employee_Details fourthForm = new Department_wise_Employee_Details(new LoginPage().list);
        fourthForm.Show();
    }

从那里你会看到我想要使用的组合框数据的表单。

public HomePage()
{
    InitializeComponent();
}
List<string> list=new List<string>();
  private void ListTransfer()
   {
    SqlDataAdapter da = new SqlDataAdapter("SELECT [Department] FROM [employeeTable]", con);
    DataSet ds = new DataSet();
    da.Fill(ds, "employeeTable");
    List<string> list = new List<string>();
    foreach(DataRow row in ds.Tables["employeeTable"].Rows)
    {
        list.Add(row["Department"].ToString());
    }
   // Department_wise_Employee_Details dep = new Department_wise_Employee_Details(list);
   }
Department_wise_Employee_Details fourthForm = new Department_wise_Employee_Details(list);
private void btn1_Click(object sender, EventArgs e)
{
    fourthForm.Show();
}

从您的代码片段可以明显看出,ListTransfer 中的 dep 实例从未显示。因此,您添加项目的点击事件来自另一个未接收列表参数的实例。