C#NullReferenceException未被用户代码处理

时间:2013-03-28 21:19:01

标签: c# asp.net sql nullreferenceexception visual-web-developer-2010

对此的任何帮助将不胜感激。每当我运行我的程序时,我得到一个NullReferenceException未被用户代码处理。

因此,当我启动程序时,我会看到一个表单,我应该从名单列表中进行选择,并能够更新详细信息或删除它们,但下拉列表中没有任何内容。

我在以下地址获得了空参考:

comm.Parameters["@EmployeeID"].Value =
employeesList.SelectedItem.Value;

这是CS文件中的选择按钮:

protected void selectButton_Click(object sender, EventArgs e)
{
    // Declare objects
    SqlConnection conn;
    SqlCommand comm;
    SqlDataReader reader;
    // Read the connection string from Web.config
    string connectionString =
        ConfigurationManager.ConnectionStrings[
        "HelpDesk"].ConnectionString;
    // Initialize connection
    conn = new SqlConnection(connectionString);
    // Create command
    comm = new SqlCommand(
        "SELECT FirstName, LastName, Username, Address, City, County, Post Code, " +
        "HomePhone, Extension, MobilePhone FROM Employees " +
        "WHERE EmployeeID = @EmployeeID", conn);
    // Add command parameters
    comm.Parameters.Add("@EmployeeID", System.Data.SqlDbType.Int);
    comm.Parameters["@EmployeeID"].Value =
        employeesList.SelectedItem.Value;
    // Enclose database code in Try-Catch-Finally
    try
    {
        // Open the connection
        conn.Open();
        // Execute the command
        reader = comm.ExecuteReader();
        // Display the data on the form
        if (reader.Read())
        {
            firstnameTextBox.Text = reader["FirstName"].ToString();
            lastnameTextBox.Text = reader["lastName"].ToString();
            userNameTextBox.Text = reader["Username"].ToString();
            addressTextBox.Text = reader["Address"].ToString();
            cityTextBox.Text = reader["City"].ToString();
            countyTextBox.Text = reader["county"].ToString();
            postcodeTextBox.Text = reader["Post Code"].ToString();
            homePhoneTextBox.Text = reader["HomePhone"].ToString();
            extensionTextBox.Text = reader["Extension"].ToString();
            mobilePhoneTextBox.Text = reader["MobilePhone"].ToString();
        }
        // Close the reader 
        reader.Close();
        // Enable the Update button
        updateButton.Enabled = true;
        // Enable the Delete button
        deleteButton.Enabled = true;
    }
    catch
    {
        // Display error message
        dbErrorLabel.Text =
            "Error loading the employee details!<br />";
    }
    finally
    {
        // Close the connection
        conn.Close();
    }
}

这是aspx文件中的代码:

<p>
<asp:Label ID="dbErrorLabel" ForeColor="Red" runat="server" />
Select an employee to update:<br />
<asp:DropDownList ID="employeesList" runat="server" />
<asp:Button ID="selectButton" Text="Select" runat="server"
    onclick="selectButton_Click" />

3 个答案:

答案 0 :(得分:6)

断点:

comm.Parameters["@EmployeeID"].Value =
        employeesList.SelectedItem.Value;

当你运行程序时,请检查以下值:

employeesList.SelectedItem

如果没有选择任何项目,很可能会为空。在使用之前尝试检查空值:

 if (employeesList.SelectedItem != null)
 {
comm.Parameters["@EmployeeID"].Value =
    employeesList.SelectedItem.Value;
 }
 else
 {
// Handle this case
 }

希望这有帮助!

答案 1 :(得分:0)

看起来您没有在下拉列表中添加任何项目。

你有:

<p>
<asp:Label ID="dbErrorLabel" ForeColor="Red" runat="server" />
Select an employee to update:<br />
<asp:DropDownList ID="employeesList" runat="server" />
<asp:Button ID="selectButton" Text="Select" runat="server"
    onclick="selectButton_Click" />

但是下拉列表应该包含以下项目:

<asp:DropDownList ID="employeesList" runat="server">
    <asp:ListItem Text="Item 1" Value="1" Selected="true" />
    <asp:ListItem Text="Item 2" Value="2"/>
</asp:DropDownList>

答案 2 :(得分:0)

首先,您必须在数据库中可用表格的下拉列表中填写数据。

示例代码: 在Page_Load中,

if (!IsPostBack)
{
   ddlName.DataSource=ds;
   ddlName.DataValueField="ID";
   ddlName.DataTextField="NAME";
   ddlName.DataBind();
}