?
Solved

c#: Weather to use Command object or not in asp.net

Posted on 2004-11-22
4
Medium Priority
?
428 Views
Last Modified: 2012-06-21
Hi There,

I have been populating (data binding) a control to a dataset in my asp.net (C#) application.

I have now noticed, that I can use the Command object or not (see below), I wonder if anybody can advise if i should use the command object or not

My code below uses the MySQL OLE DB connector, but its the same as OLE: My Code below, automatically populates a Listbox but doesn't use the COMMAND:

                        // Open connection to database
                        MySqlConnection myConnection = new MySqlConnection(myConnString);
                        myConnection.Open();

                        // Create new DataSet for storing data to bind
                        DataSet lookupDataSet = new DataSet();

                        // Fill Area ListBox
                        
                        // Create new DataAdapter
                        MySqlDataAdapter areasDataAdapter = new MySqlDataAdapter("SELECT * FROM tccAvailableAreas ORDER BY Position ASC", myConnection);

                        // Fill the DataSet from the DataAdapter
                        areasDataAdapter.Fill (lookupDataSet,"Areas");

                        // Set the DataSource, DataTexField, DataValueField of areaListBox for binding
                        areaListBox.DataSource = lookupDataSet.Tables["Areas"];
                        areaListBox.DataTextField = "Area";
                        areaListBox.DataValueField = "ID";
0
Comment
Question by:ianinspain
  • 2
  • 2
4 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 12644242
Hi ianinspain,

with simply sql statements you don't need the command object and set it to MySqlDataAdapter.SelectCommand
BUT if you want to set parameters it is needed

Regards!
B..M
mmarinov
0
 

Author Comment

by:ianinspain
ID: 12647548
Hi there.

Not too sure if i understand this?...

What is that MySqlDataAdapter.SelectCommand used for?

I was doing some reading, and i read that if you don't open a command object then one is opened auitmatically in the background and closed automatically in the background (actually this is how it worked with VB and ADO) --- but I am interested in your comment re MySqlDataAdapter.SelectCommand

0
 
LVL 28

Accepted Solution

by:
mmarinov earned 2000 total points
ID: 12648354
Here is your code with command object

// Open connection to database
                    MySqlConnection myConnection = new MySqlConnection(myConnString);
                    MySqlCommand myCommand = new MySqlCommand ("SELECT * FROM tccAvailableAreas ORDER BY Position ASC", myConnection);

                    myConnection.Open();

                    // Create new DataSet for storing data to bind
                    DataSet lookupDataSet = new DataSet();

                    // Fill Area ListBox
                   
                    // Create new DataAdapter
                    MySqlDataAdapter areasDataAdapter = new MySqlDataAdapter();
                    areasDataAdapter.SelectCommand = myCommand;
                    // Fill the DataSet from the DataAdapter
                    areasDataAdapter.Fill (lookupDataSet,"Areas");

                    // Set the DataSource, DataTexField, DataValueField of areaListBox for binding
                    areaListBox.DataSource = lookupDataSet.Tables["Areas"];
                    areaListBox.DataTextField = "Area";
                    areaListBox.DataValueField = "ID";

in this code you can add parameters to areasDataAdapter.SelectCommand
also there are UpdateCommand, DeleteCommand InsertCommand of the adapter control so when and Update method is called the needed actions can be performed

B..M
mmarinov
0
 

Author Comment

by:ianinspain
ID: 12648844
Superb! clear now, thanks... going to have a little read about these, seemed to have overlooked some things :-)

Thanks for your prompt reply

Awarding points! Thanks again!

Ian
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question