Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
if user want to search for product he can enter any one option example he can enter product id or user wants to enter both produtid and prodcutname and suppliername what will be the query to search.

Information:- My table is like this

table Name:- Product

(ProductID int primary key not null, ProductName varchar(50) not null, ProductDescription varchar(100), ProductPrice int not null,
SupplierName varchar(50))

my code :-
private DataSet ProductSearch()
       {
           System.Text.StringBuilder sb = new System.Text.StringBuilder();
           DataSet ds = new DataSet();

           sb.Append("select * from Products");

           if (!(string.IsNullOrEmpty(Convert.ToString(ProductID))))
           {
               sb.Append(" where ");
               sb.Append("ProductID like'").Append(ProductID).Append("%'");
           }
           if (!(string.IsNullOrEmpty(ProductName)))
           {
               sb.Append(" where ");
               sb.Append("ProductName like'").Append(ProductName).Append("%'");
           }

           if (!(string.IsNullOrEmpty(SupplierName)))
           {
               sb.Append(" where ");
               sb.Append("SupplierName like'").Append(SupplierName).Append("%'");
           }

           try
           {
               OpenConnection();
               SqlCommand command = new SqlCommand(sb.ToString(), connection);
               command.CommandType = CommandType.Text;
               SqlDataAdapter da = new SqlDataAdapter(command);
               da.SelectCommand = command;
               da.Fill(ds);
           }
           catch { }
           finally
           {
               CloseConnection();
           }
           return ds;
       }


if above code is not working correc can any one provide code for this functionality.

Thanks
Posted
Updated 13-Mar-12 20:53pm
v2

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

XML
<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

C#
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

SQL
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.
 
Share this answer
 
v3
Comments
RDBurmon 14-Mar-12 5:02am    
See my updated solution
RDBurmon 14-Mar-12 5:02am    
and thanks for acceptance and voting
RDBurmon 14-Mar-12 6:46am    
Adjusted first tag.
I want give an idea : make sure the sb.ToString() is correct statement return.
Use it: response.write(sb.ToString()); this provide you select query execute it in Query-Analyzer. Get your needy results . I think it help u!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900