34
Reply

How do I: Pass a DataTable from a Class to the Main program?

Gustavo

Gustavo

14 years ago
2.2k

Hello:
 
I have a class that get me a record and I want it to be passed back to the main program. I can not get it to work. Below is the part of the Class and the main program:
 

Class:

        public DataTable ReadRecord(string DBCommand)

        {

            Connection();

            SetupDataAdapter();

            DBDataAdapter = new SqlDataAdapter(DBCommand, DBConnect);

            try

            {

                DataTable DBDataTable = new DataTable();

                DBDataAdapter.Fill(DBDataTable);

                foreach (DataRow DBDataRow in DBDataTable.Rows)   // GOOD - IT WORKS

                {

                    TheID = DBDataRow["Name"].ToString();

                    MessageBox.Show("TheID=" + TheID);

                }

                Terminate();

                return (DBDataTable);

            }

            catch

            {

                return null;

            }

            finally

            {

            }

        }

 

Main:

        private void buttonDBSelectRecord_Click(object sender, EventArgs e)

        {

            DBCommand =

                " SELECT"

                + " [Name]"

                + " ,[Address_1]"

                + " FROM"

                + " [Company]"

                + " WHERE"

                + " [Company_ID] = " + "'000000'"

                ;

            ClassDB DBConnection = new ClassDB();

            ClassDB.Connection();

            DBConnection.ReadRecord(DBCommand);

            foreach (DataRow DBDataRow in DBDataTable.Rows)   // BAD

            {

                TheID = DBDataRow["Name"].ToString();

                MessageBox.Show("Login...TheID=" + TheID);

            }

        }
 


Answers (34)