Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please i tried to validate filtering of data. This is the method i used below but it didn't work instead it keeps showing Enter Details.
C#
private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != String.Empty && comboBox1.SelectedText != String.Empty)
            {
                var res = from result in ds.Results where result.REGISTRATION_NUMBER == textBox1.Text && result.EXAM_YR == comboBox1.SelectedItem select result;
                if (res != null)
                    resultBindingSource1.DataSource = res;
                else
                    MessageBox.Show("no data found");
            }
            else
            {
                MessageBox.Show("Enter details");
            }
        }
Posted
Comments
[no name] 8-Sep-13 17:08pm    
The only reason that this code would show "Enter details" all of the time is if textBox1.Text != String.Empty AND comboBox1.SelectedText != String.Empty. Are you sure that your logic is correct?
phil.o 8-Sep-13 19:26pm    
Not AND: OR ;)
(!A && !B) == !(A || B)
(!A || !B) == !(A && B)
[no name] 8-Sep-13 20:13pm    
Yes.... hence my questioning his logic... ;-)

Maybe try to replace
C#
result.EXAM_YR == comboBox1.SelectedItem
with
C#
result.EXAM_YR == comboBox1.SelectedText


[EDIT]: ThePhantomUpvoter's remark is right ; if you get "Enter details" message it means that either textBox1.Text is empty or comboBox1.SelectedText is empty. I would go for comboBox1.SelectedText. You could try to replace it by comboBox1.SelectedValue.
Moreover, a linq variable has to be enumerated to be used.
You could also create some variables and check their values while debugging. For example:
C#
private void button1_Click(object sender, EventArgs e)
{
   string tbText = textBox1.Text;
   string cbValue = (string)comboBox1.SelectedValue;
   if (!string.IsNullOrEmpty(tbText) && !string.IsNullOrEmpty(cbtext)) {
      var res = from result in ds.Results
                where result.REGISTRATION_NUMBER == cbText && result.EXAM_YR == cbValue
                select result;
      List<Result> results = res.ToList();
      if (results != null) {
         resultBindingSource1.DataSource = results;
      }
      else {
         MessageBox.Show("no data found");
      }
   }
   else {
      MessageBox.Show("Enter details");
   }
}
 
Share this answer
 
v3
The problem is not in LINQ code, as that is not even getting evaluated here, as indicated by the error message "Enter Details".
It seems that either of the textbox or the combo is not have the value being set.

Place required field validators to detect at client side if valid data is present before posting back.
Once this is done, you can work on the linq query to check proper filtering. Also see if some other postback event is resetting the controls.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900