Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i Try search textBox Value From DataBase and i use this Code and it's Work fine

C#
private DataTable PopulateDataGridView()
        {
            
                string query = "SELECT Number, CustomerName, MobileNo, SecMobileNo FROM customer";
                query += " WHERE Number LIKE '%' + @SearchTerm + '%'";
                query += " OR CustomerName LIKE '%' + @SearchTerm + '%'";
                query += " OR MobileNo LIKE '%' + @SearchTerm + '%'";
                query += " OR SecMobileNo LIKE '%' + @SearchTerm + '%'";
                query += " OR @SearchTerm = ''";
                string constr = ConfigurationManager.ConnectionStrings["TestData"].ConnectionString;
                using (OleDbConnection con = new OleDbConnection(constr))
                {
                    using (OleDbCommand cmd = new OleDbCommand(query, con))
                    {
                        cmd.Parameters.AddWithValue("@SearchTerm", txtName.Text.Trim());
                        using (OleDbDataAdapter sda = new OleDbDataAdapter(cmd))
                        {
                            DataTable dt = new DataTable();
                            sda.Fill(dt);
                            return dt;
                        }
                    }
                }
            }



but When i Try in my datatable show and search only the Data Whose Status Columns Value is "OPEN" But my Code not Work and data show like my Above/Old Code without any filter

What I have tried:

C#
private DataTable PopulateDataGridView()
        {
            string query = "SELECT Number, CustomerName, MobileNo, SecMobileNo, Status FROM customer";
                query += " WHERE Number LIKE '%' + @SearchTerm + '%'";
                query += " OR CustomerName LIKE '%' + @SearchTerm + '%'";
                query += " OR MobileNo LIKE '%' + @SearchTerm + '%'";
                query += " OR SecMobileNo LIKE '%' + @SearchTerm + '%'";
                query += " OR @SearchTerm = ''";
                query += "AND Status=@Status";
                string constr = ConfigurationManager.ConnectionStrings["TestData"].ConnectionString;
                using (OleDbConnection con = new OleDbConnection(constr))
                {
                    using (OleDbCommand cmd = new OleDbCommand(query, con))
                    {
                        cmd.Parameters.AddWithValue("@SearchTerm", txtName.Text.Trim());
                        cmd.Parameters.AddWithValue("@Status", "OPEN");
                        using (OleDbDataAdapter sda = new OleDbDataAdapter(cmd))
                        {
                            DataTable dt = new DataTable();
                            sda.Fill(dt);
                            return dt;
                        }
                    }
                }
        }
Posted
Updated 21-May-20 6:13am
v2
Comments
ZurdoDev 21-May-20 11:37am    
You have to debug this. What did you want us to do?

Your query is:
SQL
WHERE 
    Number LIKE ...
OR 
    CustomerName LIKE ...
OR 
    MobileNo LIKE ...
OR 
    SecMobileNo LIKE ...
OR 
    @SearchTerm = ''
AND 
    Status = @Status
Adding parentheses to match the implicit operator precedence[^] gives:
SQL
WHERE 
    Number LIKE ...
OR 
    CustomerName LIKE ...
OR 
    MobileNo LIKE ...
OR 
    SecMobileNo LIKE ...
OR 
(
    @SearchTerm = ''
AND 
    Status = @Status
)
In other words, if the Number, CustomerName, MobileNo, or SecMobileNo match, the record will be included regardless of the Status.

To override the operator precedence, you will need to explicitly add parentheses to your query:
SQL
WHERE 
(
    Number LIKE ...
OR 
    CustomerName LIKE ...
OR 
    MobileNo LIKE ...
OR 
    SecMobileNo LIKE ...
OR 
    @SearchTerm = ''
)
AND 
    Status = @Status
In code:
C#
string query = "SELECT Number, CustomerName, MobileNo, SecMobileNo, Status FROM customer";
query += " WHERE (Number LIKE '%' + @SearchTerm + '%'";
query += " OR CustomerName LIKE '%' + @SearchTerm + '%'";
query += " OR MobileNo LIKE '%' + @SearchTerm + '%'";
query += " OR SecMobileNo LIKE '%' + @SearchTerm + '%'";
query += " OR @SearchTerm = '')";
query += "AND Status = @Status";
NB: It may be easier to see the query if you use a verbatim string[^]:
C#
const string query = @"SELECT 
    Number, 
    CustomerName, 
    MobileNo, 
    SecMobileNo, 
    Status 
FROM 
    customer
WHERE
(
    Number LIKE '%' + @SearchTerm + '%'
Or
    CustomerName LIKE '%' + @SearchTerm + '%'
Or
    MobileNo LIKE '%' + @SearchTerm + '%'
Or
    SecMobileNo LIKE '%' + @SearchTerm + '%'
Or
    @SearchTerm = ''
)
And
    Status = @Status
";
 
Share this answer
 
Comments
Amar chand123 21-May-20 23:43pm    
Thank you sir Richard deeming, sir can you have a suggestion for me where i learn SQL syntax online because i just started learning programming from internet
Richard Deeming 22-May-20 5:37am    
It's a long time since I started, so I don't know what the best resources are any more. :)

This one looks like it might help:
SQL Tutorial - An Ultimate Guide for Beginners[^]
SQL
query += " OR @SearchTerm = ''";
query += "AND Status=@Status";

You are missing a space character between the two quotes ('') in the first of the above lines, and the AND in the second.
 
Share this answer
 
Comments
Amar chand123 21-May-20 5:10am    
I try but still not work
Amar chand123 21-May-20 5:27am    
and if i use this code then data show other then "OPEN" but Where Status is "OPEN" Data Not Show

string query = "SELECT Number, CustomerName, MobileNo, SecMobileNo, Status FROM customer WHERE Status=@Status ";
query += " OR Number LIKE '%' + @SearchTerm + '%'";
query += " OR CustomerName LIKE '%' + @SearchTerm + '%'";
query += " OR MobileNo LIKE '%' + @SearchTerm + '%'";
query += " OR SecMobileNo LIKE '%' + @SearchTerm + '%'";
query += " OR @SearchTerm = ' '";

string constr = ConfigurationManager.ConnectionStrings["TestData"].ConnectionString;
using (OleDbConnection con = new OleDbConnection(constr))
{
using (OleDbCommand cmd = new OleDbCommand(query, con))
{
cmd.Parameters.AddWithValue("@SearchTerm", txtName.Text.Trim());
cmd.Parameters.AddWithValue("@Status", "OPEN");
using (OleDbDataAdapter sda = new OleDbDataAdapter(cmd))
{

DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
Richard MacCutchan 21-May-20 5:28am    
Your code is checking if the status equals "OPEN", OR if any of the other conditions is true. Try a simple test with one search term and one status just to get your syntax correct. And please read carefully what you have coded.

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