RubenPieters

Hello,

I've made a topic about this already but did not answer my last question, and my questions are a bit different now

so I got this atm for filtering data: (comes from http://msdn2.microsoft.com/en-us/library/aa325668(VS.71).aspx)

private void GetRowsByFilter(){
DataTable myTable;
myTable = DataSet1.Tables["Orders"];
// Presuming the DataTable has a column named Date.
string strExpr;
strExpr = "Date > '1/1/00'";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
foundRows = myTable.Select(strExpr);
// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++){
Console.WriteLine(foundRowsIdea[0]);
}
}

well I now the questions

1) What will this filter result into (what will it display after it found results)

2) Is it possible to put the number of results it found into a integer

3) How do I make it so it fits in my form (what things must be altered so it searches in the correct database/table etc), I tried doing a bit things with it but I am whole time having errors, and I'm not even sure if its right what I'm doing

4) Does this filter in the original database or does it take in account with changes in the database

Thx in advance,

Ruben



Re: Visual C# Express Edition Filtering Database Questions

jrboddie

Assuming the code is correct (I did not verify), I offer the following answers:

1. All rows in the Orders Table of the DataSet1 in which the Date value is older than 1/1/00.

2. int i = foundRows.Length;

3.

4. The filter is on the data in the DataSet which is an in-memory subset of the DataBase. So the answer depends on whether you have altered the DataSet data prior to this operation.





Re: Visual C# Express Edition Filtering Database Questions

RubenPieters

jrboddie wrote:

Assuming the code is correct (I did not verify), I offer the following answers:

3.

Yes the code is correct, but it is from the help thing so that leads to my question 3 I want it to fit in my form, not with the column 'Date' but with my column, my dataset, my table, how do I change the code so it filters my dataset not the previous one

int XLoc = Convert.ToInt32(label1.Text);

DataTable Table1 = new DataTable();

Database1DataSet1 DataSet1 = new Database1DataSet1();

Table1 = DataSet1.Tables["xloc"];

// Presuming the DataTable has a column named Date.

string strExpr;

strExpr = "xloc = XLoc";

DataRow[] foundRows;

// Use the Select method to find all rows matching the filter.

foundRows = Table1.Select(strExpr);

// Print column 0 of each returned row.

for (int i = 0; i < foundRows.Length; i++)

{

listBox1.Items.Add(foundRows[i ][0]); - theres a space after 'i' so that lamp thing doesnt come up

}

this is how I tried to change it but its not really correct, so how do I change the original thing correct

edit: my table is Table1 my dataset is Database1DataSet1 my column is xloc and I want it to add all rows from xloc to the listbox that are equal to XLoc

and rereading my question 3 again, it's indeed a bit unclear

and thanks for your other answers





Re: Visual C# Express Edition Filtering Database Questions

jrboddie

Here is how I got something to work that is close to your specification:

1. I used Visual Studio to configure a data source based on the table in the database.

2. Using the Data Sources window, I drug the Table1 onto the form. This created a DataGridView of the data, but more importantly, it automatically wrote the code for filling the dataset with the data from the database. (You can always delete the DataGridView.) Here is the code that was created in the form load event handler:

Code Snippet

private void Form1_Load(object sender, EventArgs e)

{

this.table1TableAdapter.Fill(this.dataSet11.Table1);

}

(Note that because I already had a definition of 'dataSet1' in my code, it created and filled a dataSet called 'dataSet11'.) (Maybe you've already filled your dataSet.)

3. So here is the code that filters the data in the dataSet and writes the result to the listBox:

Code Snippet

private void button1_Click(object sender, EventArgs e)

{

int XLoc = Convert.ToInt32(label1.Text);

DataTable Table1 = new DataTable();

Table1 = dataSet11.Tables["Table1"];

string strExpr;

strExpr = "xloc = "+XLoc.ToString();

DataRow[] foundRows;

// Use the Select method to find all rows matching the filter.

foundRows = Table1.Select(strExpr);

// Print column 0 of each returned row.

for (int i = 0; i < foundRows.Length; i++)

{

listBox1.Items.Add(foundRows[i][0]);

}

}

Note that you need to reference "Table1" in the Table1 variable assignment. Also the strExpr assignment needs to convert the int XLoc back to a string.





Re: Visual C# Express Edition Filtering Database Questions

RubenPieters

hmm now it doesn't give any errors, but I don't get any filter results either when the text from label1 is the same as one of my rows

http://img19.imageshack.us/my.php image=project4rs5.jpg

as you can see in the pic I have 2 rows with xloc being 84 but when label1.text changes to 84 it doesnt print anything in the listbox

so, how can I fix this





Re: Visual C# Express Edition Filtering Database Questions

jrboddie

Sorry. There are too many ways for your code not to give results for me to guess.

I suggest that you use some breakpoints in your program to see where you are getting unexpected results.





Re: Visual C# Express Edition Filtering Database Questions

RubenPieters

Is there maybe any way to check if its filtering the right column/table/dataset Because that seems the only thing that could be wrong.





Re: Visual C# Express Edition Filtering Database Questions

jrboddie

Yes. Click on the line:

string strExpr;


then F9. This should set a breakpoint at that statement. You will see the line highlighted and a filled circle to the left.

Next, run the program in debug mode: (F5) and operate your program so this routine will be executed. The program should stop and display your code with the breakpoint line now highlighted in yellow.

While it is in this state, hover your mouse over the 'Table1' to the left of '=' in the line above.

You should see a magnifying glass icon to the left of '{Table1}'. Click the magnifying glass and you should see your data table.

You can hover over any declared variable in scope and see what its value is at that point in time.

F11 will cause the program to execute one more line and you can see what has changed.

To return to normal operation, stop the program (Shift-F5) and click on the filled circle to remove the breakpoint.









Re: Visual C# Express Edition Filtering Database Questions

RubenPieters

I see the problem now, my table doesn't have any data in it after I pressed the magnifying glass, you maybe know what can cause that





Re: Visual C# Express Edition Filtering Database Questions

jrboddie

Sounds like the Table1 in the routine is not referencing a dataset that was filled by your program. See if the name of the dataset in the form load event matches the name of the dataset in the routine.

(I am assuming that you put a databound dataGridView on your form which would have generated this code for you.)





Re: Visual C# Express Edition Filtering Database Questions

RubenPieters

ok thanks, but the result only shows the ID column, is it possible to show the xloc column itself





Re: Visual C# Express Edition Filtering Database Questions

jrboddie

Inside the loop it should be

foundRows[ i ]["xloc"]





Re: Visual C# Express Edition Filtering Database Questions

RubenPieters

Yes, ty that works Smile

but I got another question Stick out tongue is there any way to make a string expression look at each row in the table and see if there any row is equal to another row

I wil try to explain a bit, say you have a table with rows wich contain 1, 2, 4, 6 and 6

So the filter then searches for equals in the rows and find the two 6'es, but if it would contain 1, 2, 3, 4 and 5 it wouldnt 'find' anything

I hope you understand my question