Question : C# ,framework 2.0 , bindingsource filter question


I have the following query

SELECT     *
FROM         ORDERS
WHERE     (Product IN
                          (SELECT     ID_PRODUCT
                            FROM          Products
                            WHERE      Description LIKE N'a%'))     -->'a' or whatever is assigned by textBox Entry.

I have a DataGridView that has datasource an OrderBindingSource.
The OrderBindingSource has datasource  a dataset and datamember the ORDERS.
How can I set this query in the OrderBindingSource  Filter?

With regards,
nikavak.

Answer : C# ,framework 2.0 , bindingsource filter question

you can create a sqlcommand with every query you want
and then call binding source with this command
look at sample code below:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
public static BindingSource GetRecordsById(int id)
{
    //query to execute
    string query = "SELECT * FROM YourTable WHERE YourID = @id";
    //OleDbConnection Object to use
    OleDbConnection cnGetRecords = new     OleDbConnection(amaDBHelper.GetConnectionString("YourConnectionName"));
    //OleDbCommand Object to use
    OleDbCommand cmdGetRecords = new OleDbCommand();
    //OleDbDataAdapter Object to use
    OleDbDataAdapter daAgents = new OleDbDataAdapter();
    DataSet dsGetRecords = new DataSet();
    //Clear any parameters
    cmdGetRecords.Parameters.Clear();
    try
      {
        //set the OleDbCommand Object Parameters
        cmdGetRecords.CommandText = query; //tell it what to execute
        cmdGetRecords.CommandType = CommandType.Text; //tell it its executing a query
        //heres the difference from the last method
        //here we are adding a parameter to send to our query
        //you use the AddWithValue, then the name of the parameter in your query
        //then the variable that holds that value
        cmdGetRecords.Parameters.AddWithValue("@year", id);
        //set the state of the OleDbConnection Object
        DataAccess.HandleConnection(cnGetRecords);
        //create BindingSource to return for our DataGrid Control
        BindingSource oBindingSource = DataAccess.GetBindingSource(cmdGetRecords);
        //now check to make sure a BindingSource was returned
        if (!oBindingSource == null)
         {
            //return the binding source to the calling method
            return oBindingSource;
         }
            else //no binding source was returned
                {
                    //let the user know the error
                    throw new exception("There was no BindingSource returned");
                }
         }
        catch (Exception ex)
        {
            MsgBox(ex.Message, "Error Retrieving Data");
        }
        //now we close the connection
        DataAccess.HandleConnection(cnGetRecords);
}
Random Solutions  
 
programming4us programming4us