0% found this document useful (0 votes)
12 views3 pages

To Implement Dynamic Parameters For

This document outlines the implementation of dynamic parameters for stored procedures in an ASP.NET MVC application, specifically for flexible searching, filtering, or sorting. It details the creation of a dynamic stored procedure, modifications to the repository to handle optional parameters, and the setup of a controller method and search view for user input. The approach enhances query flexibility, reusability, performance, and code cleanliness.

Uploaded by

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

To Implement Dynamic Parameters For

This document outlines the implementation of dynamic parameters for stored procedures in an ASP.NET MVC application, specifically for flexible searching, filtering, or sorting. It details the creation of a dynamic stored procedure, modifications to the repository to handle optional parameters, and the setup of a controller method and search view for user input. The approach enhances query flexibility, reusability, performance, and code cleanliness.

Uploaded by

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

To implement dynamic parameters for stored procedures in your ASP.

NET MVC
application, you can follow these steps. This approach is especially useful for
cases like flexible searching, filtering, or sorting, where the parameters passed
to a stored procedure are dynamic.

Step-by-Step Implementation for Dynamic Parameters:


1. Create a Dynamic Stored Procedure
In your SQL Server database, create a stored procedure that can accept optional
parameters for dynamic search.

For example, let's create a SearchEmployees stored procedure that will allow
filtering by Name and/or Position. These parameters are optional, so if no value is
passed, it will ignore the filter for that column.

sql
Copy
Edit
CREATE PROCEDURE SearchEmployees
@Name NVARCHAR(100) = NULL,
@Position NVARCHAR(100) = NULL
AS
BEGIN
SELECT *
FROM Employees
WHERE
(@Name IS NULL OR Name LIKE '%' + @Name + '%')
AND
(@Position IS NULL OR Position LIKE '%' + @Position + '%');
END
GO
In this procedure:

If @Name is passed, it will filter by Name.


If @Position is passed, it will filter by Position.
If any of the parameters is NULL, that condition will be ignored.
2. Modify the Repository to Handle Dynamic Parameters
In your repository class, modify the method to dynamically build and pass
parameters to the stored procedure. The method should handle optional parameters
and pass them as NULL when they are not provided.

Here’s an example of how you could modify the repository method to call the
SearchEmployees stored procedure with dynamic parameters.

csharp
Copy
Edit
public class EmployeeRepository
{
private EmployeeDBEntities db = new EmployeeDBEntities(); // Your Entity
Framework context

public List<Employee> SearchEmployees(string name = null, string position =


null)
{
// Use SqlParameter to handle null values
var parameters = new List<SqlParameter>
{
new SqlParameter("@Name", (object)name ?? DBNull.Value),
new SqlParameter("@Position", (object)position ?? DBNull.Value)
};

return db.Database.SqlQuery<Employee>("EXEC SearchEmployees @Name,


@Position", parameters.ToArray()).ToList();
}
}
The name and position parameters are optional (null by default).
If the parameter is null, the query passes DBNull.Value to the stored procedure to
ensure that the condition in SQL is ignored.
3. Controller Method for Searching
In your controller, you can now create an action method that calls the repository’s
SearchEmployees method and passes in the dynamic search parameters.

csharp
Copy
Edit
public class EmployeeController : Controller
{
private EmployeeRepository repository = new EmployeeRepository();

// GET: Employee/Search
public ActionResult Search(string name, string position)
{
var employees = repository.SearchEmployees(name, position);
return View(employees);
}
}
This Search action allows you to pass name and position as query parameters in the
URL (e.g., /Employee/Search?name=John&position=Manager), and it will search
employees based on those parameters.

4. Creating a Search View


Now, create a search view (Search.cshtml) where users can input the search
criteria. You can provide input fields for the Name and Position, and the form will
submit the search parameters to the controller.

html
Copy
Edit
@model IEnumerable<MvcApp.Models.Employee>

<h2>Search Employees</h2>

@using (Html.BeginForm("Search", "Employee", FormMethod.Get))


{
<div>
<label for="name">Name:</label>
@Html.TextBox("name", Request.QueryString["name"])
</div>
<div>
<label for="position">Position:</label>
@Html.TextBox("position", Request.QueryString["position"])
</div>
<input type="submit" value="Search" />
}

<h3>Results</h3>
<table>
<tr>
<th>Name</th>
<th>Position</th>
<th>Salary</th>
</tr>
@foreach (var employee in Model)
{
<tr>
<td>@employee.Name</td>
<td>@employee.Position</td>
<td>@employee.Salary</td>
</tr>
}
</table>
The form will send the parameters to the Search action, which then calls the
SearchEmployees method in the repository.
If the user searches for a name, it will only filter by that. If they search for
both name and position, it will filter by both.
5. Using the Dynamic Parameters in the URL
Now, if you navigate to /Employee/Search?name=John&position=Manager, it will show
employees whose name contains "John" and position contains "Manager".

You can also filter only by name or position by leaving the other parameter empty:

/Employee/Search?name=John will search by name only.


/Employee/Search?position=Manager will search by position only.
Advantages of Dynamic Parameters:
Flexible Queries: You can easily extend the stored procedure to accept more
parameters, such as Salary, Department, etc., without changing the code in the
controller or repository.
Reusability: This method is reusable for different search scenarios.
Improved Performance: By filtering only when necessary (avoiding unnecessary
database scans), you improve performance.
Cleaner Code: You avoid writing separate methods for each filter condition (e.g.,
SearchByName, SearchByPosition), making your code cleaner and more maintainable.

You might also like