-3
private void FillListBox()
{
    string ConctingString = "Data Source=AHMED_S_MASHALY;Initial Catalog=BookLibrary;Integrated Security=True";
    string CommandString = "SELECT * FROM BookLibraryTable2";

    SqlConnection Conncting = new SqlConnection(ConctingString);

    Conncting.Open();

    SqlCommand CMD = new SqlCommand(CommandString, Conncting);

    SqlDataReader DR1 = CMD.ExecuteReader();

    while (DR1.Read())
    {
        string listboxvalues = DR1.GetString(1);
        listBox1.Items.Add(listboxvalues);
    }
}

这是错误

An unhandled exception of type 'System.NullReferenceException' occurred in BookLibrary.exe

附加信息:Object reference not set to an instance of an object.

错误出现在这一行 >>>>>> listBox1.Items.Add(listboxvalues); <<<<<<

4

1 回答 1

1

发生错误是因为 listBox1 或 listBox1.items 为空。找出哪个最简单的方法是在该行添加一个断点并检查它们的值。

于 2013-12-09T20:16:45.193 回答