Now you add code so that the connection to the database is made and data is retrieved into the master DataWindow when the form is loaded. You need to establish the relationship between the WebDataWindowControl and the Transaction object at the same time. If any errors occur, they display in a text box.
To connect to the database and set up the transaction:
Select View>Designer to open the default.aspx form.
Expand the Standard tab in the Visual Studio Toolbox, drag a TextBox control onto the form below the dwCustomer DataWindow.
In the properties window, set its ID to errMsg, its TextMode to Multiline, and its Visible property to false.
The text box will only display if an error occurs.
Double-click in an empty area of the form.
This creates an event handler for the page’s Load event and opens the code editor:
[Visual Basic] Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub
[C#] private void Page_Load(object sender, System.EventArgs e) { }
Type the following in the Page_Load event handler:
[Visual Basic] If Not Me.IsPostBack Then Try InitializeComponent() myTrans.Connect() dwCustList.SetTransaction(myTrans) dwCustList.Retrieve() Catch Ex As Exception errMsg.Text = ex.ToString() errMsg.Visible = true dwCustomer.Visible = false Finally myTrans.Disconnect() End Try End If
[C#] if (!this.IsPostBack) { try { InitializeComponent(); myTrans.Connect(); dwCustList.SetTransaction(myTrans); dwCustList.Retrieve(); } catch(System.Exception ex) { errMsg.Text = ex.ToString(); errMsg.Visible = true; dwCustomer.Visible = false; } finally { myTrans.Disconnect(); } }
This code connects to the database, associates the connected Transaction object with the master DataWindow, and retrieves data into the master DataWindow. If any exception is thrown, the errMsg textbox displays with the text of the exception.