Olivier Tremblay
Olivier Tremblay

Reputation: 1431

sqldatasource.select not taking account of FilterExpression on explicit select call?

Meh! Can't understand what happens here.

The filter is correctly applied when used on SqlDataSource1, to correctly populate the corresponding gridview.

That being said, the filtering isn't applied on the manual select that I use to get a DataView, for the database insertion part.

What am I doing wrong? Is there something going on that I'm not aware of?

SqlDataSource1.FilterExpression = (string)ViewState["filtre"];

    cmdApply.Text = (string)ViewState["filtre"];

    DataView thingie =     (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);

    thingie.RowFilter = (string)ViewState["filtre"];

    DataView verifdv =     (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);

    verifdv.Sort = "filterClient";

    SqlConnection dbconn = new SqlConnection(SqlDataSource1.ConnectionString);        

    foreach ( DataRow dr in thingie.Table.Rows ) {

        if ( verifdv.Find(dr["orgID"]) == -1 ) {

        SqlCommand addFilter = new SqlCommand("INSERT INTO dbo.usermetafilter     (filterUser, filterClient) VALUES (@user, @client)", dbconn);

                addFilter.Parameters.Add("@user", SqlDbType.NVarChar).Value = "dummyvalue";

                addFilter.Parameters.Add("@client", SqlDbType.Int).Value = dr["orgID"];

                addFilter.Connection.Open();

                addFilter.ExecuteNonQuery();

                addFilter.Connection.Close();

            }

        }

Upvotes: 1

Views: 814

Answers (1)

Olivier Tremblay
Olivier Tremblay

Reputation: 1431

Nevermind. The correct code should have been:

foreach ( DataRow dr in thingie.Table.Select((string)ViewState["filtre"];

) ) {

The Select method for the datasource doesn't use the filter in the DS' FilterExpression property. It gets the whole thing. On the other hand, the DataView.Table.Select(String FilterExpression) returns an array of all its rows, filtered. How cool is that, I solved my own problem.

Upvotes: 1

Related Questions