none
Filtering data in a binding source problem RRS feed

  • Question

  • I am new to coding, but I am having a good time learning. I hit a road block here and hoping that someone can help.

     

    I am using C# Express.

     

    I have an access database that is used by a third party program, so this database structure should not be not be changed. All I am wanting to do is pull info off of three tables into list boxes (I have already accomplished this.) and use the selected items in each listbox to filter a fourth table and return a single value from this table to a textbox or label. When I try to filter the information in the fourth table I get an exception "Syntax error: Missing operand after '4' operator.".  This "4" is part of the Serial Number Prefix data "4NZ" that I want to filter in table four. Below is the code I used:

     

    this.z_final_totalsBindingSource.Filter = "strSNPrefix = ''" + serialPrefixListBox.SelectedValue;

     

    If I take out the single quotes after "strSNPrefix =" then it throws this exception:

     

    "Syntax error: Missing operand after 'NZ' operator."

     

    I do not understand why it is separating the 4NZ value from the listbox.

    I have looked through other post and all it seems my situation is more unique than others. Maybe not. I am just having trouble relating other peoples situations to mine.

     

    Thanks in advance for any help.

    Monday, December 31, 2007 6:28 PM

Answers

  • The value needs to be surrounded by single quotes. Try this code instead:

     

    Code Block
    this.z_final_totalsBindingSource.Filter = String.Format("strSNPrefix = '{0}'", serialPrefixListBox.SelectedValue);

     

     

     

    Monday, December 31, 2007 8:34 PM

All replies

  • The value needs to be surrounded by single quotes. Try this code instead:

     

    Code Block
    this.z_final_totalsBindingSource.Filter = String.Format("strSNPrefix = '{0}'", serialPrefixListBox.SelectedValue);

     

     

     

    Monday, December 31, 2007 8:34 PM
  • I appreciate your help

     

    This seem to take away any errors that I had before, but can I run a check some how to see if it is filtering data the way I need to.

     

    I tried running the program and then previewing the data in the dataset via the form.cs[design] page. It showed the column names, no data.

     

    Can I run these one after the other to do a multiple filter on a table? For example, instead of filtering just the serial prefix column, I need to filter the Model Column, serial prefix column, and Begining serial number column.

     

     

     

    Tuesday, January 8, 2008 2:39 PM
  • Could you please explain what is going on in the code block that you told me to use. I want to make sure that I understand everything fully.

     

    this.z_final_totalsBindingSource.Filter

    //this is telling the program what binding source to filter?

     

    = String.Format(...

    //this is telling it to put it in a string format

     

    ..."strSNPrefix = '{0}'"...

    //this is telling it to set the filtered column of strSNPrefix to any value? or to the value of zero?

     

    ..., serialPrefixListBox.SelectedValue);

    //this is telling it to filter the column above using the selected value in the listbox to get only those entries in the table with this value?

     

    Tuesday, January 8, 2008 2:53 PM
  • String.Format will replace {0} with the second parameter (in this case serialPrefixListBox.SelectedValue).

     

    If you want to do further filters, put in the string after {0}'   " and someOtherString='{1}'"  then add another paramter to the String.Format call to fill in {1}.

     

    See this msdn article for more information: http://msdn2.microsoft.com/en-us/library/system.string.format.aspx

    Tuesday, January 8, 2008 3:02 PM
  • How can I go about viewing the data to see the results of the filter?

     

    Friday, January 11, 2008 6:56 PM
  • I suppose you'll have to do that by seeing the results on the screen or using a breakpoint to view the locals. Of course, you could write a unit test to make sure your filters are working.

     

    Friday, January 11, 2008 7:09 PM
  •  

    Simply you can write down like this :

     

    this.z_final_totalsBindingSource.Filter = "strSNPrefix = ' " + serialPrefixListBox.SelectedValue+" ' ";

     

    To see the result you can use Watch , locals window etc or you can bind the result to another datagrid. But instead of binding grid with the DataTable or DataSet uset DataView.

     

    Regards,

    Shahid Riaz Bhatti

    MCP, MCAD

    Friday, January 11, 2008 8:48 PM
  • This makes since to me now. I can see that my original structure was just a little off. Thank both of you very much for your help. Now on to figuring out how to get the single value from the total cost column, that is now filtered, to the text box, then I am done with program.

     

    Friday, January 11, 2008 9:34 PM