| Code Builders are wizard-based code
generators that help you create functions that perform common tasks.
ASP.NET Web Matrix ships with several built-in code builders - including
several specifically designed to optimize common data access tasks using
ADO.NET.
In this walkthrough, you will use the SELECT Code Builder to
generate the code needed to retrieve information from the OrderDetails
table and populate a DataGrid.
- Select File->New to get the "New File" dialog
- Select (General) from the Templates pane.
- Select the ASP.NET Page template.
- Type a file path in the Location box.
- Type myFirstDataPage.aspx in the Filename box.
- Select Visual Basic in the Language drop-down box.
- Click OK.
- The myFirstDataPage.aspx file will open in Design view.
- From the Web Controls tab in the Toolbox (on the left
hand side of the tool), drag a TextBox control, a DataGrid
control, and a Button control onto the ASP.NET page. Press the
Enter key between the placement of each control.
- Select the Code tab.
- From the Code Builders tab in the Toolbox, drag a
SELECT Data Method Code Builder onto the ASP.NET page.
- You will see the Connect to Database dialog box appear.
- Type localhost in the Server box (default).
- Click the Windows authentication radio button.
- Click the Database drop-down. ASP.NET Web Matrix will connect
to the data source and populate the Database drop-down with
database names. Select the Orders database that we developed in an
earlier section.
- Click OK.
- You will see step one of the Code Builder Wizard appear; it is
called the Construct SELECT Query dialog box.
- Select OrderDetails in the Tables list view.
- Select the check box next to the * item in the Columns
list view. This will cause all columns within the database table to be
selected.
- Click WHERE.
- You will see the WHERE Query Builder dialog box appear.
- Select OrderID in the Column list view.
Note This option sets the Left Operand portion of the
WHERE clause.
- Select the Filter radio button and type @OrderID in the
filter box (default).
Note This option sets the Right Operand portion of the
WHERE clause. You will pass this value as a parameter to the WHERE clause
later in this walkthrough.
- Click OK.
- You will return to the Construct SELECT Query step of the
Code Builder Wizard.
- Click Next.
- You will see step two of the Code Builder Wizard appear;
Query Preview.
- Click Test Query.
- Type 1 into the OrderID = box.
- Click OK.
- You will see the OrderDetails row corresponding to OrderID =
1 appear in the Query Preview dialog box results grid.
- Click Next.
- You will see step three of the Code Builder Wizard appear; it
is called the Enter A Caption dialog box. Type GetOrderDetails
into the method name box.
- Select the DataSet radio button (default).
- Click Finish.
- The Wizard will close and ASP.NET Web Matrix will place a
function called GetOrderDetails in Code view. The function
accepts an orderID as an input parameter and returns a DataSet
as an output parameter. The DataSet is populated with order detail
data for the specified orderID using ADO.NET data access code
generated by the code builder:
Function GetOrderDetails(ByVal orderID As Integer) As System.Data.DataSet
Dim connectionString As String = _
"server='localhost'; trusted_connection=true; Database='Orders'"
Dim sqlConnection As System.Data.SqlClient.SqlConnection = _
New System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = _
"SELECT [OrderDetails].* " & _
"FROM [OrderDetails] " & _
"WHERE ([OrderDetails].[OrderID] = @OrderID)"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = _
New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
sqlCommand.Parameters.Add("@OrderID", System.Data.SqlDbType.Int).Value = orderID
Dim dataAdapter As System.Data.SqlClient.SqlDataAdapter = _
New System.Data.SqlClient.SqlDataAdapter(sqlCommand)
Dim dataSet As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(dataSet)
Return dataSet
End Function
- Select the Design tab.
- Double-click the Button to open the Button1_Click event in
Code view.
- Add code that checks the value of the TextBox and passes its
value as a parameter to the GetOrderDetails function:
Sub Button1_Click(sender As Object, e As EventArgs)
DataGrid1.DataSource = GetOrderDetails(CInt(TextBox1.Text))
DataGrid1.DataBind()
End Sub
- Save and run the ASP.NET page.
- Your ASP.NET page will automatically appear in a new instance of your
Web browser.
- Type 1 into the TextBox and click the Button.
- The DataGrid will show all of the rows in the OrderDetails
table for the specified OrderID.
- Close the Web browser instance.
Next Step >> |