Update data connector

Best practices, code snippets for common functionality, examples, and guidelines.
agroenen
Posts: 17
Joined: Thu Jun 09, 2016 3:18 pm

Update data connector

Post by agroenen » Thu Jun 23, 2016 1:37 pm

Hi,

I'm pretty new at Ranorex, and not able to code yet.

I made an Access database with some variables of an address in it, and bound them to my testcase. That works fine! But what I want now is that if the test case succeeds, the housenumber in the Access database is increased with 1.

We start for example with Testingstreet 1, next time that should be Testingstreet 2.

Is it possible to include this in my recording? I see I can use GetValue to pick up the latest used number, but how do I pass this value+1 to my Access table?

jma
Posts: 111
Joined: Fri Jul 03, 2015 9:18 am

Re: Update data connector

Post by jma » Mon Jun 27, 2016 1:35 pm

Hi,

I'm afraid I'm not exactly sure what you want to achieve. Did I understand you correctly that you want to update the data source (in your case, the database table) during test execution or do you need this incremented value only for the current iteration of the test (for example, to enter this value in any input field)?

Could you upload a screenshot of the current content of the data source? Probably, it would be helpful to try illustrating the issue with a sample solution.

agroenen
Posts: 17
Joined: Thu Jun 09, 2016 3:18 pm

Re: Update data connector

Post by agroenen » Mon Jun 27, 2016 2:57 pm

I want to write TO the Access database.

I created a new house address: Testingstreet 1, 0000AA TestCity.
Next time, I want to use house number 2, because it is forbidden to create an address that already exists.
So I want to GetValue from the created address (value = 1), and write this +1 to the Access table.
Attachments
DB.jpg
DB.jpg (29.42 KiB) Viewed 905 times

jma
Posts: 111
Joined: Fri Jul 03, 2015 9:18 am

Re: Update data connector

Post by jma » Tue Jun 28, 2016 1:11 pm

Ranorex doesn't provide any API functionality to update database information, but Ranorex is basically based on the .NET framework, which means that you could implement this functionality with C# code.

I would suggest having a look at the following instruction and the corresponding stackoverflow discussion.

In Ranorex this could be, for example, implemented as a user code action.

Hope this helps!

agroenen
Posts: 17
Joined: Thu Jun 09, 2016 3:18 pm

Re: Update data connector

Post by agroenen » Tue Jun 28, 2016 3:25 pm

I'm sorry, but not really... I keep getting "The user code class has compile errors".

Code: Select all

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using WinForms = System.Windows.Forms;

using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Testing;

namespace K2W_Desktop._Algemeen
using System.Data.OleDb
{
    public partial class Test
    {

        private void Init()
        {
            // Your recording specific initialization code goes here.
        }

        public void Huisnummer_plus_1()
        {
            try
        {
        OleDbConnection con = new OleDbConnection("M:\\hhs\\Werkruimtes\\Testteam\\Ranorex\\Variabelen\\Variabelen.accdb");
        con.Open();
        OleDbCommand cmd = new OleDbCommand("update [Variabelen] set varHuisnummer = varHuisnummer +1 where id=1", con);
        cmd.ExecuteNonQuery();
        lbl_msg.Text = "Record Updated Successfully.";
        con.Close();
        }
        catch
        {
            lbl_msg.Text ="Error Occured.";
        }


        }

    }
}

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3945
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Update data connector

Post by odklizec » Tue Jun 28, 2016 3:33 pm

Hi,

Change this part of code:

Code: Select all

namespace K2W_Desktop._Algemeen
using System.Data.OleDb
{
To this:

Code: Select all

using System.Data.OleDb;

namespace K2W_Desktop._Algemeen
{
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

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

Re: Update data connector

Post by krstcs » Tue Jun 28, 2016 3:41 pm

Your code has definite formatting issues, just from looking at it.

If you have never coded (especially in C#.NET) before, I would highly recommend some training or reading before you try to do what you are wanting to do. What you want to do is a non-trivial task and not knowing how to code will only complicate it. There is only so much we can help with, and if you don't understand the code, or the changes we recommend, it will be that much harder.

For example:

The "using" directives must ALL be above the "namespace" declaration.

All lines of code must end with a semicolon (";") or, for blocks, a block closing bracket ("}"). All blocks must have an opening bracket ("{") and closing bracket.

Your code has a using statement in between the namespace declaration and the namespace's block. That using statement also doesn't have a semicolon ending the line.

In addition, this code is a partial class. Since you don't give us the full class declaration, it is hard to know what you are dealing with. Is this a partial class that is part of a recording module?

Code: Select all

namespace K2W_Desktop._Algemeen
using System.Data.OleDb
{
Should be:

Code: Select all

using System.Data.OleDb;

namespace K2W_Desktop._Algemeen
{
Shortcuts usually aren't...

agroenen
Posts: 17
Joined: Thu Jun 09, 2016 3:18 pm

Re: Update data connector

Post by agroenen » Wed Jun 29, 2016 10:15 am

The instruction link I got from jma says: Use below namespace. using System.Data.OleDb;
I found that strange, but tried it. Also the other way around... still a compiling error.

And that was the missing semicolon, I totally missed that one... :? Sorry for that!!

Got it working now, BUT not with an Access-2010 database (extension .accdb ).
So I created a new Access-database with extension .mdb, and that was the last trick.

Thanks for your help!