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 8431287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:48:51+00:00 2026-06-10T05:48:51+00:00

I have SQL Server database for users, and now I want to make simple

  • 0

I have SQL Server database for users, and now I want to make simple search algorithm for this table. My goal is to make algorithm that can combine search of multiple words, so I tried with this strategy: when search button is pressed, method first extracts individual words from search string and then inserts them as a parameters in SqlCommand CommandText.

It works for one word but it gets stuck when I type more than one word. Here is the code sample:

private void btnSearchUsers_Click(object sender, EventArgs e)
{
             command = new SqlCommand();
             adapter = new SqlDataAdapter();
             dataset = new DataSet();
            try
            {
                User.connection.Open();
                command.CommandText = "SELECT * FROM tbl_Users WHERE userActive = 1";
                if (!String.Empty.Equals(tboxInputUsers.Text))
                {
                    command.CommandText += " AND";
                    string[] words = tboxInputUsers.Text.Split(' ');
                    string id = "id";

                    foreach (string word in words)
                    {
                        id += "a";
                        command.CommandText += String.Format(" userUsername LIKE @{0} OR userName LIKE @{0} OR userSurname LIKE @{0}", id);
                        command.Parameters.AddWithValue(String.Format("@{0}", id), word);
                    }
                }
                command.Connection = User.connection;
                adapter.SelectCommand = command;

                adapter.Fill(dataset);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                if (ConnectionState.Open.Equals(User.connection.State)) Korisnik.connection.Close();
            }
            DataTable table = new DataTable();
            table = dataset.Tables[0];

            dgvUsers.DataSource = table;
            dgvUsers.Columns[0].Visible = false;
}

When I go to debug mode and type in two words, command.Parameters shows it has 2 parameters, but it cracks nevertheless. Any insights what is wrong?

Thanks!

== EDIT ==

I get this type of

SqlException: Incorrect syntax near ‘userUsername’

  • 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-06-10T05:48:53+00:00Added an answer on June 10, 2026 at 5:48 am

    I have yet to fully understand your code, but at first sight I say:
    You need parenthesis around your command text and you need to add the OR logical operator

    command.CommandText += String.Format(" (userUsername LIKE @{0} OR userName LIKE @{0} OR userSurname LIKE @{0}) OR ", id); 
    

    of course exiting from the loop you need to remove the final OR

    command.CommandText = command.CommandText.Substring(0, command.CommandText.Length - 4);
    

    I will try to rewrite your code using a simple string

    string sqlText = "SELECT * FROM tbl_Users WHERE userActive = 1"; 
    if (!String.Empty.Equals(tboxInputUsers.Text)) 
    { 
        // It's important to add an open parenthesis here to get a correct logic
        // All activeusers with names like words
        sqlText += " AND ("; 
        string[] words = tboxInputUsers.Text.Split(' '); 
        string id = "id"; 
    
        foreach (string word in words) 
        { 
            id += "a"; 
            sqlText += String.Format(" (userUsername LIKE @{0} OR " + 
                                       "userName LIKE @{0} OR " + 
                                       "userSurname LIKE @{0}) OR ", id); 
            command.Parameters.AddWithValue(String.Format("@{0}", id), word); 
        } 
        // We are sure that there is at least one word, so we can  remove the excess OR and close
        // the opening parenthesis following the AND
        sqlText = sqlText(0, sqlText.Length - 4);
        sqlText += ")";
    }
    command.CommandText = sqlText;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Microsoft SQL server database and a set of users. Now in
I have a simple SQL Server database that we use for conversions. Basically a
I have a SQL Server Database back-end. I want an internal network front-end UI,
I have a SQL Server database with 500,000 records in table main . There
I am using SQL SERVER 2005. I have a table table1(ID,col1,col2,col3,col4); Now I have
I have a database in SQL Server 2008 R2 Standard Edition. I want to
I have a 'reference' SQL Server 2005 database that is used as our global
We have an Access UI to Sql Server database. The user connects to many
I have created one user named tuser with create database rights in SQL server
I have created SQL Server database by Visual Studio 2010 Ultimate means in Visual

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.