Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Retrieve row number based on value in DataTable
#1
Solved: 11 Years, 4 Months ago
Hi,

I've got a DataTable and I need the script to search the DataTable, find a value in a particular column that matches the condition and retrieve a row number of that row which contains the searched value and run 1 iteration on the row number retrieved.

Example, I want to run test on row which contains value = E in col2, so the result should be row number = 2

col1 col2 col3
A B C
D E F
G H I


Reply
#2
Solved: 11 Years, 4 Months ago
Hi,
you can run a For loop for the same purpose....Flow will be like this:
1.First get row count of the excel sheet with GetRowCount method.
2.Store it in a variable say Row_Count.
3.Run a For loop say "For i=1 to Row_Count
4.get the value of Coumn2 by "DataTabe.Value" method and store it in a variable say "Temp"
5.Compare the value Temp wiht value"E" and now u can run your test case accordinly...

Hope it will be helpful..........

Regards
Reply
#3
Solved: 11 Years, 4 Months ago
Thank you. So it would be like this? :

Code:
row_count = DataTable.GetRowCount
For i=1 to row_count

temp = DataTable.Value("parameter_name")
If temp = "text string" then
SetCurrentRow(i)
run the test case
End If

Next

How do I do the script stop after 1 run according to condition? I mean, the value (E) will be in more rows but I want to run it only on the first row that matches.
Reply
#4
Solved: 11 Years, 4 Months ago
You can jump out of For loop once you are getting the expected value i.e "E" for you like this:

If temp="E" Then Exit For

Regards
Reply
#5
Solved: 11 Years, 4 Months ago
Marfa,

Try out this:

Code:
getRow = Browser("YourBrowserName").Page("YourPageName").WebTable("TableName").GetRowWithCellText("E",2)  // you can replace E and 2 with any string and number respectively

Search "GetRowWithCellText" method in QTP help file.
Reply
#6
Solved: 11 Years, 4 Months ago
Woah..not a advisable method to go with a webtable. This is a method which gets confused if it finds an E in any other row and will simply mislead you into a trap. Kind of a blind method when you have a webtable with minimal rows and not many conflicting data. Better to avoid this method.
It is always advisable to loop through a webtable, retrieve the value, compare against your expected and when matched
Exit the loop.
Basanth
Give a fish to a man and you feed him for a day..Teach a man how to fish and you feed him for life.
Reply
#7
Solved: 11 Years, 4 Months ago
My issue is quite similar.

I have a table with 2 columns (LoginName and Password) and 468 rows.
I need to get the login name and password using a input box and on clicking login button. The system should verify if the login name and password is present in the table and only then allow to login. when i try executing the following code the loop is executed 468 times but exits unsuccessfully.

Pls help. below is the code that i used.

Code:
Rowcount= DataTable.GetSheet("Global").GetRowCount
msgbox "RowCount= " &RowCount,1
CurrentRow= DataTable.SetCurrentRow(1)

Browser("").Sync
Browser("").Page("").Sync

Browser("").Page("").Frame("Frame").WebEdit("UserName").Click
a= inputbox("Enter User Name")

For i = 1 To Rowcount Step 1
    col_LoginName=DataTable.Value("LoginName")
    If col_LoginName=a Then
        SetCurrentRow(i)
        msgbox "done"
        Browser("").Page("").Frame("Frame").WebEdit("Password").Click
        b= inputbox("Enter Password")
    End If
Next

Browser("").Page("").Frame("Frame").Link("Login").FireEvent "onClick", validate()

Function validate()
    msgbox "success"
End Function

Regards,
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error as Global Not defined while trying to retrieve value from Datatable siddharth1609 0 964 09-11-2019, 02:52 PM
Last Post: siddharth1609
  Cannot retrieve Native property JeL 1 1,083 07-29-2019, 05:41 PM
Last Post: JeL
  DataGrid: double click on selected row robertosalemi 0 4,436 05-12-2016, 03:00 PM
Last Post: robertosalemi
  Row count is always shown as 1 Harry1999 0 2,090 04-14-2016, 11:11 PM
Last Post: Harry1999
  To retrieve content of a WEBEDIT object alpha1 2 2,603 06-11-2015, 11:21 AM
Last Post: vinod123

Forum Jump:


Users browsing this thread: 3 Guest(s)