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 7068079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:16:48+00:00 2026-05-28T05:16:48+00:00

I’m using MVC3 with C# code. I have a table in my SQL Server

  • 0

I’m using MVC3 with C# code. I have a table in my SQL Server Express containing some columns, and I have an Excel sheet which has the same number of columns with the same datatypes and names.

My requirement is I want to browse that Excel file in my MVC3 application, where the user can select the file. There is a RegistrationNo column in both my database table as well as in the Excel sheet. Before importing the data in the table of the database the RegNo present in the Excel sheet should be compared with the RegNo in the database table and if that RegNo already exists than no insertion for that RegNO should take place else if that RegNo is not present in the table than the row for that RegNo should be inserted.

Below is my code that I have tried but I’m getting lot of issues with that.

[HttpPost]
public ActionResult AdmissionUpload()
{
    string filePath = null;
    foreach (string inputTagName in Request.Files)
    {
       HttpPostedFileBase Infile = Request.Files[inputTagName];      
       if (Infile.ContentLength > 0 && (Path.GetExtension(Infile.FileName) == ".xls" || Path.GetExtension(Infile.FileName) == ".xlsx" || Path.GetExtension(Infile.FileName) == ".xlsm"))
       {
          filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                        Path.GetFileName(Infile.FileName));
          if (System.IO.File.Exists(filePath))
          {
             System.IO.File.Delete(filePath);
          }
          Infile.SaveAs(filePath);
          //Infile.SaveAs(filePath); 
       }

       if (filePath != null)
       {
          System.Data.OleDb.OleDbConnection oconn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath.ToString() + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
          oconn.Open();

          try
          {
             if (oconn.State == System.Data.ConnectionState.Closed)
                oconn.Open();
          }
          catch (Exception ex)
          {
             // MessageBox.Show(ex.Message);
          }

          dynamic myTableName = oconn.GetSchema("Tables").Rows[0]["TABLE_NAME"];
          OleDbCommand ocmd = new OleDbCommand("select * from [" + myTableName + "]", oconn);
          OleDbDataReader odr = ocmd.ExecuteReader();

          if (odr.HasRows)
          {
             while (odr.Read())
             {
                 if (odr[0].ToString().Trim() != "")
                 {
                    if (CheckDepartment(odr[0].ToString().Trim()) == false)
                    {
                       var model = new DepartmentMaster();
                       model.DepartmentName = odr[1].ToString().Trim();
                       db.DepartmentMasters.AddObject(model);
                       db.SaveChanges();
                       FLAG = true;
                    }
                 }
              }
          }
       }
   }
   return View();
}   

Here CheckRegNo checks whether the RegNo exists.

  • 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-28T05:16:49+00:00Added an answer on May 28, 2026 at 5:16 am
    dynamic myTableName = oconn.GetSchema("Tables").Rows[0]["TABLE_NAME"];
    OleDbCommand ocmd = new OleDbCommand("select * from [" + myTableName + "]", oconn);
    OleDbDataReader odr = ocmd.ExecuteReader();
    if (odr.HasRows)
    {
         while (odr.Read())
     {
         var model = new Student();
         model.Col1=Convert.ToInt32(odr[0]);
         model.Col2 = odr[1].ToString().Trim();
         model.col3 = odr[2].ToString().Trim();
         model.col4 = odr[3].ToString().Trim();
        db.MyTable.AddObject(model);                            
     }
    }
    

    This is how im reading the Excel and saving data from Excel.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

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.