SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:
    Code:
    comm.Parameters["@EmployeeID"].Value =
                employeesList.SelectedItem.Value;
    This is the code for the select button:
    aspx
    PHP Code:
    <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
    PHP Code:
    protected void selectButton_Click(object senderEventArgs 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!

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,263
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by vpa View Post
    No one?
    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
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    You need to check your columns against DBNull.Value

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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •