Dorknozzle Ch 10

I have the book ASP.NET 3.5 WEB SITE. I can’t get the view/edit employee to work. (Chapter 10) I have copied the code from the “download chapters” and it still doesn’t work. Am I missing something? I don’t see where there were typos on this chapter

which error do you get?

I don’t get any errors When excuting the page, it is supoose to have 2 working ,ink buttons “view more details about the employee” AND “Edit employees” neither of these work. When I run it, the page looks good, but when clicking on the “Employee Directory” then click on the linkbuttons, nothing happens. It is suppose to let me view/edit the employee. The link changes from blue to purple. Thats it. I am very new at this.

I looked briefly at the code and it looks Ok to me (as it should, of course, since it is an excerpt from the book). Did you do any changes at all to the code (such as chaning the name of a variable or editing the text? Because I feel that if you tried to change anything, maybe you missed a closing quote, or a closing tag, that may be affecting the behaviour.

no, I didn’t change anything at all. In fact, I thought that myself, so I copied the code from the downloaded file, then ran it. It still didn’t work. I’m wondering if there is another file that goes with it, that i am missing. Any clue?

Well, the known typos are listed here http://www.sitepoint.com/books/aspnet3/errata.php

As you can see, there’s nothing about this chapter in particular (which doesn’t mean that typo isn’t there).

I don’t have installed .NET in my web server right now so I can’t test it myself. I think I will move this post to the .NET forum so that you can get help here.

Since they may not have the book, I would suggest that you copy and post the code. Use the [noparse]


[/noparse] tags to do so.

When you do, let me know so I can move the thread to the .NET forum

Maybe you can help me. I tried posting the code with


and I get an error…
“In an effort to stamp out forum spam only members with 5 posts or more can post website links or email addesses” My code doesn’t contain either of these.

Adding a “blank” post (this is my 5th post) I will try to post my code now (my 6th post) Maybe it will now let me…
see below = )

Here is the EmployeeDirctory.aspx


<%@ Page Language="C#" MasterPageFile="~/Dorknozzle.master" AutoEventWireup="true" CodeFile="EmployeeDirectory.aspx.cs" Inherits="EmployeeDirectory" Title="Dorknozzle Employee Directory" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  <h1>Employee Directory</h1>
  <asp:DataList id="employeesList" runat="server" 
    onitemcommand="employeesList_ItemCommand" BackColor="#DEBA84" 
        BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
        CellSpacing="2" GridLines="Both" oneditcommand="employeesList_ItemCommand">
      <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
    <ItemTemplate>
      <asp:Literal ID="extraDetailsLiteral" runat="server"
          EnableViewState="false" />
      Name: <strong><%#Eval("Name")%></strong><br />
      Username: <strong><%#Eval("Username")%></strong><br />
      <asp:LinkButton ID="detailsButton" runat="server"
          Text=<%#"View more details about " + Eval("Name")%>
          CommandName="MoreDetailsPlease"
          CommandArgument=<%#Eval("EmployeeID")%> />
      <br />
      <asp:LinkButton ID="editButton" runat="server"
          Text=<%#"Edit employee " + Eval("Name")%>
          CommandName="EditItem"
          CommandArgument=<%#Eval("EmployeeID")%> />
    </ItemTemplate>
      <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
      <ItemStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
    <EditItemTemplate>
      Name: <asp:TextBox ID="nameTextBox" runat="server"
          Text=<%#Eval("Name")%> /><br />
      Username: <asp:TextBox ID="usernameTextBox" runat="server"
          Text=<%#Eval("Username")%> /><br />
      <asp:LinkButton ID="updateButton" runat="server"
          Text="Update Item" CommandName="UpdateItem"
          CommandArgument=<%#Eval("EmployeeID")%> />
      or
      <asp:LinkButton ID="cancelButton" runat="server"
          Text="Cancel Editing" CommandName="CancelEditing"
          CommandArgument=<%#Eval("EmployeeID")%> />
    </EditItemTemplate>
      <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
    <SeparatorTemplate>
      <hr />
    </SeparatorTemplate>
  </asp:DataList>
</asp:Content>



