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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:32:57+00:00 2026-05-16T20:32:57+00:00

I am trying to read one excel sheet and then creating another excel sheet

  • 0

I am trying to read one excel sheet and then creating another excel sheet through OleDb

In this another excel sheet, I made some colums like Name char(10), Age Number

now when I reading that excel sheet and trying to insert the values in this another excel sheet then values are inserting but also contains one extra char i.e single quote(‘) as the first char.

Why all the values are instered with single quote pre to actual value? How to get rid of it? I am using c#.net for this.

@Edit

    OleDbCommand cmd = new OleDbCommand();
                   cmd.CommandType = System.Data.CommandType.Text;
                   cmd.Connection = con;
                   cmd.CommandText = "CREATE TABLE Data1 (Name char(10), Age char(10)) ";

                   cmd.ExecuteNonQuery();

foreach (DataRow row in someDataTable.Rows)
 {

       cmd.CommandText = "INSERT INTO Data1 values ('" + row["Name"] +
                       "','" + row["Age"] +  "')";

       cmd.ExecuteNonQuery();
}

I have exactlly the same data in someDataTable as in excel which I need to copy.

  • 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-16T20:32:58+00:00Added an answer on May 16, 2026 at 8:32 pm

    It would be strange, but maybe it is some sort of attempt of OleDb to avoid SQL Injection? (What if row["Name"] contains a single quote?) Even if it does not explain, like @Treb said, why there is a leading quote only, without a trailing one.

    What happens if you try to use the query with parameters, ie.:

    using (OleDbCommand addLine = new OleDbCommand("INSERT INTO Data1 values (@name, @age)"))
    {
        addLine.Parameters.AddWithValue("@name", row["Name"]);
        addLine.Parameters.AddWithValue("@age", row["Age"]);
        int affectedRows = addLine.ExecuteNonQuery();
        Debug.Assert(1 == affectedRows);
    }
    

    After a few attempts, I still can’t reproduce the observed behavior. I suggest to do several things:

    • Post in your question the whole source code which produces the single quote problem,

    • Try the following source (Console application) and see what happens. In my case, values are inserted well, without any leading single quotes. The only problem are the ages, inserted as text instead of numbers (which can probably be solved by explicitly specifying the type of the row):

    Source code:

    namespace Demos.StackOverflow.ExcelUpdate
    {
        using System;
        using System.Collections.ObjectModel;
        using System.Data.OleDb;
    
        class Program
        {
            static void Main(string[] args)
            {
                // -> Change the following name of the file to use.
                string fullPath = @"C:\Users\Arsene\Desktop\Book1.xlsx";
                string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0'", fullPath);
    
                Collection<Tuple<string, int>> dataToPut = new Collection<Tuple<string, int>>();
    
                dataToPut.Add(new Tuple<string, int>("Some name", 34));
                dataToPut.Add(new Tuple<string, int>("Other name here", 41));
                dataToPut.Add(new Tuple<string, int>("Last one", 36));
    
                using (OleDbConnection oleConnection = new OleDbConnection(connectionString))
                {
                    oleConnection.Open();
    
                    using (OleDbCommand createTable = new OleDbCommand("create table Data1 (Name char(10), Age char(10))", oleConnection))
                    {
                        createTable.ExecuteNonQuery();
                    }
    
                    foreach (var t in dataToPut)
                    {
                        using (OleDbCommand addItem = new OleDbCommand("insert into Data1 values (@name, @age)", oleConnection))
                        {
                            addItem.Parameters.AddWithValue("@name", t.Item1);
                            addItem.Parameters.AddWithValue("@age", t.Item2);
                            addItem.ExecuteNonQuery();
                        }
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.