Hello there,
I want to reuse an existing Database Connection.
The idea is to use
assign
in_Config(“X)”) = dbconnection
to store it in the Config dicitionary
later I want to get the dbconnection out of the config.
assign
dbconnection = convert.ChangeType( in_Config(“x”), in_Config(“x”).GetType)
is not working.
Error Message:
Cannot assign from type ‘System.Object’ to type
‘UiPath.Databse.DatabaseConnection’ in Assign activity Assign.
Any thoughts or ideas to solve this issue?
Is there a way to get this working?
**The workarround to pass the dbconnection as out argument is known. **
Thanks for your help / comments.
1 Like
thorsten-wack:
dbconnection
I think this variable should be of type UiPath.Databse.DatabaseConnection
You can try this.
I think this is the most reliable and the robust way you should go with the same.
What’s the problem you are facing while sending it as an out argument?
Hi I like to pass as few arguments as possible therefore putting almost everything in the dictionary.
Especially if you have several layers in your code I tend to avoid passing an argument multiple times form workflow to workflow.
Hey @thorsten-wack
For this case, you can go for serialisation.
Serialise your object to string & then can be saved to config file.
Which can then be utilised in the code back by de-serializing.
Hope this helps
Thanks
#nK
1 Like
Hello #nK , could you provide an example for this, please?
I am afraid this is overdoing it
Regards
#T
1 Like
Hey @thorsten-wack
Sure… Here you go,
nmnithinkrishna_SaveDbConnInConfig.zip (2.1 KB)
I don’t see a big deal of overdoing in it. It’s just a one step addition.
I have used dummy props for the activity, whereas in your case you can go with actual values
Hope this helps.
Thanks
#nK
1 Like
ppr
(Peter)
April 7, 2022, 11:44am
#8
Give a try at:
DirectCast(in_Config("x"), in_Config("x").GetType)
1 Like
Hi #nK ,
I cannot see the activity.
Do I need to install aspecific package?
Error Message
File ‘Main.xaml’ - variable ‘obj_dbConn’: Type ‘ui:DatabaseConnection’ (‘http://schemas.uipath.com/workflow/activities ’) could not be resolved and was automatically replaced with ‘UnknownType’
regards
#T
1 Like
Hey @thorsten-wack
It should auto download.
Kindly have a look at the project json for more details.
Btw the packages used are this,
"Newtonsoft.Json": "[13.0.1]",
"UiPath.Database.Activities": "[1.5.0]",
"UiPath.Excel.Activities": "[2.11.4]",
"UiPath.System.Activities": "[21.10.4]"
Thanks
#nK
Hi Peter,
it worked almost.
this is how I got it to work.
DirectCast(Config(“conn”),DatabaseConnection)
Using the DirectCast together with the Type DatabaseConnection.
For some reason it doesn’t let you use the GetType Method for the Config Object.
Regards
#T
Cool @thorsten-wack
Also sorry, Actually I missed to add the de-serialize part, but I guess you should have figured it by this time.
1 Like
Hi #nK ,
I tried it and there seems to be an issue with the serialize part.
Regards
#T
Hey @thorsten-wack
In the serialization, I hope you got the right value and saved it into the config.
The same should be de-serialized now.
Because, I can see when you print the conn string JSON
it has nothing in it.
Thanks
#nK
Hi #nK ,
I have not put the Object into the config.
This is how the workflow looks like.
As you can see.
I start off with the connect.
Then I query the DB.
After I log the connection object.
Next the JSON.Convert Action takes place in the Assign activity.
The following log message shows the resulting string is empty.
The result of this Newtonsoft.Json.JsonConvert.SerializeObject(obj_dbConn) is the empty string.
Any idea why this is not working?
When I tried to figure out more about storing a connect in a JSON Object I found some hints, that this might be not a good ide in some cases refering to certain Databases.
I am using SQL Server by the way.
Regards
#T
1 Like
Hey @thorsten-wack
Could you please debug it and check if the DB_Conn
object has some value in it before serialization ?
Thanks
#nK
str_dbConn now has a length of 2 after serializing the connection
1 Like
Hey @thorsten-wack
I can see there is nothing coming in the db_conn object as appearing in the locals panel.
Thanks
#nK