Search code examples
c#.netvisual-studio-2010sqldatasourcewhere-in

SQL DataSource using WHERE IN clause


Using Studio 2010 C# with DB2 UDB 9.7.5

I have a gridview being populated from a DataSource with a WHERE IN clause as follows:

<asp:SqlDataSource ID="LUWAccts" runat="server" onSelecting="gvGrid01_Selecting"
ConnectionString="<%$ ConnectionStrings:LUWHAConnect %>" 
ProviderName="<%$ ConnectionStrings:LUWHAConnect.ProviderName %>" 

SelectCommand="SELECT mycolumns FROM mytables WHERE GROUP_NAME IN ? ">
<SelectParameters>
  <asp:Parameter Name="grpList" Type="String" /> 
</SelectParameters>

Hardcoding the WHERE clause works fine (i.e. WHERE GROUP_NAME IN ('GROUP1','GROUP2','GROUP3')

But I am trying to build the group list using the onSelecting event of my data source in my code behind as follows:

protected void gvGrid01_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
  string gpList = Get_SVCACCT_Auth_Groups(Session["ThisUser"].ToString());
              //This routine returns the string "('GROUP1','GROUP2','GROUP3')"
}

When I display the grpList string I do in fact have ('GROUP1','GROUP2','GROUP3').

But I do not get any data displayed. I've searched and tried various techniques to alter the SELECT statement, modify the parm list using e.Command.Parameters.Add, using ControlParemeters, etc.

I also read a post about how WHERE IN clauses cannot handle comma-separated lists paremeters, but the IN keyword can query against a table.

My gpList is not built from another SELECT statement - but I still tried a few examples of this with no luck. Being a novice - I seem to be dancing around the solution.

Please let me know if I've missed a post that would help answer this. Any help would be appreciated.


Solution

  • You can try this in your gvGrid01_Selecting event

    LUWAccts.SelectCommand = 
    String.Format("SELECT mycolumns FROM mytables WHERE GROUP_NAME IN {0})",
    Get_SVCACCT_Auth_Groups(Session["ThisUser"].ToString()));
    

    Check this link