Here is the code for EmployeeDirectory.aspx.cs



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class EmployeeDirectory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindList();
        }
    }
    protected void BindList()
    {
        // Define data objects
        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader reader;
        // Read the connection string from Web.config
        string connectionString =
            ConfigurationManager.ConnectionStrings[
            "Dorknozzle"].ConnectionString;
        // Initialize connection
        conn = new SqlConnection(connectionString);
        // Create command
        comm = new SqlCommand(
          "SELECT EmployeeID, Name, Username FROM Employees",
          conn);
        // Enclose database code in Try-Catch-Finally
        try
        {
            // Open the connection
            conn.Open();
            // Execute the command
            reader = comm.ExecuteReader();
            // Bind the reader to the DataList
            employeesList.DataSource = reader;
            employeesList.DataBind();
            // Close the reader
            reader.Close();
        }
        finally
        {
            // Close the connection
            conn.Close();
        }
    }
    protected void employeesList_ItemCommand(object source, DataListCommandEventArgs e)
    {
        // Which button was clicked?
        if (e.CommandName == "MoreDetailsPlease")
        {
            // Find the Literal control in the DataList item
            Literal li;
            li = (Literal)e.Item.FindControl("extraDetailsLiteral");
            // Add content to the Literal control
            li.Text = "Employee ID: <strong>" + e.CommandArgument +
                "</strong><br />";
        }
        else if (e.CommandName == "EditItem")
        {
            // Set the index of the item being edited
            employeesList.EditItemIndex = e.Item.ItemIndex;
            // update the DataList
            BindList();
        }
        else if (e.CommandName == "CancelEditing")
        {
            // Cancel edit mode
            employeesList.EditItemIndex = -1;
            // Refresh the DataList
            BindList();
        }
        else if (e.CommandName == "UpdateItem")
        {
            // Get the employee ID
            int employeeId = Convert.ToInt32(e.CommandArgument);
            // Get the new username
            TextBox nameTextBox =
                (TextBox)e.Item.FindControl("nameTextBox");
            string newName = nameTextBox.Text;
            // Get the new name
            TextBox usernameTextBox =
                (TextBox)e.Item.FindControl("usernameTextBox");
            string newUsername = usernameTextBox.Text;
            // Update the item
            UpdateItem(employeeId, newName, newUsername);
            // Cancel edit mode
            employeesList.EditItemIndex = -1;
            // Refresh the DataList
            BindList();
        }
    }
    protected void UpdateItem(int employeeId, string newName, string newUsername)
    {
        // Declare data objects
        SqlConnection conn;
        SqlCommand comm;
        // Read the connection string from Web.config
        string connectionString =
            ConfigurationManager.ConnectionStrings[
            "Dorknozzle"].ConnectionString;
        // Initialize connection
        conn = new SqlConnection(connectionString);
        // Create command 
        comm = new SqlCommand("UpdateEmployee", conn);
        // Specify we're calling a stored procedure
        comm.CommandType = System.Data.CommandType.StoredProcedure;
        // Add command parameters
        comm.Parameters.Add("@EmployeeID", SqlDbType.Int);
        comm.Parameters["@EmployeeID"].Value = employeeId;
        comm.Parameters.Add("@NewName", SqlDbType.NVarChar, 50);
        comm.Parameters["@NewName"].Value = newName;
        comm.Parameters.Add("@NewUsername", SqlDbType.NVarChar, 50);
        comm.Parameters["@NewUsername"].Value = newUsername;
        // Enclose database code in Try-Catch-Finally
        try
        {
            // Open the connection
            conn.Open();
            // Execute the command
            comm.ExecuteNonQuery();
        }
        finally
        {
            // Close the connection
            conn.Close();
        }
    }
}

You missed a key part of the linkbutton–setting the onlclick action. So, it posts, but the linkbutton don’t know what to do.

Thank you so much for your reply. I have gone through the book and code. I try to add the Onclick code, from what I leant in a previous chapter, but I get A LOT of errors. I’m not sure where/what the code should be. I am new at this. (as you can proably tell) Can you help me out?

OK, I just had the idea to try running in FF instead of IE. It works just fine! So why would it work in FF but not IE? Any clue?

Do you have javascript disabled in IE? Linkbuttons require them to function, change them to buttons and see what happens.

Java sript is enabled. I don’t have time now to change to a button, I will write back later and let you know.
Thanks