Add List<newFiles> to a dt, save to table_1

In this code, I need help with two items:

Q1: How to code this:        // define a datatable (dt) and add newFiles List to it , see the code below.

Q2: Given cs, cn, table_1, how to do ...       // add dt to SQL Server table_1

   protected void Button1_Click(object sender, EventArgs e)
        List<ExcelFiles> newFiles = new List<ExcelFiles>();
        ExcelFiles xlFile = new ExcelFiles();

        // Put file names in the directory into array.
        string dirPath ="C:\\Users\\Mike\\Data\\"; 
        var files = Directory
                        .EnumerateFiles(dirPath, "*", SearchOption.TopDirectoryOnly)

        // store file names in xlFile object
        foreach (string name in files)
            string file =name;
            string fileExtention = name.Substring(name.Length - 5);
            if (fileExtention == ".xlsx")
                xlFile.filename = file;

                // add file name to xlFile object 
        // define a datatable (dt) and add newFiles List to it (I will need this method elsewhere as well)

       // add dt to SQL Server table_1

Open in new window

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

If table_1 is the name of the table, cs the connection string, and cn the column name:

DataTable dt = new DataTable
    Columns = { { cn, typeof(string) } }

foreach(string file in newFiles)

Open in new window

SqlConnection connection = new SqlConnection(cs);
SqlDataAdapter adapter = new SqlDataAdapter("select * from table_1", connection);
new SqlCommandBuilder(adapter);

Open in new window


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
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you.
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

From novice to tech pro — start learning today.