0% found this document useful (0 votes)
84 views8 pages

How Can You Perform Fast Searching Result in Dot Net

The document provides step-by-step guidance for implementing server-side paging to efficiently retrieve and display a large dataset in an ASP.NET grid or web API. The key steps include creating a paginated database query, retrieving data using the query, binding data to the grid, adding paging controls, and handling paging events to fetch additional records.

Uploaded by

Zubair Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views8 pages

How Can You Perform Fast Searching Result in Dot Net

The document provides step-by-step guidance for implementing server-side paging to efficiently retrieve and display a large dataset in an ASP.NET grid or web API. The key steps include creating a paginated database query, retrieving data using the query, binding data to the grid, adding paging controls, and handling paging events to fetch additional records.

Uploaded by

Zubair Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

How can you perform fast Search result in Asp.Net?

Question: - I have a dataset with more than 100,000 records that I want to
display in an ASP.NET grid using C#. However, I'd like to implement a
mechanism where I can retrieve and display the first 50 records initially, and
then efficiently fetch and display additional records from the database as
needed. I want to avoid loading all 100,000 records into the grid at once. How
can I achieve this, let’s follow step-by-step guidance and code examples to
illustrate the process?

Details Answers
Here's a solution to display a large dataset in an ASP.NET grid with server-side
paging:

1. Set up your environment: Ensure you have a working ASP.NET project


and a database from which you want to retrieve data. Make sure you
have a reference to your database and the necessary data access
components (e.g., ADO.NET, Entity Framework, etc.) in your project.

2. Create a database query with pagination: Create a SQL query that


retrieves data with pagination. For example, if you're using SQL Server,
you can use the OFFSET and FETCH clauses. Here's a sample query to
retrieve the first 50 records:

SELECT * FROM YourTable ORDER BY YourColumn


OFFSET 0 ROWS FETCH NEXT 50 ROWS ONLY;

3. Implement a method to retrieve data: In your C# code-behind, create a


method to retrieve data from the database using the query you've
defined. For example, if you're using ADO.NET:

Prepared By Sr.Software Engineer Zubair Hussain


public DataTable GetPagedData(int pageIndex, int pageSize)
{
using (SqlConnection connection = new
SqlConnection("your_connection_string"))
{
connection.Open();
using (SqlCommand command = new SqlCommand("your_sql_query",
connection))
{
command.Parameters.Add(new SqlParameter("pageIndex",
pageIndex));
command.Parameters.Add(new SqlParameter("pageSize",
pageSize));
using (SqlDataAdapter adapter = new
SqlDataAdapter(command))
{
DataTable table = new DataTable();
adapter.Fill(table);
return table;
}
}
}
}

4. Bind the data to the grid: In your ASP.NET page, bind the retrieved data
to the grid control. For example:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
myGrid.DataSource = GetPagedData(0, 50);
myGrid.DataBind();
}
}

5. Add paging controls to the grid: Add paging controls to the grid control
so that users can navigate through the data. For example:
<asp:GridView ID="myGrid" runat="server" AllowPaging="True"
PageSize="50">
<PagerSettings Mode="NumericFirstLast" PageButtonCount="10"/>
</asp:GridView>

6. Handle paging events: Handle paging events in your code-behind so


that when users click on a paging control, the next set of records is
retrieved and displayed. For example:

Prepared By Sr.Software Engineer Zubair Hussain


protected void myGrid_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
myGrid.PageIndex = e.NewPageIndex;
myGrid.DataSource = GetPagedData(e.NewPageIndex, 50);
myGrid.DataBind();
}

This code retrieves and displays the first 50 records from YourTable, and then
adds numeric paging controls to myGrid. When the user clicks on one of these
buttons, it retrieves and displays the next set of 50 records from YourTable.

Please note that this is just one example of how you can implement paging in
an ASP.NET grid using C#. The exact implementation may vary depending on
your specific requirements and database schema.

In Vb.NET Solutions
Here's a solution to display a large dataset in an ASP.NET grid with server-side
paging using VB.NET:

1. Set up your environment: Ensure you have a working ASP.NET project


and a database from which you want to retrieve data. Make sure you
have a reference to your database and the necessary data access
components (e.g., ADO.NET, Entity Framework, etc.) in your project.

2. Create a database query with pagination: Create a SQL query that


retrieves data with pagination. For example, if you're using SQL Server,
you can use the OFFSET and FETCH clauses. Here's a sample query to
retrieve the first 50 records:
SELECT * FROM YourTable
ORDER BY YourColumn
OFFSET 0 ROWS FETCH NEXT 50 ROWS ONLY;

Prepared By Sr.Software Engineer Zubair Hussain


