Click here to Skip to main content
15,889,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am reading data from a csv file using Oledb in C#
I also want to retrieve row number with the records of the file

I know this can be done in Ole db using the command
SQL
SELECT rank = ( SELECT COUNT(*)  FROM tableName b WHERE  a.ID > b.ID ), * FROM tableName a ORDER BY a.ID

this can be done in SQl Server using:
SQL
SELECT ROW_NUMBER() OVER (ORDER BY ID) AS Row  FROM TableName

But I want to read data from a csv file and there I want to add a column as RowNumber using any of the above query

But it is throwing me following exception
"No Value given for one or more parameters"

My whole code is as follows:
C#
void ReadData(string FilePath)
{
  string connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Text;", System.IO.Path.GetDirectoryName(FilePath));                   

  string CommandString = "SELECT rank = ( SELECT COUNT(*)  FROM {0} b WHERE  a.ID > b.ID ), * FROM {0}  a ORDER BY a.ID";

  CommandString = string.Format(CommandString ,Path.GetFileNameName(FilePath) );

  OleDbDataAdapter dataAdapter = new OleDbDataAdapter(CommandString , connString);
  DataTable dataSet = new DataTable();
  dataAdapter.Fill(dataSet); // this is giving "No Value given for one or more parameters" exception
  dgv.DataSource = dataSet;
}


Can any one provide any help, what I should do to Add one more column as RowNumber while reading data from a csv file
Posted
Updated 4-Oct-11 2:52am
v3
Comments
Bala Selvanayagam 4-Oct-11 10:12am    
Meha23, do you need to go by oledb or are you open for any other methods ?
meha23 4-Oct-11 11:02am    
Yes I can use any method
The only thing I want is to be able to add Row_Number column in my data table, while reading csv file

1 solution

Command string needs rank column to be aliased in a different way:
SQL
SELECT ( SELECT COUNT(*) FROM {0} b WHERE  a.ID > b.ID ) AS rank, * FROM {0}  a ORDER BY a.ID
 
Share this answer
 
v2
Comments
meha23 5-Oct-11 4:20am    
Thanks for the solution.
But I want to know one thing, when I run
"SELECT rank = ( SELECT COUNT(*) FROM {0} b WHERE a.ID > b.ID ), * FROM {0} a ORDER BY a.ID"
in SQl Server It worked fine,
then why do the error comes with ole db??
JOAT-MON 5-Oct-11 5:35am    
All server engines have their own proprietary syntax and functions. The OleDb provider that you used is the Microsoft JET Engine, which is different from the SQL Server engine, and therefore has its own way of parsing the query and compiling it. As you access different databases, realize that there will be slight differences in the syntax of the T-SQL that you use in your queries.
meha23 5-Oct-11 11:25am    
ok, thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900