ASP.NET Logo Back to WebMatrix Home

 
ASP.NET Pages with Data
Using Data Code Builder
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.

  1. Select File->New to get the "New File" dialog
  2. Select (General) from the Templates pane.
  3. Select the ASP.NET Page template.
  4. Type a file path in the Location box.
  5. Type myFirstDataPage.aspx in the Filename box.
  6. Select Visual Basic in the Language drop-down box.
  7. Click OK

    New File Dialog

  8. The myFirstDataPage.aspx file will open in Design view.

     
  9. 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. 

    Add CB Web Controls

  10. Select the Code tab.

     
  11. From the Code Builders tab in the Toolbox, drag a SELECT Data Method Code Builder onto the ASP.NET page. 

     

    Select Code Builder

  12. You will see the Connect to Database dialog box appear.

     

    Select Orders Database

  13. Type localhost in the Server box (default).

     
  14. Click the Windows authentication radio button.

     
  15. 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.

     
  16. Click OK

     
  17. You will see step one of the Code Builder Wizard appear; it is called the Construct SELECT Query dialog box.

    SELECT Query Builder

     
  18. Select OrderDetails in the Tables list view.

     
  19. 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.

     
  20. Click WHERE.

     
  21. You will see the WHERE Query Builder dialog box appear.

    Where Clause Query Builder

  22. Select OrderID in the Column list view.

     

    Note This option sets the Left Operand portion of the WHERE clause.

  23. 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.

  24. Click OK

     
  25. You will return to the Construct SELECT Query step of the Code Builder Wizard.

    Construct SELECT Query

  26. Click Next

     
  27. You will see step two of the Code Builder Wizard appear; Query Preview.

     
  28. Click Test Query.

     
  29. Type 1 into the OrderID = box.

     
  30. Click OK

    Build Test Query

  31. You will see the OrderDetails row corresponding to OrderID = 1 appear in the Query Preview dialog box results grid. 

    Test Query Results

  32. Click Next.

     
  33. 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.

     
  34. Select the DataSet radio button (default).

     
  35. Click Finish

    Enter Caption

  36. 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
    
  37. Select the Design tab.

     
  38. Double-click the Button to open the Button1_Click event in Code view.

     
  39. 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
    
  40. Save and run the ASP.NET page.

     
  41. Your ASP.NET page will automatically appear in a new instance of your Web browser.

     
  42. Type 1 into the TextBox and click the Button.

     
  43. The DataGrid will show all of the rows in the OrderDetails table for the specified OrderID

    CB Data Grid

  44. Close the Web browser instance.

Next Step >>