Call data source connection from user code

Ranorex Studio, Spy, Recorder, and Driver.
Posts: 34
Joined: Tue Sep 17, 2013 1:45 pm

Call data source connection from user code

Post by ElSticky » Fri Dec 27, 2013 1:10 pm

I'm looking for the best way to validate if some newly added value is added to my database. So I tought of binding my project with a data source containing the select which returns a table that contains the newly added item. Is it possible to call this data source from user code and add the returned items from the data source to an array? So I can loop through the items returned by the data source and check if the new created one is in there.

User avatar
Posts: 61
Joined: Tue Dec 17, 2013 6:42 pm

Re: Call data source connection from user code

Post by mebner » Mon Dec 30, 2013 2:47 pm


You could use a data source to validate the values.
Each row of the data source represents one iteration, so you need to design your test case in a way that each iteration validates one value of your database.
You could also define the range for the data source, you can define that just the newly added row should be used for the validation, used as data value.
Do you already now the section about data driven testing?


Posts: 3
Joined: Wed Jun 04, 2014 5:59 pm

Re: Call data source connection from user code

Post by Vic » Sun Nov 16, 2014 7:58 pm


I am having similar problem.

The scenario is as follow :

1. Begin the execution by opening a browser (and a web page)
2. In the user code, initialize a variable with a data from the repository (e.g. varA = "AAAA")
3. Call the select in the data source with the variable varA and return a specific data from the database table

It seems like that the data source is the first thing called during the execution. At that point the webpage is not opened yet and varA has no value.

How to have a specific data from a database from the user code?


Attached the datasource used.

Windows XP
Ranorex 5.0.3
DataSource Test.png
DataSource Test.png (32.74 KiB) Viewed 3508 times

Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Call data source connection from user code

Post by krstcs » Mon Nov 17, 2014 3:30 pm

You cannot pass test variables into the data connectors directly. Data connectors are static.

The only thing you can do is to manipulate the SQL query right before the data connector is used. You will have to do this in user code.

1. Create your data connector with a default data set. This will allow you to bind the connector to the variables.
2. Create a user-code module.
3. Add a test variable to the module and name it the same as the variable you want to use in the query (varA).
4. In the Run() method, change the sql query for the specific data connector you are wanting to manipulate.

Code: Select all

((SqlDataConnector)DataSources.Get("<Your Data Connector's name here>).Connector).Query = "select * from myTable where name=" + varA;

//my method...
        public static void SetupSqlDataConnector(string dataCacheName, string queryString) {
            ((SqlDataConnector)DataSources.Get(dataCacheName).Connector).Query = queryString;
I actually have this in method in a DLL that I use for all of my tests, as you can see in the second part above.

Place the user-code module RIGHT BEFORE the test case that uses the data connector and bind the variable.
Shortcuts usually aren't...