Question

I'm trying to narrow down the rows that are in my DataView based on a relation with another table, and the RowFilter I'm using is as follows;

dv = new DataView(myDS.myTable,
                 "id IN (SELECT DISTINCT parentID FROM myOtherTable)",
                 "name asc",
                 DataViewRowState.CurrentRows);

"myTable" and "myOther" table are related via myTable.ID and myOtherTable.parentID, and so the idea is that the DataView should only contain rows from "myTable" which have corresponding child rows in "myOtherTable".

Unfortunately, I'm getting this error;

Syntax error: Missing operand after 'DISTINCT' operator.

The SQL is fine as far as I am aware, so I'm wondering is there some limitation on using the DISTINCT keyword as part of RowFilter's SQL? Anyone have any idea?

Was it helpful?

Solution

Unfortunately, I don't think you can perform a subquery in a DataView's filter expression. You're only allowed to use a subset of SQL in some expressions (documented here).

You'll probably need to perform your subquery (SELECT DISTINCT parentID FROM myOtherTable) separately.

This article describes the problem and a possible solution.

OTHER TIPS

Unfortunately you can't do it that way, as the RowFilter property does not support the distinct keyword. Here is the list of expressions you can perform in a RowFilter (which is just a DataColumn Expression): http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx

DataViews have a ToTable method, and several overloads take a boolean to specify whether to return only the distinct rows.

Here is one method: http://msdn.microsoft.com/en-us/library/wec2b2e6.aspx

Here is how you would use it:

DataTable newDataTable = myDataView.ToTable( true, [array of column names as strings] );

DataView dvBindAssignedByDropDown = new DataView();

DataTable dtBindAssignedByDropDown = new DataTable();

dvBindAssignedByDropDown = ds.Tables[0].DefaultView;


string[] strColnames=new string[2];

strColnames[0] = "RedNames";

strColnames[1] = "RedValues";

dtBindAssignedByDropDown = dvBindAssignedByDropDown.ToTable(true, strColnames);

ddlAssignedby.DataTextField = "RedNamesNames";
ddlAssignedby.DataValueField = "RedNames";
ddlAssignedby.DataSource = dtBindAssignedByDropDown;
ddlAssignedby.DataBind();
ddlAssignedby.Items.Insert(0, "Assigned By");
ddlAssignedby.Items[0].Value = "0";

the following code is extracting distinct values/records from a table/dataview, namely(PROD_DESP_TRN) having field(CONTAINER_NO) Finally, this code is filling a combobox(cmbContainerNo) with unique values/records

Form Level Declaration:

Dim dsLocal As DataSet 
Dim dvm As DataViewManager
Private Sub FillcomboContainer()

    Try
        Dim dv As DataView = New DataView

        cmbContainerNo.DataSource = Nothing
        dv = dvm.CreateDataView(dsLocal.Tables("PROD_DESP_TRN"))
        dv.Sort = "CONTAINER_NO"

        cmbContainerNo.DataSource = dv.ToTable(True, "CONTAINER_NO")
        cmbContainerNo.DisplayMember = "CONTAINER_NO"

    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
    End Try
End Sub

Try just leaving out the "DISTINCT". In this case, the results should be the same with or without. Troubleshoot from there.

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