How to make our own queries with variables retrieved from Excell

Experiences, small talk, and other automation gossip.
Clément
Posts: 1
Joined: Fri Oct 12, 2018 1:25 pm

How to make our own queries with variables retrieved from Excell

Post by Clément » Fri Oct 12, 2018 1:37 pm

Hello,
I know this subject has been already treated but I still have issue with.
So, I tried the code I've saw on the forum :
[language=]
using Ranorex.Core.Data;

...

string dataCacheName = "<Put the name of your data connector here>";
string newQueryString = string.Format("EXEC usp_TimeCodesPerDU {0}", DU_ID); //assuming your unique ID is an int value, otherwise you need to wrap the {0} with single quotes like: '{0}'

DataCache dc = DataSources.Get(dataCacheName);
((SqlDataConnector)dc.Connector).Query = newQueryString;
dc.Load();
[/language]

Here my code :
[language=]
using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Testing;
using Ranorex.Core.Data;
. . . . . . . . . . . . . . . . . . .

string _Compte = "508007848017899";
[TestVariable("f78fcabb-3db8-4c5b-8a4e-1315e30a60a0")]
public string Compte
{
get { return _Compte; }
set { _Compte = value; }
}



string _Nom = "Qsdfgerty";
[TestVariable("f9975a38-a288-4399-82a7-1facbed5ed54")]
public string Nom
{
get { return _Nom; }
set { _Nom = value; }
}

void ITestModule.Run()
{
string query = "PostgreSQL";
string newQueryString = string.Format("SELECT dos.code FROM watt2.dossier dos JOIN watt2.document doc on dos.documentgenerateur_id = doc.id WHERE numcot = {0} AND nomsal = '{1}' ", _Compte, _Nom);

DataCache dc = DataSources.Get(query);
((SqlDataConnector) dc.Connector).Query = newQueryString; // having at this line
dc.Load();
}[/language]

But I'm having error : " La référence d'objet n'est pas définie à une instance d'un objet. " (sorry for no translation)

Here I'm trying to retrieve data from Excell and put them in variables "_Compte" and "_Nom" then execute query.

Hope you can help me,
Regards.

semate
Posts: 12
Joined: Tue Jul 03, 2018 7:42 am

Re: How to make our own queries with variables retrieved from Excell

Post by semate » Thu Dec 13, 2018 5:08 pm

Hi!

are you sure your DataConnector is named "PostgreSQL"?

For the example below ...
DataConnector.PNG
DataConnector.PNG (2.07 KiB) Viewed 97 times
... you would have to use "DYN_TestEnv" as Name like:

Code: Select all

DataCache dc = DataSources.Get("DYN_TestEnv");
BTW, you may want to consider to use the code block (button </> ) when posting code. Makes it easier to read in my eyes.
Hope it helps.