34
Answers

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

Gustavo

Gustavo

14y
2.2k
1

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)
0
Sumit Jolly

Sumit Jolly

NA 18.7k 558.5k 10y
Help me understand, why do you want to store html data into excel with css?

If you have already data in html form you can leave it in html itself?
0
Waqas Ali

Waqas Ali

NA 6 3.5k 10y
Can you give me some example.How I convert html table data into excel with css
0
Sumit Jolly

Sumit Jolly

NA 18.7k 558.5k 10y
Use NuGet Closed.XML package to generate Excel. It's fairly easy to use, and you'll find lot of tutorials online for that.

twitter @sumitjolly