user1501034
user1501034

Reputation: 363

Invalid character exception while reading from excel sheet

I am having an excel sheet. I write a query in a particular cell in that excel sheet and then call that query through my C# code. I have declared that cell as strParam1. So, strParam1 contains whatever query I type in my excel sheet. The code is as follows:-

public void UpdateDatabase(String strParam1, int row)
        {
            System.Data.OracleClient.OracleConnection conn = new System.Data.OracleClient.OracleConnection();
            conn.ConnectionString = "Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.5.144)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = orcl)));UID=mwm;PWD=mwm";
            try
            {
                conn.Open();
                OracleCommand command = conn.CreateCommand();
                command.CommandText = strParam1.Trim();
                OracleDataReader reader = command.ExecuteReader();
                command.Dispose();
            }
            catch (Exception ex)
            {
                ExcelRecorder(ex.Message, row);
            }
            finally
            {
                conn.Close();
            }
        }

When ExecuteReader is reached, it throws an exception "Invalid character" found. In the excel sheet, I have typed Select * from \"Task\"; Since Task is an oracle keyword, I have restructured the above query. But, the query is not executing. If I type the below code and run it, everything is fine.

command.CommandText = "Select * from \"Task\""; 

What is wrong? Is there any special formatting I must do? And there is no problem in reading from the excel sheet. I tested it thoroughly. So, that has got to do nothing with my exception.

Upvotes: 0

Views: 839

Answers (1)

Steve
Steve

Reputation: 216273

When you read the string from the Excel cell and put inside a string variable complete with quotes, you don't need to add the \ to escape.
To prove it, just put a breakpoint on the UpdateDatabase method and look at the strParam1 var.
You could see that it contains the quotes, while when you assign the directly to the command.CommandText you need to escape the string literal.

Upvotes: 1

Related Questions