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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:54:25+00:00 2026-05-29T03:54:25+00:00

I am trying to read an Excel file with C# and I keep getting

  • 0

I am trying to read an Excel file with C# and I keep getting this error: oledbexception cannot update. database or object is read-only on the line. Any ideas?

        string connStr = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        //file upload path

        string path = FileUpload1.PostedFile.FileName;
        //Create connection string to Excel work book
        string excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileUpload1.PostedFile.FileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
        //Create Connection to Excel work book
        OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
        //Create OleDbCommand to fetch data from Excel
        OleDbCommand cmd = new OleDbCommand("Select [Coupon], [First Name], [Last Name] from [Sheet1$]",excelConnection);
        excelConnection.Open();
        OleDbDataReader dReader;
        dReader = cmd.ExecuteReader();
        SqlBulkCopy sqlBulk = new SqlBulkCopy(conn);
        //Give your Destination table name
        sqlBulk.DestinationTableName = "CPC_Coupons";
        sqlBulk.WriteToServer(dReader);
        excelConnection.Close();

Thanks!

  • 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-29T03:54:26+00:00Added an answer on May 29, 2026 at 3:54 am

    Please see the following code if that might help in your scenario:

      public static class DatabaseManager
        {
            //set connection string for SQL Server Express Edition
            static string connString = @"Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=DocumentStore;Integrated Security=True;Pooling=False";
    
    
            //method to save document to database
            public static bool SaveDocumentToDatabase(MemoryStream msDocument, DocumentType docType, string documentName)
            {
                //keep track of the save status 
                bool isSaved = false;
    
                //create database connection
                SqlConnection sqlConnection = new SqlConnection(connString);
                try
                {
                    sqlConnection.Open();
                }
                catch (Exception ex)
                {
                    Settings.LogException(ex);
                    Console.WriteLine("Unable to open database connection");
                    isSaved = false;
                }
    
                string commandText = "INSERT INTO Documents (DocumentType, DocumentName, DocumentContent, CreateDate) VALUES('" + docType + "','" + documentName + "', @DocumentContent ,'" + DateTime.Now + "')";
                SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection);
                sqlCommand.Parameters.AddWithValue("DocumentContent", msDocument.ToArray());
                try
                {
                    sqlCommand.ExecuteNonQuery();
                    Console.WriteLine("Document saved successfully");
                    isSaved = true;
                }
                catch(Exception ex)
                {
                    Console.WriteLine("Unable to save the document to database");
                    Settings.LogException(ex);
                    isSaved = false;
                }
    
                //close database connect
                sqlConnection.Close();
    
    
                return isSaved;
            }
    
        public static bool LoadDocumentFromDataBase(DocumentType docType)
        {
             //keep track of the retrieve status 
            bool isRetrieved = false;
    
    
            //create database connection
            SqlConnection sqlConnection = new SqlConnection(connString);
            try
            {
                sqlConnection.Open();
            }
            catch(Exception ex)
            {
                Console.WriteLine("Unable to open database connection");
                Settings.LogException(ex);
                isRetrieved = false;
            }
    
    
            string commandText = "SELECT * FROM Documents WHERE DocumentType ='" + docType + "'";
    
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(commandText, sqlConnection);
            SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection);
    
            DataTable dtDocuments = new DataTable();
    
            try
            {
                sqlDataAdapter.Fill(dtDocuments);
                Console.WriteLine("Document retrieved successfully");
                isRetrieved = true;
            }
            catch(Exception ex)
            {
                Console.WriteLine("Unable to retrieve documents from database");
                Settings.LogException(ex);
                isRetrieved = false;
            }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a stored procedure that will read an Excel file into
I'm trying to read a .doc file into a database so that I can
i am trying to read an excel file using java. while compiling the program
I'm trying to read an Excel file (.xls, I think Excel 2003 compatible) via
I'm trying to read an excel file using an OleDb Reader, I couldn't debug
I am trying to read an Excel spreadsheet file with Ruby, but it is
I am trying to read in an Excel file using xlrd, and I am
I am trying to read data from excel file in asp.net. I have added
I'm trying to generate an excel file with some validations, I've read the poi
I'm trying to read excel file which is uploaded on itemadded event. I need

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.