Accessing a Simple Data Table via C#

Class library usage, coding and language questions.
RandomName
Posts: 6
Joined: Thu Sep 06, 2012 7:38 pm

Accessing a Simple Data Table via C#

Post by RandomName » Thu Sep 06, 2012 7:43 pm

Hi,

I've made a simple data table with Ranorex, but I'm trying to get and set its values during runtime with C#. I've looked around for a while but I can't figure this one out. Can anyone please help me with this?

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Accessing a Simple Data Table via C#

Post by Support Team » Fri Sep 07, 2012 11:54 am

Hi,

You meant in your application under test is a Table and you added the identified table adapter to Ranorex, or do you really created a C# data table in Ranorex?
Please send us some more information abut your Table and the AUT which includes it, which technologies are used and how did you try to set/get the values?
Which Ranorex version are you using?
Please also send us a Ranorex Snapshot file of that table.
Following link will show you how to generate a snapshot file:
Creating Ranorex Snapshot Files.

Regards,
Markus
Ranorex Support Team
.
Image

RandomName
Posts: 6
Joined: Thu Sep 06, 2012 7:38 pm

Re: Accessing a Simple Data Table via C#

Post by RandomName » Fri Sep 07, 2012 3:12 pm

I have attached a screenshot of my Ranorex showing the table I created with Ranorex. I created a simple data table in the Data Source window, and I would like to know how I can access that table with a C# code module while the Ranorex recording is playing. For instance, I would like to be able to add a row to the table, or change the value of row 2 column 1 with C#. I'm not using any special technologies, just Ranorex and standard C#. I am using Ranorex 3.2.2
Attachments
RanorexTable.PNG
RanorexTable.PNG (195.67 KiB) Viewed 5405 times

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Accessing a Simple Data Table via C#

Post by Support Team » Mon Sep 10, 2012 4:10 pm

Hi,

I have attached a sample project for you. The sample shows how to add customized data to a data source.

I have pretty much 2 Test Cases. The first includes the code module where you can add your data to the data source you created through the Test Suite GUI. So, all your data can be added to the data source table as shown in the code module.
TestSuiteView.png
TestSuite
TestSuiteView.png (36.12 KiB) Viewed 5380 times
DataConnectorView.png
DataConnector
DataConnectorView.png (49.47 KiB) Viewed 5380 times
The second Test Case provides the data source “myConnector” which is a simple data table you need to create initially. I added to columns and bound 2 variables from a recorder. I just write the values into the reporting file (just to demo it). In the code module I add a row with 2 values.

So, what you can do is the following: just create a simple data source table as shown above. Then use a code module to dynamically add the data to the data source. Each recorder which uses the data source will now iterate through all the rows you added. That’s pretty much it.

I hope this proves helpful.

Here are also two links to similar forum posts: http://www.ranorex.com/forum/csv-file-r ... t3520.html
http://www.ranorex.com/forum/test-data- ... 95-15.html, maybe these links are also helpful.

Regards,
Markus
Ranorex Support Team
Attachments
Example.zip
(355.79 KiB) Downloaded 398 times
.
Image

RandomName
Posts: 6
Joined: Thu Sep 06, 2012 7:38 pm

Re: Accessing a Simple Data Table via C#

Post by RandomName » Tue Sep 11, 2012 10:31 pm

Thank you for the solution, it has been very helpful so far. The posted solution said how to add rows to the table:

Code: Select all

TestCase.Current.DataContext.Source.Rows.Add(list);
but my question now: how can I access this table and print out its contents (in C#), and how could I edit the contents of specific rows?

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Accessing a Simple Data Table via C#

Post by Support Team » Wed Sep 12, 2012 12:52 pm

Hi,

This can be done as follows:
foreach (var row in dataConn.Rows){
  foreach(var values in row.Values){
    Report.Info("Values:"+values.ToString());
  }
}
			
dataConn.Rows[1].Values[1]="test";
Regards,
Markus
Ranorex Support Team
.
Image

RandomName
Posts: 6
Joined: Thu Sep 06, 2012 7:38 pm

Re: Accessing a Simple Data Table via C#

Post by RandomName » Tue Sep 18, 2012 10:29 pm

Thank you again for the great example. I am now at another roadblock, but am close to figuring this all out.

I would like to be able to access the data table without making references to a test case object. Right now this is how I access the data:

Code: Select all

TestCase.Current.DataContext.CurrentRow.Values[0] = example[0];
Is it possible to access the same data without using the current test case object? I ask because I would like to access it in program.cs before the test case is initialized.

Code: Select all

        public static int Main(string[] args)
        {
            Keyboard.AbortKey = System.Windows.Forms.Keys.Pause;
            int error = 0;

            try
            {
                // This is where I want to access the data table
            	 TestCase.Current.DataContext.CurrentRow.Values[0] = example[0] 
                error = TestSuiteRunner.Run(typeof(Program), 
                            Environment.CommandLine);
            }
            catch (Exception e)
            {
                Report.Error("Unexpected exception occurred: " + e.ToString());
                error = -1;
            }
            return error;
        }
So is there another way to access a data table by it's name maybe that would work before TestSuiteRunner.Run()?

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Accessing a Simple Data Table via C#

Post by Support Team » Wed Sep 19, 2012 10:29 am

Hi,

You cannot access the Data Connector before it is created, the example I attached in one of the previous posts shows how you can get the connector with its name in a previous test case.

Regards,
Markus
Ranorex Support Team
.
Image

RandomName
Posts: 6
Joined: Thu Sep 06, 2012 7:38 pm

Re: Accessing a Simple Data Table via C#

Post by RandomName » Wed Sep 19, 2012 3:34 pm

Thanks again for the reply. So there is no way to access a data table before the TestSuiteRunner.Run()? I would think I should be able to access the data table whenever I want since it already exists in the Ranorex Solution and I'm not making any specific references to a test case.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Accessing a Simple Data Table via C#

Post by Support Team » Thu Sep 20, 2012 3:49 pm

Hi,

Of course you are using a test case "reference": TestCase.Current....
Is there a reason why you doesn't want to make it in the first test case of your solution?
This would work without any issues!

Regards,
Markus
Ranorex Support Team
.
Image

RandomName
Posts: 6
Joined: Thu Sep 06, 2012 7:38 pm

Re: Accessing a Simple Data Table via C#

Post by RandomName » Fri Sep 21, 2012 2:48 pm

I'm trying to change the values of the datatable at the very start of the program, before the first test case has even started. I've created a popup window that allows the user to easily set certain values they want to test, and then those values are saved to the data table. This way when the first test case starts, it already has the final data table and no more changes need to be made.

I'm also trying to call this code in program.cs because I need to make this feature as painless to implement as possible for my team who has no coding experience.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Accessing a Simple Data Table via C#

Post by Support Team » Fri Sep 21, 2012 4:43 pm

Hello,

You can use an Excel file as data source. How to change Excel files was discussed in the forum post
My Excel Framework Methods
Is this an option for you?

Regards,
Bernhard
Ranorex Support Team
.
Image