Question

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.

Was it helpful?

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

OTHER TIPS

Finally I Decided to use Like and joining them with OR. So i do iterate on list of items and add like filter and attach them with or something like this:

string[] allStr = recieveFilter.Split(',');
string filterRecieverItem = "";
foreach (string recieveItem in allStr)
{
   filterRecieverItem += "(sampleField like '%" + recieveItem.Trim() + "%') OR ";
}
filterRecieverItem = filterRecieverItem.Remove(filterRecieverItem.Length - 3, 3);
ds_Sample.FilterExpression = filterStr;

Hope this help.

I also suggest you do something like by user VisDev String.Format("SELECT mycolumns FROM mytables WHERE GROUP_NAME IN ({0})", yourparamlist)

Where yourparamlist would be

"value1, value2, value3" its just a hint so you can implement it according to your requirement.

Hope it will solve your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top