Fetching Data for the Details

Fetching Data for the Details

To fetch data for the details page in ASP.NET, you can use a data source control such as the SqlDataSource or ObjectDataSource. Here’s an example of using the SqlDataSource to fetch data for the details page:

Add a SqlDataSource control to your details page:

<asp:SqlDataSource ID=”SqlDataSource1″ runat=”server” ConnectionString=”<%$ ConnectionStrings:NorthwindConnectionString %>” SelectCommand=”SELECT ProductID, ProductName, UnitPrice FROM Products WHERE CategoryID=@CategoryID”>

   <SelectParameters>

      <asp:QueryStringParameter Name=”CategoryID” QueryStringField=”CategoryID” Type=”Int32″ />

   </SelectParameters>

</asp:SqlDataSource>

This code defines a SqlDataSource control with an ID of “SqlDataSource1” that retrieves data from the “Products” table of the Northwind database. The SelectCommand specifies a parameterized SQL query that retrieves products with a specified category ID. The parameter is defined using a QueryStringParameter that gets its value from the “CategoryID” query string parameter.

Add a control to display the data:

<asp:DetailsView ID=”DetailsView1″ runat=”server” DataSourceID=”SqlDataSource1″ AutoGenerateRows=”False”>

   <Fields>

      <asp:BoundField DataField=”ProductID” HeaderText=”Product ID” />

      <asp:BoundField DataField=”ProductName” HeaderText=”Product Name” />

      <asp:BoundField DataField=”UnitPrice” HeaderText=”Unit Price” DataFormatString=”{0:C}” />

   </Fields>

</asp:DetailsView>

This code adds a DetailsView control with an ID of “DetailsView1” to the details page. The DataSourceID property is set to “SqlDataSource1” to bind the data from the SqlDataSource control to the DetailsView control. The AutoGenerateRows property is set to “False” to specify that the details fields are defined manually. The Fields collection specifies the details fields to display, which are bound to the “ProductID”, “ProductName”, and “UnitPrice” fields of the SqlDataSource control. The DataFormatString property of the “UnitPrice” field is set to “{0:C}” to format the unit price as a currency value. When the details page is loaded, the SqlDataSource control retrieves the data from the database based on the category ID specified in the query string, and populates the DetailsView control with the product details.

Apply for ASP.NET Certification Now!!

https://www.vskills.in/certification/certified-aspnet-programmer

Back to Tutorial

Get industry recognized certification – Contact us

Menu