Implement search web page using ASP NET and Stored Procedure

Implement search web page using ASP NET and Stored Procedure

Text version of the video
http://csharp-video-tutorials.blogspot.com/2017/03/implement-search-web-page-using-aspnet.html

Slides
http://csharp-video-tutorials.blogspot.com/2017/03/implement-search-web-page-using-aspnet_30.html

All SQL Server Text Articles
http://csharp-video-tutorials.blogspot.com/p/free-sql-server-video-tutorials-for.html

All SQL Server Slides
http://csharp-video-tutorials.blogspot.com/p/sql-server.html

All SQL Server Tutorial Videos

All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd

All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatArabic/playlists

In this video we will discuss implementing a search web page using ASP.NET and Stored Procedure.

Step 1 : Modify the “spSearchEmployees” stored procedure to include NULL as the default value for the parameters. The advantage of specifying default value for the parameters is that the ASP.NET page need not pass those parameters when calling the stored procedures if the user did not specify any values for the corresponding search fields on the Search Page.

Alter Procedure spSearchEmployees
@FirstName nvarchar(100) = NULL,
@LastName nvarchar(100) = NULL,
@Gender nvarchar(50) = NULL,
@Salary int = NULL
As
Begin

Select * from Employees where
(FirstName = @FirstName OR @FirstName IS NULL) AND
(LastName = @LastName OR @LastName IS NULL) AND
(Gender = @Gender OR @Gender IS NULL) AND
(Salary = @Salary OR @Salary IS NULL)
End
Go

Step 2 : Create a new empty ASP.NET Web Forms application. Name it “DynamicSQLDemo”.

Step 3 : Add a connection string to your database in web.config. For the connection string please check my blog at the following link
http://csharp-video-tutorials.blogspot.com/2017/03/implement-search-web-page-using-aspnet.html

Step 4 : Add a WebForm to the project. Name it “SearchPageWithoutDynamicSQL.aspx”

Step 5 : Copy and paste the HTML on my blog at the following link on the ASPX page. Notice we are using Bootstrap to style the page. If you are new to Bootstrap, please check out our Bootstrap tutorial for beginners playlist.

Step 6 : Copy and paste the following code in the code-behind page. Notice we are using the stored procedure “spSearchEmployees”. We are not using any dynamic SQL in this example. In our next video, we will discuss implementing the same “Search Page” using dynamic sql and understand the difference between using dynamic sql and stored procedure.

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace DynamicSQLDemo
{
public partial class SearchPageWithoutDynamicSQL : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{}

protected void btnSearch_Click(object sender, EventArgs e)
{
string strConnection = ConfigurationManager.ConnectionStrings[“connectionStr”].ConnectionString;

using (SqlConnection con = new SqlConnection(strConnection))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = “spSearchEmployees”;
cmd.CommandType = CommandType.StoredProcedure;

if (inputFirstname.Value.Trim() != “”)
{
SqlParameter param = new SqlParameter(“@FirstName”, inputFirstname.Value);
cmd.Parameters.Add(param);
}

if (inputLastname.Value.Trim() != “”)
{
SqlParameter param = new SqlParameter(“@LastName”, inputLastname.Value);
cmd.Parameters.Add(param);
}

if (inputGender.Value.Trim() != “”)
{
SqlParameter param = new SqlParameter(“@Gender”, inputGender.Value);
cmd.Parameters.Add(param);
}

if (inputSalary.Value.Trim() != “”)
{
SqlParameter param = new SqlParameter(“@Salary”, inputSalary.Value);
cmd.Parameters.Add(param);
}

con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
gvSearchResults.DataSource = rdr;
gvSearchResults.DataBind();
}
}
}
}

Get Paid Taking Pictures
Share