views:

178

answers:

4

I need to change what rows are grabbed from the database based upon a few flags. How can I do this? My current code is like this:

this.tblTenantTableAdapter.Fill(this.rentalEaseDataSet.tblTenant);

Say if I wanted only rows that the id was greater than 50, how would I do that?

Edit:

The code to access the database was autogenerated by the original programmer a long time ago though VisualStudio. I don't know exactly how to get a connection from the autogenerated code. If I could do that I know how to use the SqlDataAdapter

+3  A: 

Why wouldn't you use a WHERE clause in your SQL query?

Also, I would hope you don't use your ID field for anything like this. If you just want the first 50 selections, you may want to use a TOP 50 clause or something similar.

For some info on how to do this with your TableAdapter: http://www.shiningstar.net/ASPNet_Articles/DataSet/DataSetProject7.aspx

Geoffrey Chetwood
I'm not actually using an SQL query and that's a problem...I'm working on legacy code and I have to work with what VS2008 has given me and what the original programmer has used. I.E. TableAdapter instead of a database connection. I'm still learning C# too :(
Malfist
@malfist: You have control of the code and the DB right? What is the issue?
Geoffrey Chetwood
I don't have control of the DB, the code to access the DB was autogenerated by Visual Studio a long time ago.
Malfist
@Malfist: A tableadapter uses a SQL Query. Just change the query.
Geoffrey Chetwood
@Malfist: But you still don't explain why on earth you would be trying to do ID < 50.
Geoffrey Chetwood
I'm not, it's just an example. I'm actually testing if TenantStatusID equals a specific value (Past, Current, Future)
Malfist
http://www.shiningstar.net/ASPNet_Articles/DataSet/DataSetProject7.aspx
Geoffrey Chetwood
@Malfist: You should probably post more of this solution you are working on. It sounds very WTFey.
Geoffrey Chetwood
well, it was an access program, then my boss tried to get some offshore company to do it, after 4 months they gave up on it and now I get it!
Malfist
@Malfist: If it is an access program, why would it be a problem to start with a new connection?
Geoffrey Chetwood
Because now it's a C# program. Or attempting to be. The only thing left from the Access program is a bunch of GUI screenshots and the database (which this has to be compatible with)
Malfist
@Malfist: It doesn't make any difference what language it is written in. If it uses access, why is this difficult for you? Start with a new connection and go from there.
Geoffrey Chetwood
@Malfist: Perhaps you should post a new question with "How do I make a connection and perform a query against an Access DB in C#?"
Geoffrey Chetwood
Because it doesn't use access anymore. It _was_ and Access application. I'm rewriting the rewrite to port it from Access to C#, why I don't know. It worked fine in Access.
Malfist
@Malfist: You are making me think you don't understand that Access is a DB and C# is a language. Do you understand the difference?
Geoffrey Chetwood
Yes I know the difference. I guess I misspoke, this was developed inside the Access program using access's database and VBA as the language. The program is not using an Access database anymore. It's using an MSSQL as I said earlier.
Malfist
@Malfist: I don't see where you said MSSQL anywhere. If you are developing the application as a rewrite why would you not just rewrite this to make a connection to the DB and use a SQLCommand to execute your query? (Preferably a sproc)
Geoffrey Chetwood
Oh, my bad. I could have swore I said I was using MSSQL. Odd. Because I don't fully understand it. I mean I could. But that may involve rewriting a lot of what's already done (but I may end up doing it that way anyway). If at all possible I'd like to learn how to work _with_ the IDE
Malfist
@Malfist: I recommend sitting down and actually reading about using DBs in .NET before proceeding any further.
Geoffrey Chetwood
I've done a lot of stuff with Databases in PHP, but am still learning C#. I will do so, do you have any good suggestions? Links or books?
Malfist
@Malfist: I am sure you can find plenty on this site and on google. But you are showing a misunderstanding of databases in my opinion. Perhaps sitting down and reading a book on RDBMS' and SQL would be a place to start?
Geoffrey Chetwood
I understand databases, I've used them quite a bit before. I'm just not familiar with the API in C#.
Malfist
@Malfist: I don't think you are. Otherwise you wouldn't be trying to do so much in C#, and you would be writing proper queries.
Geoffrey Chetwood
I would love to be writing proper queries! Queries are easier to understand. I just don't know how to get to where I can send them to the database.
Malfist
@Malfist: Again, read up on it. Then if you have trouble, post a question.
Geoffrey Chetwood
A: 

I would imagine that the simplest way is to change whatever SQL query is running behind the scenes using a WHERE clause.

TheTXI
+1  A: 

On the server side you can use plaid old SQL:

SELECT * FROM TBLTENANT WHERE id > 50

On the client side:

rentalEaseDataSet.tblTenant.DefaultView.RowFilter = "id > 50";
Igor Zelaya
The BindingSource it's attached to still shows all the rows so the BindingNavigator still goes to all of them.
Malfist
@Maflist - Use the BindingSource filter property instead.
Igor Zelaya
A: 

Your TableAdapter needs to have the CommandText specified to a SQL query with a WHERE clause.

    private void InitCommandCollection() {
        this._commandCollection = new global::System.Data.SqlClient.SqlCommand[1];
        this._commandCollection[0] = new global::System.Data.SqlClient.SqlCommand();
        this._commandCollection[0].Connection = this.Connection;
        this._commandCollection[0].CommandText = @"SELECT * FROM MyTable WHERE ID >50;";
        this._commandCollection[0].CommandType = global::System.Data.CommandType.Text;
    }

NOTE: The above query is just an example, not meant as a solution query. The format of the above code is used to edit the generated Table Adapter designer class's InitCommandCollection() method so you can specify your own SQL. It is possible your class already has some SQL in here, in which case you could just alter it.

Jay S
You are not addressing the problem with using the ID field like this...
Geoffrey Chetwood
The presence of the 'ID' in the WHERE clause is just an example, the code snippet is meant to highlight how to edit a TableAdapter class to add SQL restrictions to the generated designer file.
Jay S
@Jay S: He asked how to do it, you should be careful advising him how to do it without warning him.
Geoffrey Chetwood
You're right, I'll edit my answer to be more clear.
Jay S