使用comboBox的空例外

时间:2015-02-08 00:04:55

标签: c# combobox

我正在尝试将从Main Form传递的字符串列表中的数据显示到此表单中,但问题是我一直在获取" 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);
    }

WelcomePage表单代码:

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

然后它到达HomePage Form:

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

然后从那里进入我想要使用的comboBox数据的表单。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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