Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 733907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:18:39+00:00 2026-05-14T07:18:39+00:00

I have an ASP.NET (3.5) page that allows a user to upload an Excel

  • 0

I have an ASP.NET (3.5) page that allows a user to upload an Excel 2003 file with multiple sheets and that data is inserted into staging tables in the database. The mapping of database tables/columns to Excel sheets/columns is specified in an XML file. I use LINQ to SQL to send the data to the database.

ImportTablesDataContext db = new ImportTablesDataContext();
tblImportLoan loan; // class defined by LINQ to SQL

// iterate through each row of data from the Excel worksheet called Loans
foreach (DataRow dr in _data.Tables[ExcelSheetName].Rows)
{
  loan = new tblImportLoan();

  // iterate through each column in the mapping for the Loans worksheet
  foreach (... column in mapping for this worksheet ...)
    loan.GetType().GetProperty(column.DBName).SetValue(loan, GetValue(column, dr), null);

  db.tblImportLoans.InsertOnSubmit(loan);
}

I repeat most of this code for each of the 5 worksheets. I would like to iterate through the set of 5 tables defined in the mapping – but I cannot figure out how to make the hard-coded class names / methods / properties dynamic (lines 2, 7, and 13 above). This would allow me to avoid referencing the table names outside of the XML file.

Any suggestions?

EDIT:
Here is what I’m looking for… but I don’t know how to do lines 5 and 8.

foreach (... table in mapping ...)
{
  foreach (DataRow dr in _data.Tables[table.ExcelSheetName].Rows)
  {
    obj = new <LINQ constructor derived from table name>;
    foreach (... column in mapping ...)
      obj.GetType().GetProperty(column.DBName).SetValue(obj, GetValue(column, dr), null);
    db.<LINQ property derived from table name>.InsertOnSubmit(obj);
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:18:39+00:00Added an answer on May 14, 2026 at 7:18 am

    I think you could do it by retrieving the Table<TEntity> objects directly from the DataContext. First, you need to wrap your code in a generic method:

    public void DoWorkFor<TEntity>()
    {
        ImportTablesDataContext db = new ImportTablesDataContext();
        Table<TEntity> table = db.GetTable<TEntity>();
    
        // iterate through each row of data from the Excel worksheet called Loans
        foreach (DataRow dr in _data.Tables[ExcelSheetName].Rows)
        { 
            entity = new TEntity();
    
            // iterate through each column in the mapping for the Loans worksheet
            foreach (... column in mapping for this worksheet ...)
            {
                entity.GetType().GetProperty(column.DBName)
                    .SetValue(entity, GetValue(column, dr), null);
            }
    
            table.InsertOnSubmit(entity);
        }
    }
    

    Then, somewhere else in your program, you need to call this method and supply the appropriate entity type:

    DoWorkFor<tblImportLoan>();
    DoWorkFor<tblOtherType1>();
    DoWorkFor<tblOtherType2>();
    DoWorkFor<tblOtherType3>();
    DoWorkFor<tblOtherType4>();
    

    Is this close to what you’re looking for? Add a comment if not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'll have an ASP.net page that creates some Excel Sheets and sends them to
I'm trying to create an ASP.NET/C# page that allows the user to edit a
We have a non-SSL ASP.NET web app that allows a user to login (ASP
We have created a web application, using ASP.NET, that allows users to upload documents
I have an asp.net website that allows the user to download largish files -
I'm working on an asp.net application that allows the user to update data for
I have an ASP.Net page that will be hosted on a couple different servers,
I have an ASP.NET page that uses a repeater nested within another repeater to
I have an asp.net page that calls a dll that will start a long
I have an ASP.NET page that uses the ASP.NET Ajax Control Toolkit TabContainer .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.