Page 1 of 2
DB local instance

Posted:
Mon Aug 13, 2018 1:45 pm
by Riyaz
Hi Mike
I found the below example on Jiwa documentation for connecting to DB, question is how do I connect to the local jiwa application instance instead of providing server, db name, etc
var manager = new JiwaFinancials.Jiwa.JiwaApplication.Manager();
manager.Logon("MyServer", "JiwaDemo", JiwaFinancials.Jiwa.JiwaODBC.database.AuthenticationModes.JiwaAuthentication, "Admin", password);
manager.BusinessLogicFactory.CreateBusinessLogic<JiwaFinancials.Jiwa.JiwaSales.SalesOrder.SalesOrder>()null;
var db = manager.Database;
using (SqlCommand SQLCmd = new SqlCommand(sql, db.SQLConnection, db.SQLTransaction))
{
}
Re: DB local instance

Posted:
Mon Aug 13, 2018 2:00 pm
by Scott.Pearce
What do you mean by "local jiwa application instance"? If you are referring to localDB, you simply use "(LocalDb)\MSSQLLocalDB" as the server name.
Re: DB local instance

Posted:
Mon Aug 13, 2018 2:04 pm
by Riyaz
Hi Scott
What I mean, is that we usually develop plugins on our local instance and then we export and import on to the server instance of our clients, I dont want to edit the plugin and change the database name, username, password etc everytime I do that, so if there is a way where I can connect to the database of jiwa without mentioning user details etc, thats what am after
Re: DB local instance

Posted:
Mon Aug 13, 2018 3:10 pm
by Scott.Pearce
Code executing as part of a plugin is already "logged in" to whatever database the plugin resides in. You should not be logging in again via code.
Re: DB local instance

Posted:
Mon Aug 13, 2018 3:30 pm
by Riyaz
Thats what I thought, but it gives me the runtime error "The connection property has not been initialized, Module: InvokeMethod",
below is the code used
Dim manager = New JiwaFinancials.Jiwa.JiwaApplication.Manager()
With manager.Database
Try
SQL = "SELECT dbo.IN_Main.Partno, dbo.IN_Main.Description FROM dbo.IN_Main WHERE PartNo = @PartNo ORDER BY PartNo "
Using SQLCmd As SqlCommand = New SqlCommand(SQL, .SQLConnection, .SQLTransaction)
SQLParam = New SqlParameter("@PartNo", System.Data.SqlDbType.Char)
SQLParam.Value = CustomFieldValue.Contents
SQLCmd.Parameters.Add(SQLParam)
SQLReader = .ExecuteReader(SQLCmd)
If SQLReader.Read = True Then
CustomFieldValue.DisplayContents = String.Format("{0} / {1}", .Sanitise(SQLReader, "PartNo"), .Sanitise(SQLReader, "Description"))
End If
End Using
SQLReader.Close()
Finally
If Not SQLReader Is Nothing Then
SQLReader.Close()
End If
End Try
End With
Re: DB local instance

Posted:
Mon Aug 13, 2018 3:32 pm
by Scott.Pearce
You should not be new-ing a manager object - there should already be one for you to use that is set up and logged in.
Tell me which version of Jiwa you are using here, and also show me the function in which that code resides.
Re: DB local instance

Posted:
Mon Aug 13, 2018 3:36 pm
by Riyaz
Using Jiwa 7.2
Function as below
Public Sub ReadData(ByVal BusinessLogicHost As JiwaApplication.IJiwaBusinessLogic, ByVal GridObject As JiwaApplication.Controls.JiwaGrid, ByVal FormObject As JiwaApplication.IJiwaForm, ByVal Row As Integer, ByVal HostObject As JiwaApplication.IJiwaCustomFieldValues, ByVal CustomField As JiwaApplication.CustomFields.CustomField, ByVal CustomFieldValue As JiwaApplication.CustomFields.CustomFieldValue) Implements JiwaApplication.IJiwaCustomFieldPlugin.ReadData
Dim SQLParam As SqlClient.SqlParameter = Nothing
Dim SQLReader As SqlDataReader = Nothing
If CustomField.PluginCustomField.Name.Trim = "40-SalesPartNo" Then
Dim SQL As String = ""
Dim manager = New JiwaFinancials.Jiwa.JiwaApplication.Manager()
With manager.Database
Try
SQL = "SELECT dbo.IN_Main.Partno, dbo.IN_Main.Description FROM dbo.IN_Main WHERE PartNo = @PartNo ORDER BY PartNo "
Using SQLCmd As SqlCommand = New SqlCommand(SQL, .SQLConnection, .SQLTransaction)
SQLParam = New SqlParameter("@PartNo", System.Data.SqlDbType.Char)
SQLParam.Value = CustomFieldValue.Contents
SQLCmd.Parameters.Add(SQLParam)
SQLReader = .ExecuteReader(SQLCmd)
If SQLReader.Read = True Then
CustomFieldValue.DisplayContents = String.Format("{0} / {1}", .Sanitise(SQLReader, "PartNo"), .Sanitise(SQLReader, "Description"))
End If
End Using
SQLReader.Close()
Finally
If Not SQLReader Is Nothing Then
SQLReader.Close()
End If
End Try
End With
End If
End Sub
Re: DB local instance 

Posted:
Mon Aug 13, 2018 4:02 pm
by Scott.Pearce
Every Jiwa object points to a single manager object that was created and configured at log in time. You should use the existing manager object too. It can be accessed via a property that exists as part of most (if not all) Jiwa objects.
So, change your line:
- Code: Select all
Dim manager = New JiwaFinancials.Jiwa.JiwaApplication.Manager()
to:
- Code: Select all
Dim manager = CustomField.Manager
CustomField was passed into the ReadData() function, and it has a manager property
that you can use. Similarly BusinessLogicHost, GridObject, FormObject, HostObject, and CustomFieldValue are passed in - these would all have manager objects accessible for you to use if required.
Re: DB local instance

Posted:
Mon Aug 13, 2018 4:18 pm
by Riyaz
Thanks Scott, that worked
Re: DB local instance

Posted:
Mon Aug 13, 2018 4:22 pm
by Scott.Pearce
Cool.
Don't forget. Inside a plugin you are already logged in.
7.0.175.0 and earlier only:
Use JiwaFinancials.Jiwa.JiwaApplication.Manager.Instance
7.1 and later only:
Use an existing manager from an existing object (all Jiwa objects have a manager property for you to leverage).
Always use a factory to create new Jiwa objects.