locked
How to get search result for order place by specific user? RRS feed

  • Question

  • User481677910 posted

    Hi,

    I am using membership where user can login and place an order. I would like to have a search function in it where he can place his old order name and it will show to him.

    Currently what I  am using code is showing all of the entries in database submitted by other client also.

            string constr = ConfigurationManager.ConnectionStrings["udsConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    string sql = "SELECT Order_Name, Date, Id FROM Customer_Order";
                   
     
                    
                    if (!string.IsNullOrEmpty(txtSearch.Text.Trim()))
                    {
                        
                        sql += " WHERE Order_Name LIKE @Order_Name + '%'";
                       
                        cmd.Parameters.AddWithValue("@Order_Name", txtSearch.Text.Trim());
                    }
                    cmd.CommandText = sql;
                    cmd.Connection = con;
                    using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        sda.Fill(dt);
                        gvCustomers.DataSource = dt;
                        gvCustomers.DataBind();
                    }
                }
            }

    Kindly help.

    Thursday, September 12, 2019 12:45 AM

Answers

  • User409696431 posted

    Example of what?  A Where clause with an AND?

     sql += " WHERE Order_Name LIKE @Order_Name + '%' AND Id = @id";
                       
    cmd.Parameters.AddWithValue("@Order_Name", txtSearch.Text.Trim());
    cmd.Parameters.AddWithValue("@id", your user's id)

    It's up to you to replace "your user's id" with the actual user's id.  You haven't given any information on how you know it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 12, 2019 3:12 AM

All replies

  • User409696431 posted

    Where is the order associated with the person who ordered it?  You need to have that connection in order to search only by that person's orders.  Otherwise, you'll have to search by specific order name, no "LIKE".

    Thursday, September 12, 2019 1:18 AM
  • User481677910 posted

    KathyW

    Where is the order associated with the person who ordered it?  You need to have that connection in order to search only by that person's orders.  Otherwise, you'll have to search by specific order name, no "LIKE".

    Every user has a unique Id attached to him. 

    What will be the correct command

     
    string sql = "SELECT Order_Name, Date, Id FROM Customer_Order" Where USERNAME = USERNAME ; 

    Thursday, September 12, 2019 2:02 AM
  • User409696431 posted

    Is the id in the order table the user's id?  If it is, you need to get the user's id, and add an AND condition to your WHERE clause to include matching that id to the user's id.

    Thursday, September 12, 2019 2:08 AM
  • User481677910 posted

    Is the id in the order table the user's id?  If it is, you need to get the user's id, and add an AND condition to your WHERE clause to include matching that id to the user's id.

    Do you have any example please?

    Thursday, September 12, 2019 2:36 AM
  • User409696431 posted

    Example of what?  A Where clause with an AND?

     sql += " WHERE Order_Name LIKE @Order_Name + '%' AND Id = @id";
                       
    cmd.Parameters.AddWithValue("@Order_Name", txtSearch.Text.Trim());
    cmd.Parameters.AddWithValue("@id", your user's id)

    It's up to you to replace "your user's id" with the actual user's id.  You haven't given any information on how you know it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 12, 2019 3:12 AM
  • User481677910 posted

    Thank you it worked.

    I have a question if I want to search either using order_name or by date what will be command for where

    WHERE Order_Name LIKE @Order_Name + '%' OR @date AND Id = @id";

    Currently above I tried not working

    Thursday, September 12, 2019 2:52 PM
  • User61956409 posted

    Hi new2world2015,

    if I want to search either using order_name or by date what will be command for where

    You can try following WHERE clause: 

    WHERE (Order_Name LIKE @Order_Name + '%' OR [Date] = @date) AND Id = @id

    With Regards,

    Fei Han

    Friday, September 13, 2019 8:26 AM