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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:31:35+00:00 2026-05-31T14:31:35+00:00

Another annoying one for me but probably something simple. I have a number of

  • 0

Another annoying one for me but probably something simple.

I have a number of possible where clauses for a query based on user input, my question is how can I add these programmatically?

For instance:

wherequery = @"WHERE fieldname = @p_FieldName AND ";
if (txtValue.textLength > 0){
    wherequery += "fieldname2 = @p_FieldName2 AND ";
}
query = @"SELECT * FROM tabe" + wherequery;
sql = connection.CreateCommand();
sql.CommandText = query;

How would I go about doing the parameters for that? I’ve tried ArrayLists, Dictionaries and a few other methods but can’t find a way of doing it. Ideally I’d want to do something like this:

SqlParameter[] sqlparams;
wherequery = @"WHERE fieldname = @p_FieldName AND ";
if (txtValue.textLength > 0){
    wherequery += "fieldname2 = @p_FieldName2 AND ";
    sqlparams.Parameters.Add("@p_FieldName2 ", SqlDbType.VarChar).Value = txtValue.text;
}
query = @"SELECT * FROM tabe" + wherequery;
sql = connection.CreateCommand();
sql.CommandText = query;
sql.Parameters.Add(sqlparams);
  • 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-31T14:31:36+00:00Added an answer on May 31, 2026 at 2:31 pm

    I’ve made a small alteration to your code, which should hopefully point you in the right direction:

        sql = connection.CreateCommand();    
        wherequery = @"WHERE fieldname = @p_FieldName ";
        sql.Parameters.Add(new SqlParameter("@p_FieldName ", "some value for fieldname"));
    
        if (txtValue.textLength > 0){
            wherequery += " AND fieldname2 = @p_FieldName2 ";
            sql.Parameters.Add(new SqlParameter("@p_FieldName2 ", txtValue.text));
        }
        query = @"SELECT * FROM tabe" + wherequery;
    
        sql.CommandText = query;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting this annoying error in one environment but not another and I can't
Another question related to this one . I have a List<SortableObjects> that is the
I have 2 procedures. One that builds a temp table and another (or several
another newbie-question regarding rails and bootstrap. I am using something like this: <div class=tabbable
another problem. I have already placed my Jython.jar into what my computer recognizes as
Another jQuery question for you guys. Say I have a url of an image
I have a report that takes one parameter, and works fine. So I've put
I use Visual Studio 2008 primarily for web development, and there's one annoying behaviour
We have an application with multiple subsystems running in different threads (ie one thread
Update : I have tried using fuslogvw.exe to get logs. But what exactly am

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.