count the no of rows in csv file thats a data source

Ask general questions here.
rsudhak
Posts: 118
Joined: Fri Jan 04, 2019 1:38 pm

count the no of rows in csv file thats a data source

Post by rsudhak » Mon Mar 11, 2019 2:34 pm

Hi all,
I need to find the no of rows in the data source that's provided in the code, what's the best way to do it?

Thanks,
Rajee

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: count the no of rows in csv file thats a data source

Post by odklizec » Mon Mar 11, 2019 2:46 pm

Hi,

You can use something like this:
	//create CSV data connector
	string refConnector = "CSVConnector";

	//get data from ref. CSV
	Ranorex.Core.Data.CsvDataConnector refCSVConnector = new Ranorex.Core.Data.CsvDataConnector(refConnector,@refFile,true);
	refCSVConnector.SeparatorChar = ',';
	Ranorex.Core.Data.ColumnCollection refCSVColumns = new Ranorex.Core.Data.ColumnCollection();
	Ranorex.Core.Data.RowCollection refCSVRows = new Ranorex.Core.Data.RowCollection(refCSVColumns);
	
	//load CSV connector
	refCSVConnector.LoadData(out refCSVColumns, out refCSVRows);

	//get number of rows
	int refCSVRowsCount = refCSVRows.Count;
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

rsudhak
Posts: 118
Joined: Fri Jan 04, 2019 1:38 pm

Re: count the no of rows in csv file thats a data source

Post by rsudhak » Thu Mar 21, 2019 4:57 pm

what is @refFile here? is it the header name? in the data source?

rsudhak
Posts: 118
Joined: Fri Jan 04, 2019 1:38 pm

Re: count the no of rows in csv file thats a data source

Post by rsudhak » Thu Mar 21, 2019 5:00 pm

public CsvDataConnector(string name, string fileName, bool withHeaders)
{
this..ctor(name, fileName, withHeaders, CheckState.Indeterminate);
}



for tghe file name do I have to give the whole path?

User avatar
woha
Posts: 13
Joined: Wed Mar 06, 2019 12:56 pm

Re: count the no of rows in csv file thats a data source

Post by woha » Mon Aug 31, 2020 2:17 pm

Works also good for Excel files
//create excel data connector  
string refConnector = "ExcelConnector";  
//get data from ref. Excel file  
Ranorex.Core.Data.ExcelDataConnector excelConnector  = new Ranorex.Core.Data.ExcelDataConnector(refConnector,@"C:\temp\excelfile.xlsx","(insert your sheet name here)","A:A",CheckState.Unchecked);
Ranorex.Core.Data.ColumnCollection columnCollection = new Ranorex.Core.Data.ColumnCollection();  
Ranorex.Core.Data.RowCollection rowCollection = new Ranorex.Core.Data.RowCollection(columnCollection); 
excelConnector.LoadData(out columnCollection, out rowCollection);
int linecounter = rowCollection.Count;
Report.Info("Excel-Tab has "+linecounter.ToString())+" rows";
:-)