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

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";
ianinspainAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mmarinovCommented:
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
ianinspainAuthor Commented:
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
mmarinovCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ianinspainAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.