Public Class EditBook Inherits System.Web.UI.Page Protected WithEvents Label2 As System.Web.UI.WebControls.Label Protected WithEvents Label3 As System.Web.UI.WebControls.Label Protected WithEvents Label4 As System.Web.UI.WebControls.Label Protected WithEvents Label5 As System.Web.UI.WebControls.Label Protected WithEventsbtnUpdate As System.Web.UI.WebControls.Button Protected WithEventstxtPrice As System.Web.UI.WebControls.TextBox Protected WithEventstxtAuthor As System.Web.UI.WebControls.TextBox Protected WithEventstxtBookName As System.Web.UI.WebControls.TextBox Protected WithEventscboAvailability As System.Web.UI.WebControls.DropDownList Protected WithEventsbtnCancel As System.Web.UI.WebControls.Button Protected WithEventsrfvBookName As System.Web.UI.WebControls.RequiredFieldValidator Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator Protected WithEventslblMsg As System.Web.UI.WebControls.Label Protected WithEvents Label1 As System.Web.UI.WebControls.Label #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here ' Ensure the msg label is invisible on load Me.lblMsg.Visible = False If Not Page.IsPostBack Then ' Get a reference to the viewbooks page which invokes this webform Dim ViewBookRef As ViewBooks = CType(Context.Handler, ViewBooks) ' Grab the BookId from the form Dim BookId As String = ViewBookRef.BookId ' Instantiate our business tier objects required Dim BookObj As New BookServices() Dim AvailObj As New AvailabilityServices() ' Retrieve the Book being edited Dim BookDs As DataSet = BookObj.GetBooks(Convert.ToInt32(BookId), Nothing, Nothing, Nothing) ' Populate our form with the book info If Not BookDs Is Nothing Then With BookDs.Tables(0).Rows(0) Me.txtAuthor.Text = .Item(BookDb.FIELD_AUTHOR) Me.txtBookName.Text = .Item(BookDb.FIELD_BOOK_NAME) Me.txtPrice.Text = .Item(BookDb.FIELD_PRICE) ' Populate the availability drop down Dim AvailDs As DataSet = AvailObj.GetAllAvailability() Me.cboAvailability.DataSource = AvailDs.Tables(0) Me.cboAvailability.DataTextField = AvailabilityDb.FIELD_AVAILABILITY_NAME Me.cboAvailability.DataValueField = AvailabilityDb.FIELD_AVAILABILITY_ID Me.DataBind() ' Once the form is databound we can select the availability Id of the book in the dropdown Me.cboAvailability.Items.FindByValue(.Item(AvailabilityDb.FIELD_AVAILABILITY_ID)).Selected = True ' Add the BookId to the viewstate so we can retrieve it on the next postback Me.ViewState.Add(BookDb.FIELD_BOOK_ID, BookId) End With End If End If End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click Server.Transfer(WebPageNames.VIEW_BOOKS) End Sub '######################################################################################################################### ' Updates the book information to the database '######################################################################################################################### Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click ' Our validation controls have already ensured the correct fields are populated Try Dim BizObj As New BookServices() BizObj.UpdateBook(Me.ViewState.Item(BookDb.FIELD_BOOK_ID), Me.txtAuthor.Text, _ Me.txtBookName.Text, Me.txtPrice.Text, Me.cboAvailability.SelectedItem.Value) Me.lblMsg.Visible = True Me.lblMsg.Text = "The book was updated successfully" Catch ex As Exception Me.lblMsg.Visible = True Me.lblMsg.Text = "There was an error updating the book" End Try End Sub End Class |