Page 8 - ASP.NET 2.0 Interview Questions
 

Q29. How to create a read-only Data Report using GridView?

A29. The simplest type of data-driven page is a read-only report, which displays data but does not allow the user to manipulate the presentation or modify the data. To create a read-only report against a SQL database, first configure a SqlDataSource on the page and then connect a data-bound control such as GridView to the data source by specifying its DataSourceID property. The example below shows a GridView control associated to a SqlDataSource control.

<form runat="server">
  <asp:GridView ID="GridView1" DataSourceID="SqlDataSource1" runat="server"/>
  <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    SelectCommand="SELECT [au_id], [au_lname], [au_fname] FROM [authors]"
    ConnectionString="<%$ ConnectionStrings:Pubs %>" />
</form>

The SqlDataSource ConnectionString property specifies the connection string to the database and the SelectCommand property specifies the query to execute to retrieve data. The connection string can be specified literally in the page, but in this case the property is assigned using a new expression syntax that retrieves the value from Web.config.

<configuration>
  <connectionStrings>
    <add name="Pubs" connectionString="Server=(local);Integrated  Security=True;Database=UrDatabaseName;"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

Q30. How do we sort data in a GridView?

A30. The SqlDataSource control supports sorting when its DataSourceMode property is set to "DataSet". To enable the sorting UI in the GridView, set the AllowSorting property to true. This causes the GridView to render link buttons for its column headers that can be clicked to sort a column. The GridView control passes the SortExpression associated with the column field to the data source control, which returns the sorted data to the GridView.


Q31. Can we sort data from a SQLDataSource when mode is set to DataReader?

A31. No. Sorting is only supported by SqlDataSource when in DataSet mode.

Inline Code looks like this...

<asp:GridView ID="GridView1" AllowSorting="true" runat="server" DataSourceID="SqlDataSource1"
      AutoGenerateColumns="False">
      <Columns>
        <asp:BoundField HeaderText="ID" DataField="au_id" SortExpression="au_id" />
        <asp:BoundField HeaderText="Last Name" DataField="au_lname" SortExpression="au_lname" />
        <asp:BoundField HeaderText="First Name" DataField="au_fname" SortExpression="au_fname" />
        <asp:BoundField HeaderText="Phone" DataField="phone" SortExpression="phone" />
        <asp:BoundField HeaderText="Address" DataField="address" SortExpression="address" />
        <asp:BoundField HeaderText="City" DataField="city" SortExpression="city" />
        <asp:BoundField HeaderText="State" DataField="state" SortExpression="state" />
        <asp:BoundField HeaderText="Zip Code" DataField="zip" SortExpression="zip" />
        <asp:CheckBoxField HeaderText="Contract" SortExpression="contract" DataField="contract" />
      </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
      SelectCommand="SELECT [au_id], [au_lname], [au_fname], [phone], [address], [city], [state], [zip], [contract] FROM [authors]"
      ConnectionString="<%$ ConnectionStrings:Pubs %>" />

Q32. How do we enable paging in a GridView?

A32. You can enable paging UI in the GridView by setting the AllowPaging property to true. The GridView can automatically page over any return value from a data source that supports the ICollection interface. The DataView returned by SqlDataSource when in DataSet mode supports this interface, so GridView can page over the result. When in DataReader mode, the GridView cannot page over the data returned by SqlDataSource.

Inline Code looks like this...

<asp:GridView ID="GridView1" AllowSorting="true" AllowPaging="true" PageSize="3" runat="server"
      DataSourceID="SqlDataSource1" AutoGenerateColumns="False">
      <PagerSettings Mode="NextPreviousFirstLast" Position="TopAndBottom" FirstPageImageUrl="~/Images/First.gif"
        LastPageImageUrl="~/Images/Last.gif" NextPageImageUrl="~/Images/Next.gif" PreviousPageImageUrl="~/Images/Prev.gif" />
      <PagerStyle ForeColor="White" HorizontalAlign="Right" BackColor="#284775" />
      <Columns>
        <asp:BoundField HeaderText="ID" DataField="au_id" SortExpression="au_id" />
        <asp:BoundField HeaderText="Last Name" DataField="au_lname" SortExpression="au_lname" />
        <asp:BoundField HeaderText="First Name" DataField="au_fname" SortExpression="au_fname" />
        <asp:BoundField HeaderText="Phone" DataField="phone" SortExpression="phone" />
        <asp:BoundField HeaderText="Address" DataField="address" SortExpression="address" />
        <asp:BoundField HeaderText="City" DataField="city" SortExpression="city" />
        <asp:BoundField HeaderText="State" DataField="state" SortExpression="state" />
        <asp:BoundField HeaderText="Zip Code" DataField="zip" SortExpression="zip" />
        <asp:CheckBoxField HeaderText="Contract" SortExpression="contract" DataField="contract" />
      </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT [au_id], [au_lname], [au_fname], [phone], [address], [city], [state], [zip], [contract] FROM [authors]"
      ConnectionString="<%$ ConnectionStrings:Pubs %>" />

 


Home Prev Next