|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
Dreamweaver The Tag That Takes the Drag Out of Coding
The Tag That Takes the Drag Out of Coding
By: Heidi Bautista
Feb. 2, 2004 12:00 AM
If you're using Dreamweaver to build ASP.NET-driven Web sites, you've undoubtedly encountered DataSets. The DataSet tag, one of Macromedia's custom tags, interacts with a database via SQL statements like SELECT, INSERT, UPDATE, DELETE. You can also use it to execute stored procedures. The DataSet is to ASP.NET what the recordset is to ASP. Except that it's better, of course. The most obvious application of the DataSet tag is to select data from a table in your database. The resulting dataset can be displayed in a variety of ways: in a DataGrid, DropdownList, or pretty much wherever you want in your page. Another handy, though less obvious, function of the DataSet tag is to change data in a table (e.g., insert, update, or delete). In this case, the DataSet tag doesn't need to create an actual dataset, just perform some function. In this article you'll learn about the attributes and methods available with the DataSet tag. I assume you've already set up an ASP.NET site within Dreamweaver - using either the Visual Basic or C# ASP.NET server model. (If you're not sure how this is done, select "Getting Started and Tutorials" from the Help menu. ) What's in a Name? DataSet Attributes
![]() Required attributes Here's an example of using a SQL statement for the CommandText: CommandText= Tip: The CommandText can also contain a binding expression. For example: CommandText=' This ternary expression stipulates that:
You cannot break up the binding expression over multiple lines the way it's shown here. The entire binding expression has to be on one line. (You'll get a nasty runtime error otherwise.) Parameterized SQL Statements Only the value in a name/value pair within the SQL statement is legal for parameterization. Table II lists some of the name/value pairs that are candidates for parameterization.
![]() The WHERE clause is a prime example of where a name/value pair is used. Keep in mind that the name and value need not be adjacent to each other (see the INSERT example). The ORDER BY clause cannot be parameterized since it doesn't follow the name/value paradigm. The syntax for the parameter depends on which type of connection you are using: OLE DB or SQL Server. Consider the following example of a SELECT statement: SELECT * If you replace the values in the name/value pairs in the WHERE clause with parameters, the statement becomes: SQL Server connection
SELECT *
Continuing with the example given above, here are the required <Parameter> tags to define the parameters (depending on the connection type used). SQL Server connection <Parameters> OLE DB connection <Parameters>
![]() ConnectionString In the DataSet dialog all you have to do is select one of the predefined connections from the Connection dropdown menu and voilá, the ConnectionString attribute is written for you (see Image I).
![]() Here's an example of a ConnectionString: ConnectionString= (Keep in mind that you can't put a carriage return in the middle of a binding expression. Refer to the tip on page 12 for an explanation.) runat="server" The DataSet tag is a server-side control; it is processed on the server before being sent to the client. The default value for the runat attribute is "client" so you must explicitly set it to "server." Dreamweaver does this for you when it creates a DataSet tag. Optional Attributes
CreateDataSet This optional Boolean attribute determines whether or not a dataset is created. That might seem odd since you'd think that the DataSet tag's whole purpose is to create sets of data. But remember that you can also use the DataSet tag to insert, update, delete, etc. Turns out that nothing bad will happen when you leave the CreateDataSet attribute at its default value of true when no records are returned. But it's good practice to set it to false in these cases. The DataSet dialog does not have a checkbox for you to specify this value. If you want to set it to false, you have to do it directly in code view. CreateDataSet="false" Tip: Notice that the value for this Boolean attribute is in double quotes. In order to be XHTML compliant, the values in all name/value pairs must be quoted (even if the value is a Boolean). Expression If a DataSet tag is sitting on your page and its Expression attribute is either absent or set to true, it will execute when the page loads. But what if the purpose of the DataSet tag is to update the database based on values the user is supposed to enter on the page? Instead of running each time the page loads, what you really want is for the DataSet to execute after the page has been submitted (e.g., posted back). You can accomplish that goal with the following: Expression='<%# IsPostBack %>' (Of course, you'll probably want to do some error checking to make sure all the required information is available first.) The DataSet dialog does not have a checkbox for you to specify the value of the Expression attribute. You'll have to type it directly into the code if you want its value to be anything other than the default value of true. IsStoredProcedure IsStoredProcedure="true" I bet you can't find a place on the DataSet dialog to set the IsStored- Procedure value. That's because Dreamweaver has an entirely separate Server Behavior for DataSets that execute stored procedures (see Image II).
![]() It's still a DataSet, but Dreamweaver has simplified matters by providing a specific Server Behavior just for DataSets that execute stored procedures (see Image III).
![]() Notice that it looks very similar to the DataSet dialog shown in Image I, but it prompts for the name of the stored procedure and its parameters rather than asking you to select a table and columns. In addition, the Stored Procedure dialog has a checkbox labeled Returns DataSet. The default is unchecked, which means that a DataSet is not created (in other words, the CreateDataSet attribute is false). Dreamweaver's user interface separates the DataSet and Stored Procedure server behaviors into two separate commands for convenience and clarity in the dialogs. But keep in mind that they share the underlying code that supports the DataSet tag. Read through the DreamweaverCtrls.cs file to convince yourself. If you installed Dreamweaver MX 2004 in the default location, you will find the Dreamweaver Ctrl.cs file in C:\Program\Files\Macromdia\DreamweaverMX 2004\Configuration\ServerBehaviors\Shared\ASP.net\Sripts\Source. TableName Post-Processing Attributes Also keep in mind that if you set Debug to true, the FailureURL attribute is ignored. When an exception is thrown, you'll get the debugging information instead of being redirected to the page specified by FailureURL. Code 1 is an example of a DataSet tag with a problem with the ConnectionString attribute (it doesn't exist). Image IV shows the debugging information you'll see when you run the page.
![]() Obviously Dreamweaver hasn't pinpointed the exact problem, but it does give you a pretty broad hint that you should look at the ConnectionString attribute. FailureURL SuccessURL Paging Attributes The CurrentPage and PageSize attributes are meant to be used together. They tell the DataSet tag how many rows to get (the PageSize) and from where in the table to get them (CurrentPage). Note that the PageSize is zero-based so the first page corresponds to CurrentPage="0". Consider the following example: let's say that your CommandText's SQL statement returns 63 rows. If you specify a PageSize of 10, that means you will have seven pages. The first six pages will have 10 rows each and the seventh page will have the remaining 3 rows. If you want to see the page with the 22nd row, then the CurrentPage would be 2. Tip: If you're creating a dataset to be used as the data source for a DataGrid, be sure to use Dreamweaver's user interface to create the DataGrid. They've built in the correspondence between the DataSet's PageSize attribute and the DataGrid's PageSize attribute. Culling Attributes StartRecord is zero-based, so if you want the 10th through 25th record, then StartRecord="9" and MaxRecords="16". That is, StartRecord indicates the first record to get and MaxRecords indicates the total number of records to get. Tip: Regarding the usage of the PageSize/CurrentPage and StartRecord/MaxRecords attributes:
GetRecordCount This Boolean attribute defaults to true, meaning that the number of records in the DataSet will be calculated. If you don't need this value for your application, set the GetRecordCount to false. In fact, under certain circumstances you can improve performance with GetRecordCount=false. RecordCountCommandText SELECT COUNT(*) Using the RecordCountCommand Text attribute is particularly important (performance-wise) when you've set PageSize to be greater than zero. It turns out that under the hood Dreamweaver uses the DbDataAdapter.Fill method to fill the .NET DataSet object with rows from the data source. The Fill method returns the number of rows added to the .NET DataSet (e.g., the record count). If PageSize is greater than zero, the value returned from DbDataAdapter.Fill is exactly equal to the PageSize. When you're looking for the record count, that's not the number you want. That means that the MM:DataSet code has to do some extra work to compute the actual record count. This is how Dreamweaver handles record counting when PageSize is greater than zero:
One last note on the RecordCountCommandText: you can't parameterize it. Computed or Created Attributes The simplest calculation for the record count occurs when both PageSize and MaxRecords are zero. The RecordCount corresponds to the number of records retrieved by the CommandText. If MaxRecords is greater than zero, the value returned in the RecordCount attribute takes the MaxRecords and StartRecord values into account. RecordCount is equal to MaxRecords so long as MaxRecords is not greater than the number of records you actually have. If PageSize is greater than zero, the RecordCount calculation totally ignores it. See the RecordCountCommandText section above, which describes how the record count is computed when the PageSize attribute is greater than zero. Tip: For more information on the record count (including performance aspects), search the DreamweaverCtrls.cs file for the CalculateRecordCount method and the GetRecordCount attribute. EndRecord LastPage DefaultView <asp:datagrid The DefaultView represents all of the data retrieved by the DataSet tag and in the same order that it was retrieved. If you wanted a subset of the data and/or the data ordered differently, you would need to create a new view to associate with the DataSet or change the CommandText of the DataSet so that it gets the subset you want and/or orders it the way you want it ordered. Tip: The MM:DataSet tag retrieves records from a single table in the database; but that's a limitation of the MM:DataSet tag, not of the .NET DataSet object. If you look at the documentation for the .NET DataSet object, you'll see that it can contain data from one or more tables. The theDS attribute of the MM:DataSet tag is equal to the DefaultView of the table 0 in the .NET dataset. myDataSet.DefaultView <==> theDS.Tables[0].DefaultView where:
theDS Consider the following example: you have a dropdown list populated with the contents of a DataSet, but you want the first item (e.g., option tag) of the dropdown list to be "Please select one" and the option's value to be 0 (see Image V).
![]() <select name="sizeDropdown" id="sizeDropdown"> You can programmatically add that first option tag by utilizing the theDS attribute to add a new row to the .NET dataset. DataSet Methods Both of the DataSet methods return a string corresponding to the value of a named field (FieldName) within some row in the DataSet. The FieldValue method returns the value of the named field from the first row (row 0). FieldValueAtIndex takes an additional parameter that indicates the specific row number. Note that rows are zero-indexed. Since FieldValue acts on row 0, the following two function calls are equivalent: DS_uidAndPwd.FieldValue("emailaddr", null) To Use a Container or Not Here's a quick example: you have a page with a DataGrid. The data source for the DataGrid is a DataSet that contains a list of e-mail addresses. Each row of DataGrid corresponds to a row in the DataSet so you have one e-mail address on each row. The first thing you need to do in Dreamweaver MX is change the column data type from Simple Data Field to Free Form. This allows you to use an ItemTemplate. This is where you use the FieldValue("FieldName", Container) form of the method: <asp:TemplateColumn HeaderText="emailaddr" Visible="True"> Using Container in the call to FieldValue ensures that the appropriate DataSet row is used. Tip: Curious about the meaning of Container? Check out the .NET documentation for the Eval method of the DataBinder class. The Container used in FieldValue and FieldValueAtIndex is identical to the Container used when you call DataBinder.Eval. On the other hand, you should use null when you want to use a value from the dataset somewhere outside of a DataGrid, DataList, or Repeater. The following example prints out a single e-mail address from the 5th row of the dataset. <%# DataSet1.FieldValueAtIndex(4, "emailaddr", null) %> Conclusion Reader Feedback: Page 1 of 1
Your Feedback
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||