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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:26:22+00:00 2026-05-14T08:26:22+00:00

I have to make a string by using the values which the user selects

  • 0

I have to make a string by using the values which the user selects on the webpage,

Suppose I need to display files for multiple machines with different search criteria…

I currently use this code:

DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection();
connection.ConnectionString = ConfigurationManager
               .ConnectionStrings["DBConnectionString"].ConnectionString;
connection.Open();
SqlCommand sqlCmd = new SqlCommand
  ("SELECT FileID FROM Files
    WHERE MachineID=@machineID and date= @date", connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

sqlCmd.Parameters.AddWithValue("@machineID", machineID);
sqlCmd.Parameters.AddWithValue("@date", date);

sqlDa.Fill(dt);

Now this is a fixed query where the user just has one machine and just selects one date…

I want to make a query in which the user has multiple search options like type or size if he/she wants depending on what he/she selects.

Also if he/she can select multiple machines…

SELECT FileID FROM Files
WHERE (MachineID=@machineID1 or MachineID = @machineID2...)
AND (date= @date and size=@size and type=@type... )

All of this happens at runtime… otherwise I have to create a for loop to put multiple machines one by one… and have multiple queries depending on the case the user selected…

This is quite interesting and I could use some help…

  • 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-14T08:26:23+00:00Added an answer on May 14, 2026 at 8:26 am

    If you are going to do this via dynamic SQL, you need to build a call to the IN function. (e.g. In(id1, id2, id3…)

    private string GetSql( IList<int> machineIds )
    {
        var sql = new StringBuilder( "SELECT FileID FROM Files Where MachineID In(" );
        for( var i = 0; i < machineIds.Count; i++ )
        {
            if ( i > 0 )
                sql.Append(", ")
            sql.Append("@MachineId{0}", i);
        }
    
        sql.Append(" ) ");
    
        //additional parameters to query
        sql.AppendLine(" And Col1 = @Col1" );
        sql.AppendLine(" And Col2 = @Col2 ");
        ...
    
        return sql.ToString();
    }
    
    private DataTable GetData( IList<int> machineIds, string col1, int col2... )
    {
        var dt = new DataTable();
        var sql = GetSql( machineIds );
        using ( var conn = new SqlConnection() )
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
            using ( var cmd = new SqlCommand( sql, conn ) )
            {
                conn.Open();
    
                for( var i = 0; i < machineIds.Count; i++ )
                {
                    var parameterName = string.Format("@MachineId{0}", i );
                    cmd.Parameters.AddWithValue( parameterName, machineIds[i] );
                }
    
                cmd.Parameters.AddWithValue( "@Col1", col1 ); 
                cmd.Parameters.AddWithValue( "@Col2", col2 ); 
                ...
    
                using ( var da = new SqlDataAdapter( cmd ) )
                {
                    da.Fill( dt );
                }
            }
        }
    
        return dt;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 430k
  • Answers 430k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer (Updated, since previous answer was wrong.) The problem may be… May 15, 2026 at 1:58 pm
  • Editorial Team
    Editorial Team added an answer This has nothing to do with Subsonic, nor is it… May 15, 2026 at 1:58 pm
  • Editorial Team
    Editorial Team added an answer SELECT * FROM comments WHERE approved = 1 LIMIT 3,SOME_HUGE_NUMBER… May 15, 2026 at 1:58 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.