Primal Form and Database driven datagrids

Archived support forum for customers who once purchased a PrimalForms product license. This forum is locked.
This topic is 14 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
tconley@semprautilities.com
Posts: 18
Last visit: Thu May 27, 2021 11:28 am

Primal Form and Database driven datagrids

Post by tconley@semprautilities.com »

I just got the full version of primal forms and I was wondering how to setup a binding either to a database or to odbc to the database. I am trying to put a gui from end on one of my scripts. I have developed a small access data as a prototype for my app, eventually i will have it moved up to a sql server. I just cant seem to figure out how to bind to the sql datasource toget the information. I have found some examples but they are for the community edition and it requires continual export and readding of the script code. I had my boss buy the full version to try and eliminate that tedium.

Does anyone have a sample or proceedure for binding to the database?tconley@semprautilities.com2009-12-11 16:10:17
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Primal Form and Database driven datagrids

Post by davidc »

We don't have any Database examples, but we will add it to our list.

Chad has a blog on how to access a database and load the data into a grid on this blog: http://chadwickmiller.spaces.live.com/b ... !493.entry

If you use ODBC you will need to replace the SQL objects with the ODBC versions:

I.e. System.Data.SqlClient.SqlDataAdapter becomes System.Data.Odbc.OdbcDataAdapter, etc.

You can also try PrimalSQL, which allows you to export queries to PowerShell. You may find the resulting script helpful. You can download the 45 Day Trial from http://www.sapien.com/software/primalsql.

For more complete scripting help I recommend posting this question in Scripting Answers.

Also don't forget to add the "System.Data" assembly to your Form.

David
David
SAPIEN Technologies, Inc.
User avatar
tconley@semprautilities.com
Posts: 18
Last visit: Thu May 27, 2021 11:28 am

Primal Form and Database driven datagrids

Post by tconley@semprautilities.com »

i am getting the following error when i use his code and i am not sure why?

Error:
ERROR: Property 'SelectCommand' cannot be found on this object; make sure it exists anERROR: d is settable.ERROR: At line:40 char:15


Code:

function OnApplicationLoad {#Note: This function runs before the form is created#Note: To get the script directory in the Packager use: Split-Path $hostinvocation.MyCommand.path#Note: To get the console output in the Packager (Windows Mode) use: $ConsoleOutput (Type: System.Collections.ArrayList)#TODO: Add snapins and custom code to validate the application load
return $true #return true for success or false for failure $bindingSource1 = new-object System.Windows.Forms.BindingSource $dataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter $commandBuilder = new-object System.Data.SqlClient.SqlCommandBuilder $dataAdapter
$SQLServer = "server" $databaseName = "testdb" $Query="SELECT * from queues"
}
$handler_form1_Load={#TODO: Place custom script here $dataGridView1.DataSource = $bindingSource1 $connString = "Server=$serverName;Database=$databaseName;Integrated Security=SSPI;"
$dataAdapter.SelectCommand = new-object System.Data.SqlClient.SqlCommand ($query,$connString) $commandBuilder = new-object System.Data.SqlClient.SqlCommandBuilder $dataAdapter $dt = New-Object System.Data.DataTable [void]$dataAdapter.fill($dt) $bindingSource1.DataSource = $dt
$dataGridView1.AutoResizeColumns([System.Windows.Forms.DataGridViewAutoSizeColumnsMode]::AllCellsExceptHeader) }
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Primal Form and Database driven datagrids

Post by davidc »

The objects you create in the OnApplicationLoad function are never called because the function exits (on the return $true line) before it can reach these lines. In addition, when you create objects in the OnApplicationLoad function, you will have to declare them in the script scope otherwise they will not be accessible in your event handlers.
Corrected Function:

function OnApplicationLoad {#Note: This function runs before the form is created#Note: To get the script directory in the Packager use: Split-Path $hostinvocation.MyCommand.path#Note: To get the console output in the Packager (Windows Mode) use: $ConsoleOutput (Type: System.Collections.ArrayList)#TODO: Add snapins and custom code to validate the application load
$script:bindingSource1 = new-object System.Windows.Forms.BindingSource $script:dataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter $script:commandBuilder = new-object System.Data.SqlClient.SqlCommandBuilder $script:dataAdapter
$script:SQLServer = "server" $script:databaseName = "testdb" $script:Query="SELECT * from queues"

return $true #return true for success or false for failure}


We are going to post a blog explaining how OnApplicationLoad function works very soon. Keep an eye out for it on http://www.sapien.com/blog.



David
David
SAPIEN Technologies, Inc.
This topic is 14 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.