Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SetNextRow explanation
#1
Solved: 10 Years, 9 Months, 1 Week ago Rolleyes 
Hi,
Code:
DataTable("MyCol","Action1")=123456
row=DataTable.GetSheet("Action1").getCurrentRow
DataTable.SetCurrentRow(row+1)
DataTable.GetSheet("Action1").SetNextRow
DataTable("MyCol","Action1")=1255

when i run this code 1255 is getting written in 3rd row of Action1 datasheet. But when i run this code step by step (F10), 1255 is getting written in 2nd row of the Action1 datasheet.

Can anyone explain me why this is happening????
Reply
#2
Solved: 10 Years, 9 Months, 1 Week ago
I just wonder how you are getting 1255 in second row.
I have gone through the statements above and feel that it will always write 1255 in the the third row only unless you have not set the current row / commented your statements anywhere in betwenn again apart from the mentioned.

if the current row is not set at the beginning
first statement DataTable("MyCol","Action1")=123456 will always write at the first row
in second statement you will get the value as '1' for row
which you use to set the current row increasing with 1 i.e. now the current row is 2
in the next statement you are again setting the current row as next row, which means the current row is now 3
so the last statement will always write in row 3.
makes sense?

Reply
#3
Solved: 10 Years, 9 Months, 1 Week ago
hi Saket,

Yeah, thanks. That makes sense.

But if you comment the 3rd line i.e. try this code

Code:
DataTable("MyCol","Action1")=123456
row=DataTable.GetSheet("Action1").getCurrentRow
DataTable.GetSheet("Action1").SetNextRow
DataTable("MyCol","Action1")=1255

according to your explanation, this should write 1255 in 2nd row. But this is writing in the first row itself. why is it so??
Reply
#4
Solved: 10 Years, 9 Months, 1 Week ago
Hi Vijyendra, you will need to understand the difference between the two i.e. SetCurrentRow and SetNextRow

SetCurrentRow sets the row specified as the current row in your Data Table, where SetNextRow sets the row after the active row as the new current row in the Data table. If the current row is the last row in the Data Table, this sets the first row in the Data Table as the new current row.
in your case here,
Code:
DataTable("MyCol","Action1")=123456
DataTable.GetSheet("Action1").SetNextRow
DataTable("MyCol","Action1")=1255
when you set a value in your first row. it is the last row in your datatable so applying SetNextRow will go back the first row itself,at the same time when you set the next row as your current active row using SetCurrentRow the current active row becomes 2 and applying SetNextRow activates 3rd row (below code)
Code:
DataTable("MyCol","Action1")=123456
row=DataTable.GetSheet("Action1").getCurrentRow
DataTable.SetCurrentRow(row+1)
DataTable.GetSheet("Action1").SetNextRow

if you put a SetNextRow again it will be back to the first row again as the current active is the last row
Code:
DataTable("MyCol","Action1")=123456
row=DataTable.GetSheet("Action1").getCurrentRow
DataTable.SetCurrentRow(row+1)
DataTable.GetSheet("Action1").SetNextRow
DataTable.GetSheet("Action1").SetNextRow

to better understand this, have a look at the below code
Code:
DataTable.SetCurrentRow(1)
DataTable.GetSheet("Action1").SetNextRow
first statement activates the first row as the active row
applying SetNextRow in the second statement activates row 2 as the active row. you can not use setnext row alone.
try this
Code:
DataTable.GetSheet("Action1").SetNextRow
DataTable("MyCol","Action1")=123456
DataTable.GetSheet("Action1").SetNextRow
first stament will work here and goes to next row as by default the active row is 1.
on the second setnextrow stament it goes back to the first row

use SetNextRow only when the next row contains at least one value and use it when you are retriving values from Data table.
while setting values to datatable SetCurrentrow is sufficient, applying SetNextRow may create confusions.

Hope I am able to make it clear to you Smile

Reply
#5
Solved: 10 Years, 9 Months, 1 Week ago
Perfect answer!!!

This was very helpful indeed!
10/10

Cheers,
Vijayendra
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Shy SetNextRow is not working in QTP 9.2 Vswarna 1 2,204 09-26-2012, 12:00 PM
Last Post: Ankesh

Forum Jump:


Users browsing this thread: 1 Guest(s)