How to add data row in same row?

Hello everyone,
I am working in an automation where i have to extract required text form .jrn file and save data in excel worksheet.

But here i dont know how to add rows in same rows with datarow object.
The code that i am using is only adding rows of last file.

Here’s my code :

//set path of person Folder
string folderPath = @"D:\some_person";
//get files from person Folder
string[] files = Directory.GetFiles(folderPath);
//set Flag as read
int read = 0;
//CreateDataTable 
DataTable dt = new DataTable();
dt.Columns.Add("MONEY Taken", typeof(string));
dt.Columns.Add("MONEY Presented", typeof(string));
dt.Columns.Add("Action SETTINGS", typeof(string));
dt.Columns.Add("Action", typeof(string));
dt.Columns.Add("PERSON ID", typeof(string));
dt.Columns.Add("Date Time", typeof(string));


DataRow row = null;
foreach(string file in files) {
	string[] fLines = File.ReadAllLines(file);
	string stext;
	string cashTaken = "";
	string cashPresented = "";
	string atmID = "";
	string dateTime = "";

	foreach(string line in fLines){
		stext = line;
		if(! stext.Equals("")){

			
		}
	}
	dt.Rows.Add(row);
	row = dt.NewRow();
	
Console.WriteLine(dt.Rows.Count);
}

The aim here is to get output to excel file as datatable like:
MONEY TAKEN, MONEY PRESENTED, ACTION SETTINGS,ACTION,PERSON ID, DATE TIME

and i have able to extract text from file but not able to write data in row. Please help !

Try initializing dt row at the beginning of files loop. Get rid of “read” flag variable, i have a feeling it’s buggy

DataRow row = null;
foreach(string file in files) {
	string[] fLines = File.ReadAllLines(file);


	row = dt.NewRow();  // <<<< initialize here

	foreach(string line in fLines){
		
	}
	dt.Rows.Add(row);
	
	
Console.WriteLine(dt.Rows.Count);
}

Hi @prabin_chand1 ,

The reason it is adding a new row is because you’re creating a new row and adding data into it.
You might need to find index of the row where you want to put the data in: then you would have to add it something like

for eg: if you have data stored in a RecordsDT, and the rowIndex in which you wanna add the data in stored in RowIndex.
use multiple assign:
RecordsDT(RowIndex)(“Cash taken”)= cashTaken
RecordsDT(RowIndex)(“Cash presented”)= cashPresented
RecordsDT(RowIndex)(“ATM ID”)=atmID
RecordsDT(RowIndex)(“Date Time”)=dateTime

Thanks,
Aditya

Not worked as expected, it’s only adding data of last file

As per to your above mentioned code,
You are adding data in a new DataTable itself.

so it would be new rows only.

how to resolve this . ?

Can you share your input file, expected output, or XAML if you have build any.

Regards,
Aditya

nope, its confidential

Not sure of what your ask is :slight_smile:
Sorry, unable to help you out.

Hi @prabin_chand1 ,

Could you maybe check if the below modified code works for you :

//set path of person Folder
string folderPath = @"D:\some_person";
//get files from person Folder
string[] files = Directory.GetFiles(folderPath);
//set Flag as read
int read = 0;
//CreateDataTable 
DataTable dt = new DataTable();
dt.Columns.Add("MONEY Taken", typeof(string));
dt.Columns.Add("MONEY Presented", typeof(string));
dt.Columns.Add("Action SETTINGS", typeof(string));
dt.Columns.Add("Action", typeof(string));
dt.Columns.Add("PERSON ID", typeof(string));
dt.Columns.Add("Date Time", typeof(string));


DataRow row = null;
foreach(string file in files) {
	string[] fLines = File.ReadAllLines(file);
	string stext;
	string cashTaken = "";
	string cashPresented = "";
	string atmID = "";
	string dateTime = "";

	row = dt.NewRow();
	
	foreach(string line in fLines)
	{
		stext = line;
		if(! stext.Equals(""))
		{

			if ((stext.Contains(") TAKEN") || stext.Contains("MONEY TAKEN") || stext.Contains("MONEY PRESENTED")) && read == 0)
			{
				read = 1;
				cashTaken = stext.Substring(stext.IndexOf(":") - 2);
				
				row["Cash Taken"] = cashTaken;
			}
			
			if (stext.Contains("MONEY PRESENTED") && read == 1)
			{
				cashPresented = stext.Substring(stext.IndexOf(":") - 2);
				row["Cash Presented"] = cashPresented;
			}
			
			if (stext.Contains("PERSON ID") && read == 1)
			{
				atmID = stext.Substring(stext.IndexOf(":") + 1);
				row["ATM ID"] = atmID;
			}

			if (stext.Contains("DATE TIME") && read == 1)
			{
				dateTime = stext.Substring(stext.IndexOf(":") + 1);
				row["Date Time"] = dateTime;
			}
			
			if (stext.Contains("TRANSACTION END") && read == 1)
			{
				read = 0;
			}
			
		}
		dt.Rows.Add(row);
	}
	
Console.WriteLine(dt.Rows.Count);
}

error :… again but this is error

@prabin_chand1 ,

Could you make a Slight change in the provided Code by placing the highlighted row as shown in the image below :

The Highlighted Expression should be inside the foreach(string line in fLines)

This is adding new row object for each lines in file, which i dont want to be as it there will be more rows that cannot be written in Write Range Activity .

anyone ?
Who can help me out !

works just fine for me


string[] files = {"Volvo", "BMW", "Ford", "Mazda"};

DataTable dt = new DataTable();
dt.Columns.Add("MONEY Taken");

DataRow row = null;

foreach (string file in files) {
	row = dt.NewRow();  // <<<< initialize here
	row["MONEY Taken"] = file;

	dt.Rows.Add(row);
	
	Console.WriteLine(dt.Rows.Count.ToString());
}

Console.WriteLine(dt.Rows[1]["MONEY Taken"]);
	

image

I don’t know if you already fixed your code or not but you’re referencing columns that don’t exist in your datatable