Here is the solution (Whole project )
Please copy all .net code and make separate project and adjust database connection string as well
Please run stored procedure in to your database
ASPX code
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:label id="Label1" runat="server" text="ProductId" xmlns:asp="#unknown"></asp:label>
<asp:textbox id="txtProductId" runat="server" width="243px" xmlns:asp="#unknown"></asp:textbox>
<asp:label id="Label2" runat="server" text="ProductName" xmlns:asp="#unknown"></asp:label>
<asp:textbox id="txtProductName" runat="server" width="243px" xmlns:asp="#unknown"></asp:textbox>
<asp:label id="Label3" runat="server" text="Supplier Name" xmlns:asp="#unknown"></asp:label>
<asp:textbox id="txtSupplierName" runat="server" width="243px" xmlns:asp="#unknown"></asp:textbox>
<asp:button id="btnProductSearch" runat="server" onclick="btnProductSearch_Click" text="Product Search" xmlns:asp="#unknown">
Width="153px" />
<asp:gridview id="GridView1" runat="server">
</asp:gridview>
</asp:button></div>
</form>
</body>
</html>
C# Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace _346216_Need_query_and_csharp_code_for_search_criteria
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnProductSearch_Click(object sender, EventArgs e)
{
SqlConnection conn = null;
DataTable dt = new DataTable();
try
{
conn = new
SqlConnection("Server=MDT765;DataBase=TST;User Id=TestUser;Password=TestUser@123;Integrated Security=SSPI");
conn.Open();
SqlCommand cmd = new SqlCommand(
"ProductSearch", conn);
cmd.CommandType = CommandType.StoredProcedure;
if( txtProductId.Text!= string.Empty)
cmd.Parameters.Add(
new SqlParameter("@ProductID", txtProductId.Text.ToString()));
if (txtProductName.Text != string.Empty)
cmd.Parameters.Add(
new SqlParameter("@ProductName", txtProductName.Text.ToString()));
if (txtSupplierName.Text != string.Empty)
cmd.Parameters.Add(
new SqlParameter("@SupplierName", txtSupplierName.Text.ToString()));
dt.Load(cmd.ExecuteReader());
GridView1.DataSource = dt;
GridView1.DataBind();
}
finally
{
}
}
}
}
SQL Code : stored procedure
CREATE PROCEDURE ProductSearch
(
@ProductID int =NULL
,@ProductName varchar(50) = NULL
,@SupplierName varchar(50) = NULL
)
AS
IF (@ProductId is null AND @ProductName IS null AND @SupplierName is null )
BEGIN
RETURN -1
END
SELECT DISTINCT * FROM Product
WHERE ProductId=COALESCE(@ProductId,-1)
OR ProductName like COALESCE('%' + @ProductName + '%' ,'')
OR SupplierName =COALESCE('%' + @SupplierName + '%' ,'')
Hope this helps if yes then accept and vote the answer
--Rahul D.