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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:46:37+00:00 2026-05-26T19:46:37+00:00

So I have my SqlDataSource with a SelectQuery defined as follows: SELECT * FROM

  • 0

So I have my SqlDataSource with a SelectQuery defined as follows:

 SELECT * FROM table 
 WHERE UserName IN (@EmployeesIn);

With @EmployeesIn coming from a session variable Session[“EmployeesIn”]. During Page_Load I’m taking an ArrayList members and putting the results into a string and setting the session variable:

 string employeesIn = "";
 foreach (string s in members)
 {
      employeesIn = employeesIn + "'" + s + "',";
 }

 employeesIn = employeesIn.TrimEnd(',');
 Session["EmployeesIn"] = employeesIn;

Writing the output to the console I can see the value of the parameter @EmployeesIn

 @EmployeesIn = 'bob', 'joe'

However, I’m getting zero results back … and after monitoring from the database level I see the parameter is coming in as:

'''bob'',''joe'''

Then again if I just pass in one employee, I get results back from the SQL as expected and the parameter is passed correctly as just ‘bob’. I suppose this is some safety that .NET provides against SQL injection attacks, however what’s the safe way around this?

  • 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-26T19:46:37+00:00Added an answer on May 26, 2026 at 7:46 pm

    You should absolutely use parameters for this, instead of including the values within the SQL itself. You can just generate the names for the parameters, so if you had three entries you’d generate SQL of:

    SELECT * FROM table WHERE UserName IN (@p0, @p1, @p2)
    

    and then fill in those three parameters from the three values.

    // Or create the command earlier, of course
    List<SqlParameter> parameters = new List<SqlParameter>();
    
    StringBuilder inClause = new StringBuilder("(");
    for (int i = 0; i < members.Count; i++)
    {
        string parameterName = "@p" + i;
        inClause.Append(parameterName);
        if (i != members.Count - 1)
        {
            inClause.Append(", ");
        }
        // Adjust data type as per schema
        SqlParameter parameter = new SqlParameter(parameterName, SqlDbType.NVarChar);
        parameter.Value = members[i];
        parameters.Add(parameter);
    }
    inClause.Append(")");
    
    // Now use inClause in the SQL, and parameters in the command parameters
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SqlDataSource with following command: SELECT * FROM [vw_aspnet_MembershipUsers] WHERE ([UserId] =
I have a GridView and SqlDataSource for it. SelectCommand is something like SELECT *
i have a gridview which is populating from sql datasource defined at designed, i
I have an ASP SqlDataSource connected to Sybase DB query, using Select Parameters that
I have an AjaxControlToolkit ReorderList bound by a SQLDataSource to a table with the
I have a GridView in ASP.net which is filled from an SqlDataSource. one of
I have this situation where I have a SqlDatasource control and the select query
I have a SqlDataSource retrieve tabular data from a store procedure. How can I
I have a sqldatasource with a very simple select statement that should always return
On an ASP.NET page, I have a SqlDataSource configured with the following SELECT command:

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.