protected void Page_Load(object sender, EventArgs e)
        {
            if (edit)
            {
                AddOrEdit.Text = "Edit Book";
                if (!int.TryParse(Request.QueryString["ID"], out bookId))
                {
                    Response.Redirect(BookList);
                }
                if (!IsPostBack)
                {
                    DataTable dt = DatabaseHelper.Retrieve(@"
                    select Title, ISBN, FirstName, LastName
                    from Book Inner Join Author on (AuthorId = Author.Id)
                    where Book.Id = @BookId
                ", new SqlParameter("@BookId", bookId));

                    if (dt.Rows.Count == 1)
                    {
                        Title.Text = dt.Rows[0].Field <string>("Title");
                        ISBN.Text  = dt.Rows[0].Field <string>("ISBN");
                        string authorName = dt.Rows[0].Field <string>("FirstName") + " ";
                        authorName           += dt.Rows[0].Field <string>("LastName");
                        Authors.SelectedValue = authorName;
                    }
                    else
                    {
                        Response.Redirect(BookList);
                    }
                }
            }
            else
            {
                AddOrEdit.Text = "Add Book";
            }
            if (!IsPostBack)
            {
                DataTable          dt    = DatabaseHelper.Retrieve(@"
                    select FirstName, LastName
                    from Author
                ");
                ListItemCollection items = new ListItemCollection();
                items.Add(new ListItem());
                foreach (DataRow row in dt.Rows)
                {
                    string author = "";
                    author += row.Field <string>("FirstName") + " ";
                    author += row.Field <string>("LastName");
                    ListItem item = new ListItem(author, author);
                    items.Add(item);
                }
                Authors.DataSource = items;
                Authors.DataBind();
            }
        }
示例#2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            AuthorAddLink.NavigateUrl = AddAuthorUrl;
            if (!IsPostBack)
            {
                DataTable dt = DatabaseHelper.Retrieve(@"
                    select Id, FirstName, LastName
                    from Author
                    order by LastName, FirstName
                ");

                Authors.DataSource = dt.Rows;
                Authors.DataBind();
            }
        }