Two ways of binding data to a GridView (ASP.NET)

In the following example, I will show you how to bind the same field to an ASP.NET GridView using two different methods.

In the first example, I utilize the an asp:BoundField to display the data, whereas in the second example I utilize an asp:TemplateField

Example 1 – BoundField:

[vb]

<asp:BoundField DataField=”ORDER_NUMBER” HeaderText=”Order” ReadOnly=”True” SortExpression=”ORDER_NUMBER” ItemStyle-Wrap=false />

[/vb]

Example 2 – TemplateField:

[vb]

<asp:TemplateField HeaderText=”Order” SortExpression=”OrderLineSched” ItemStyle-Wrap=”false”>
<ItemTemplate>
<asp:Label ID=”OrderLineSched” runat=”server” Text='<% #Bind(“ORDER_NUMBER”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>

[/vb]

Both will display exactly the same, however the TemplateField provides greater flexibility because if, for example, you change the field type from Label to TextBox, you can make it an editable field that will actually be editable in the entire GridView (all rows).

SELECT TOP SQL alternative for Oracle databases

When querying SQL Server databases you can return the top X number of records that match the query criteria via a SQL statement similar to the following:

[sql]SELECT TOP 100 * FROM tbl_Products WHERE Prod_Desc LIKE ‘*widget*'[/sql]

However, this same SQL statement will not run against an Oracle database because there is no built-in “TOP” function.  To get around this, you can utilize the ROWNUM pseudocolumn.  Example:

[sql]SELECT * FROM tbl_Products WHERE Prod_Desc LIKE ‘%widget%’ AND ROWNUM < 101[/sql]