3. Implement a method to retrieve data: In your VB.NET code-behind,
create a method to retrieve data from the database using the query
you've defined. For example, if you're using ADO.NET:

Public Function GetPagedData(pageIndex As Integer, pageSize As


Integer) As DataTable
Using connection As New
SqlConnection("your_connection_string")
connection.Open()
Using command As New SqlCommand("your_sql_query",
connection)
command.Parameters.Add(New SqlParameter("pageIndex",
pageIndex))
command.Parameters.Add(New SqlParameter("pageSize",
pageSize))
Using adapter As New SqlDataAdapter()
adapter.SelectCommand = command
Dim table As New DataTable()
adapter.Fill(table)
Return table
End Using
End Using
End Using
End Function

4. Bind the data to the grid: In your ASP.NET page, bind the retrieved data
to the grid control. For example:

Protected Sub Page_Load(sender As Object, e As EventArgs)


Handles Me.Load
If Not IsPostBack Then
myGrid.DataSource = GetPagedData(0, 50)
myGrid.DataBind()
End If
End Sub

Prepared By Sr.Software Engineer Zubair Hussain


5. Add paging controls to the grid: Add paging controls to the grid control
so that users can navigate through the data. For example:

<asp:GridView ID="myGrid" runat="server" AllowPaging="True"


PageSize="50">
<PagerSettings Mode="NumericFirstLast"
PageButtonCount="10" />
</asp:GridView>

6. Handle paging events: Handle paging events in your code-behind so


that when users click on a paging control, the next set of records is
retrieved and displayed. For example:

Protected Sub myGrid_PageIndexChanging(sender As Object, e As


GridViewPageEventArgs) Handles myGrid.PageIndexChanging
myGrid.PageIndex = e.NewPageIndex
myGrid.DataSource = GetPagedData(e.NewPageIndex, 50)
myGrid.DataBind()
End Sub

This code retrieves and displays the first 50 records from YourTable, and then
adds numeric paging controls to myGrid. When the user clicks on one of these
buttons, it retrieves and displays the next set of 50 records from YourTable.

Please note that this is just one example of how you can implement paging in
an ASP.NET grid using VB.NET. The exact implementation may vary depending
on your specific requirements and database schema.

Prepared By Sr.Software Engineer Zubair Hussain


To implement server-side paging for a large dataset in an ASP.NET Core Web
API, you can follow these steps. I'll provide a high-level overview, and you can
refer to specific documentation and adapt the code to your needs:

Step 1: Set Up Your ASP.NET Core Web API Project

Ensure you have a working ASP.NET Core Web API project and a database or
data source from which you want to retrieve data.

Step 2: Install Required Packages

You might need to install Entity Framework Core or other data access
packages suitable for your database. You can use NuGet Package Manager to
install these packages.

Step 3: Create a Model and DbContext

Define your data model and create a DbContext class that represents your
data source. For example:
public class YourModel

public int Id { get; set; }

// Other properties

public class YourDbContext : DbContext

public DbSet<YourModel> YourModels { get; set; }

public YourDbContext(DbContextOptions<YourDbContext> options) : base(options) { }

Prepared By Sr.Software Engineer Zubair Hussain


Step 4: Implement a Method to Retrieve Paged Data

In your API controller, create an action to retrieve paged data. You can use
Entity Framework Core and LINQ to achieve this. For example:

[HttpGet]

public ActionResult<IEnumerable<YourModel>> GetPagedData(int pageIndex, int


pageSize)

var pagedData = _context.YourModels

.Skip(pageIndex * pageSize)

.Take(pageSize)

.ToList();

return pagedData;

Step 5: Configure Routing

Configure routing in your API's Startup.cs to enable this endpoint. For


example:

app.UseEndpoints(endpoints =>

endpoints.MapControllers();

});

Prepared By Sr.Software Engineer Zubair Hussain


Step 6: Set Up Your ASP.NET Core Client (if applicable)

If you have a client application, set it up to call your API endpoint, passing the
pageIndex and pageSize parameters to request specific pages of data.

Step 7: Handle Paging on the Client Side

In your client application, create the logic to handle paging. You may use AJAX
requests to call the API endpoint with the appropriate pageIndex and
pageSize parameters based on user interactions.

Step 8: Display Data in the UI

Display the retrieved data in your UI. You can use any client-side grid or data
table component, such as DataTables, ag-Grid, or a custom solution.

With these steps, you'll be able to retrieve and display a subset of data at a
time, implementing server-side paging in your ASP.NET Core Web API. Users
can navigate through the data efficiently without loading the entire dataset.
The specific implementation may vary based on your requirements, data
source, and client application.

Prepared By Sr.Software Engineer Zubair Hussain

You might also like