Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Work with and view the results of a query?

Hello!

In Visual Studio .NET, I can open the Server Explorer and drag & drop a table from there. A connection and a data adapter will be created.

Then, I can easily generate a DataSet which will load data automatically when I call the Fill method.

This is all fine and dandy. The DataSet will contain a DataTable which represents a table in the database.


The problem is - I would like to generate a DataTable (or some other database object) to represent the results of a query I write.

How do I do this?

More importantly, how do I do this using the IDE as much as possible, in order not to waste time?


In Delphi, I just put a TQuery (or TAdoQuery, if I work with ADO) on a form. I set the Connection accordingly, I set the SQL property to the SQL query I want executed, and so in less than 30 seconds I have a component which represents the result of a query, and which I can easily use to view the data in a Grid.

How do I do the same thing in C# / VS .NET?


I tried to make a new data adapter with my custom SQL SELECT instruction. The problem is - for some strange reason, altough the query is valid and executes ok, the data doesn't appear in the DataGrid.

Damn! :-)


So, how do I make an object (DataTable or something) which represents the results of a query, and which can be used to view data in the DataGrid?

In Delphi, this is an under 30 seconds task. :-(

John K.
Sunday, June 29, 2003

With VS .NET 2003:

1) Create a new C# Windows Application.
2) Drag a DataGrid to the form. I named mine dgCust.
3) Switch to the Data tab in the toolbox.
4) Drag a SqlDataAdapter control to the form. This will launch the Data Adapter Configuration Wizard.
5) Click Next to get past the intro panel of the Wizard.
6) Select or create a data connection to use and click Next. I used a connection pointing to the SQL Server Northwind database.
7) Select "Use SQL Statements" and click Next.
8) Type in your SELECT statement (I used SELECT * FROM Customers WHERE Country = 'Brazil') and click Next.
9) Click Finish to create the sqlConnection1 and sqlDataAdapter1 objects in the tray.
10) Select the sqlDataAdapter1 object. Click the Generate DataSet link at the bottom of the Properties window.
11) Create a new DataSet named DataSet1 containing the Customers table. (Note that DataSet1 is the name of the class you're creating; there will also be an instance of the class named dataSet11). Click OK.
12) Select the DataGrid control. Set its DataSource property to dataSet11 and its DataMember to Customers.
13) Double-click the form to create an event handler for the Load event and enter a single line of code:
sqlDataAdapter1.Fill(dataSet11, "Customers");
14) Run the application, and the data will load into the form.

More complex than your Delphi example, for sure, and there's no way to avoid writing the line of code to fill the DataSet (because .NET wants to give you control over when to take the performance hit of retrieving data). And there are alternative ways to do all of these things, but you said you wanted to use the IDE as much as possible.

Mike Gunderloy
Sunday, June 29, 2003

Hello, Mike.

Thank you very much for your detailed reply.

Unfortunately, after following your steps exactly, with the same database, absolutely nothing happens - the DataGrid remains empty.

And no, I haven't forgot the Fill method call.

:-(

I use Visual Studio .NET 1.0.

Sheesh, man. Even Delphi 1.0 which was released 8 years ago supported this feature, in a more intuitive way.

:-( :-( :-(

I shall keep working hard on getting this little stupid thing done, and will get back to the forum with answers.

:-(

John K.
Sunday, June 29, 2003

Thank you very much! You are a life saver! :-)

I really don't know why I didn't get it right the first time I tried this.

I was very tired today, and perhaps this is why.

Again, thank you very much and I apologize for the simple question.

Thank you!

John K.
Sunday, June 29, 2003

I just ran through those steps in a 1.0 application and they worked fine there as well.

You're not building a Web application, are you?

Mike Gunderloy
Sunday, June 29, 2003

OK, looks like we were typing at the same time. Glad it worked.

Mike Gunderloy
Sunday, June 29, 2003

Thank you very much, Mike!

It works now. Again, I don't know why it didn't work the first time when I tried it this morning - I must have been very tired and missed some step by accident. :-(

John K.
Sunday, June 29, 2003

*  Recent Topics

*  Fog Creek Home