Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6977643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:39:08+00:00 2026-05-27T17:39:08+00:00

string sql = select * from customer where name like ‘ + textBox2.Text +

  • 0
string sql = "select * from customer where name like '" + textBox2.Text + "%'";
string sql2 = "select * from customer";

if (textBox2.Text.Length > 0)
{
    DataTable dt = CarDatabase.executeSelect(sql);
    DataTable dt2 = CarDatabase.executeSelect(sql2);

    if (dt == null)
    {
        dataGridView2.DataSource = dt2;
        MessageBox.Show("There's no result with " + textBox2.Text);
    }
    else if (dt != null)
        dataGridView2.DataSource = dt;
}
else
{
    MessageBox.Show("Please fill the textbox");
}

I try to do “if like has some result, show it in DataGrid“. There’s no problem with that. However, when like finds nothing in the database, the DataGrid stays old. However, if after searching there is no result, the DataGrid is empty.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:39:08+00:00Added an answer on May 27, 2026 at 5:39 pm

    Well it is a good practice to use a stored procedure than injecting sql query from the code.

    I would have created a procedure something like this…

    Create Procedure GetCustomers(@name varchar(100))
    AS 
    BEGIN
     select * from customer  where Name like (ISNULL(@name,Name))
    END
    

    However while passing value to the @name parameter, you need to append ‘%’ to it. If textbox value is empty then pass null to @name. This query will return all customer if textbox value is empty else the requested customers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the code below: string SQL = select * from + TableName; using
I want to SELECT a formatted date string from a datetime type in SQL
My SQL looks something like this: SELECT CompanyName , LastName , FirstName FROM ...
public Cursor fetchAllPBs() { String sql = SELECT * FROM + CAPTURES_TABLE + GROUP
public static int AwaitingApprovals() { int numApprovals = 0; string sql = SELECT COUNT(Type)
I have a SqlDependency set up using the following query: string sql = "SELECT
Trying to parse an SQL string and pull out the parameters. Ex: select *
If I have the name of a database table like this: string tableName =
I am trying to select food_ItemName and food_UnitPrice from t_Food table in SQL Server
Possible Duplicate: Split string in SQL I have seen a couple of questions related

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.