How to overcome Null Reference Exeption?

Hello,

I’ve come across with a problem whilst following the ‘Build your own ASP.NET 4 website’ Book. In Chapter 9 I am following the code to create the Admin Tools and when I run the code and try to select an employee, nothing happens. On visual web developer it says that the next code that is suppose to run is:

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

This is the code for the select button:
aspx

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  <h1>Admin Tools</h1>
  <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" />
  </p>

aspx.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;
        }
        catch
        {
            // Display error message
            dbErrorLabel.Text =
                "Error loading the employee details!<br />";
        }
        finally
        {
            // Close the connection
            conn.Close();
        }
    }

Thank You in advance!

usually you would wait at least a day to bump your own thread

also, i’ve asked the mods to move this thread to the .net forum, as it doesn’t really seem to be a database problem

You need to check your columns against DBNull.Value

Example:

firstnameTextBox.Text = (reader["FirstName"] != DBNull.Value) ? reader["FirstName"].ToString() : String.Empty;