Kirix Support Forums

Connecting to db via script

Please post any help questions, requests or other feedback here.

Connecting to db via script

Postby abenitez77 on Fri Jan 21, 2011 12:49 pm

I want to connect to a database via script. I created this script below but i get an error msg. Can you please help? I want to connect to the database and return the record in a table from my TSQL statement in the script.

{
var conn
conn = CreateObject("ADODB.Connection");
var rst
rst = CreateObject("ADODB.recordset");
var sql

sql = "select * From DTL WHERE VND_NBR < 1000";

conn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=STRATA;Data Source=USATL02PRSQ70";

rst.Open(sql, conn);

while (!rst.EOF){
document.write(rst(0));
document.write("<br>");
rst.MoveNext;
}
rst.Close;
rst=Nothing;
document.close;
}
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby abenitez77 on Fri Jan 21, 2011 3:01 pm

I am not trying something new...but i still can't connect...see my code and error:

var db = new DbConnection("Driver={SQL Native Client};Server=USATL02PRSQ70;Database=STRATA;Trusted_Connection=yes;");

if (db.isConnected()) alert("Connection succeeded");

var db_output = HostApp.getDatabase();

eror msg:
Script Runtime Error: Term 'db.isConnected' does not evaluate to a function.
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby abenitez77 on Fri Jan 21, 2011 4:18 pm

I get a popup msgbox "Connection failed"...
I added this line so my code looks like this now:

var db = new DbConnection("Driver={SQL Native Client};Server=USATL02PRSQ70;Database=STRATA;Trusted_Connection=yes;");

if (db.isConnected()) alert("Connection succeeded");
else alert("Connection failed");

var db_output = HostApp.getDatabase();
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby abenitez77 on Tue Jan 25, 2011 3:13 pm

Hello....is anyone monitoring this Forum???
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby Aaron on Tue Jan 25, 2011 6:10 pm

Hi,

Is this a question about Kirix Strata? If so, this post is in the wrong forum.

Best,
Aaron
Aaron Williams
Kirix Support Team
User avatar
Aaron
Kirix Support Team
 
Posts: 120
Joined: Fri Dec 16, 2005 3:01 pm

Re: Connecting to db via script

Postby abenitez77 on Tue Jan 25, 2011 7:15 pm

Yes it is... where should it be? it says kirix support forum on the heading on top...where i am entering my questions....
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby Ken on Tue Jan 25, 2011 11:38 pm

Hi there,

Please note that you posted in the wxAUI forum, not the Kirix Strat forum (see the gray headers here: http://www.kirix.com/forums/)

I'll move this post to the Kirix Strata forums for you.

Best,
ken
Ken Kaczmarek
Kirix Support Team
User avatar
Ken
Kirix Support Team
 
Posts: 147
Joined: Mon Dec 19, 2005 10:36 am

Re: Connecting to db via script

Postby abenitez77 on Wed Jan 26, 2011 10:20 am

My bad... now that it's in the correct forum location...is there someone that can help me???
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby Aaron on Wed Jan 26, 2011 11:27 am

Ok, first thing is to get your connection working properly. Depending on the database you are connecting to, use one of the following connection strings, using your own values instead of the placeholder values, of course:

Code: Select all
var db;

// example 1: connect to the host application's local database, which
// is the project that's currently open
db = HostApp.getDatabase();

// example 2: connect to a MySQL database called 'dbname', hosted on
// mysql.my.domain at port 3306 with user name 'user' and
// password 'pw'
db = new DbConnection("mysql://user:pw@mysql.my.domain:3306/dbname");

// example 3: connect to a SQL Server database called 'dbname', hosted on
// mssql.my.domain at port 1433 with user name 'user' and
// password 'pw'
db = new DbConnection("mssql://user:pw@mssql.my.domain:1433/dbname");

// example 4: connect to an Oracle database called 'dbname', hosted on
// oracle.my.domain at port 1521 with user name 'user' and
// password 'pw'
db = new DbConnection("oracle://user:pw@oracle.my.domain:1521/dbname");

Aaron Williams
Kirix Support Team
User avatar
Aaron
Kirix Support Team
 
Posts: 120
Joined: Fri Dec 16, 2005 3:01 pm

Re: Connecting to db via script

Postby abenitez77 on Wed Jan 26, 2011 6:33 pm

I get "connection failed"...
I am using trusted connection, so I don't need username and password...it should authenticate me... What do I put for user and pw below? And I replaced "my.domain" with my actual domain "prgx.com". Is that correct?

var db

db = new DbConnection("mssql://user:pw@mssql.prgx.com:1433/STRATA");

if (db.isConnected()) alert("Connection succeeded");
else alert("Connection failed");
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby Ben on Thu Jan 27, 2011 12:12 pm

Ok, it's definitely the connection string. We've had to connect with this authentication mechanism in the past, so I'm pretty confident we can get it working. Let me research what the right string is. I'm installing a test bed here.

All the best,
Ben
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Re: Connecting to db via script

Postby abenitez77 on Thu Jan 27, 2011 6:13 pm

Success! on the connection. I made a few changes in my string, notice the port number I changed to 1434...which is what my company uses. Then I replaced prgx.com with the server name ("USATL02PRSQ70"). Now onto my other tasks... I want to get all the db names and populate them into a listbox for the user to select from in my form. Then I want to populate a separate listbox with all the tables in the db's selected from the first listbox. I created the form, list boxes and button... I just need to populate them with the results. Any help is appreciated...

db = new DbConnection("mssql://USATL02PRSQ70:1434/STRATA");
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby abenitez77 on Thu Jan 27, 2011 7:15 pm

How do i get the results from this into my listbox?

var db;
db = new DbConnection("mssql://USATL02PRSQ70:1434/STRATA");

var result = db.execute("select name from master..sysDatabases where name not in('master', 'model', 'msdb', 'tempdb')");
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby abenitez77 on Fri Jan 28, 2011 10:05 am

FYI...The Notify option for the forum does not work.
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby Aaron on Fri Jan 28, 2011 11:21 am

Hi,

Excellent. Now that you've connected to the database, the next step is to get a list of the tables in the database. To do this, use the connection object to find the tables, making sure to use the appropriate connection string for your environment:

Code: Select all
var a = new DbConnection(<connection string>);
var objects = a.getObjects();

Console.clear();
for (var i = 0; i < objects.length; ++i)
{
    if (objects[i].type != "TABLE")
        continue;

    Console.writeLine(objects[i].name);
}



To find out more about the connection object, including how to query the database and iterate through result sets, check out the documentation here:

http://www.kirix.com/docs/DbConnection.html
http://www.kirix.com/docs/DbResult.html

Also, there's a lot more information about developing extensions, including how to create forms and list controls. Our developer resources will give you an entry point for how the scripting language works overall, as well as how to build simple extensions:

http://www.kirix.com/extensions/developer-resources.html

Also, our extension wizard will help you create the various components or database scripts:

http://www.kirix.com/extensions/extension-wizard.html

By exploring these options, you should be able to load the tables into the list form and let the user select them.

One more thing: Kirix Strata allows users to connect to databases and browse them directly within the application using File->Create Connection, so you can always browse the tables in the database from the application itself.

Best,
Aaron
Aaron Williams
Kirix Support Team
User avatar
Aaron
Kirix Support Team
 
Posts: 120
Joined: Fri Dec 16, 2005 3:01 pm

Re: Connecting to db via script

Postby abenitez77 on Fri Jan 28, 2011 12:52 pm

Worked like a charm...i'm able to populate my listbox...thanks.

I am also have a picturebox that I am trying to use. I am working off of an example on your site but i can't get it to work. Can you help with this ?

var bmp = Extension.getBitmapResource("\\usatl02eras40\AuditFiles\NortheastData\RiteAid\PRGX_Logo_small.jpg");

var width = bmp.getWidth();
var height = bmp.getHeight();

var picturebox = new PictureBox;
var picturebox = new PictureBox(0, 0, width, height-70);
picturebox.setImage(bmp);

// create first column layout
var layout_col1 = new BoxLayout(Layout.Vertical);
layout_col1.addSpacer(16);
layout_col1.add(createControlTitle("PictureBox"), 0, Layout.Bottom, 4);
layout_col1.add(picturebox);
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby Aaron on Fri Jan 28, 2011 1:10 pm

Hi,

Try using Bitmap.loadFile() instead of the Extension function. See here:

http://www.kirix.com/docs/Bitmap.html

Right now, the file you want to display is stored externally on the network; it's not yet packaged up in an Extension. The Extension.getBitmapResource() function is used when you need to access resources that have been packaged up as an extension and installed as an extension to the application. This is the right general direction, and when you're ready, we'll point you in the right direction.

Best,
Aaron
Aaron Williams
Kirix Support Team
User avatar
Aaron
Kirix Support Team
 
Posts: 120
Joined: Fri Dec 16, 2005 3:01 pm

Re: Connecting to db via script

Postby abenitez77 on Fri Jan 28, 2011 1:46 pm

Also, how do i get my results to show up as a table connected under "Projects" for the user to open the table? I don't want the user to pull up the entire table, just the results from the query (ie see example below).

Can I run a query and return just the recordset I want to see under "Projects"? If not Can I run my query and create a table on the sql server and then show that result table in kirix "projects"? I will also want to know how to create a view and show that in "projects" for the user to see and use.

db = new DbConnection("mssql://USATL02PRSQ70:1434/STRATA");

var result = db.execute("select * DTL where ponbr = '7565513'");
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby abenitez77 on Fri Jan 28, 2011 3:31 pm

I get an error msg:
"Script Runtime Error: Object type mismatch (possibly missing 'new' operator)"

here's my full code so far:

var f = new ControlsForm;
f.center();
f.show();

Application.run();

class ControlsForm extends Form
{
var m_title_font = new Font(Font.Default, 10, Font.Bold);

function ControlsForm()
{
super("Controls Form", 0, 0, 800, 640);
setMinSize(600, 560);

// create all controls
var borderbox = new BorderBox("BorderBox");
var button = new Button("ConnectToServer");
button.click.connect(this, onConnectToServer);
var label = new Label("Label");
var line = new Line;
var listbox = new ListBox;
var listbox2 = new ListBox;
//var picturebox = new PictureBox;
var progressbar = new ProgressBar;
var textbox = new TextBox("TextBox");

// add default values to listbox control
Console.clear();

var bmp = Bitmap.loadFile("\\usatl02eras40\Audit Strategy\ASTRATEGY\Projects\Kstrata\PRGX_Logo_small.jpg");
var width = bmp.getWidth();
var height = bmp.getHeight();

//super("Startup Picture", 1, 1, width-20, height);
//this.center();

var picturebox = new picturebox(0, 0, width, height-70);
picturebox.setImage(bmp);
this.add(picturebox);

// create first column layout
var layout_col1 = new BoxLayout(Layout.Vertical);
layout_col1.addSpacer(16);
layout_col1.add(createControlTitle("PictureBox"), 0, Layout.Bottom, 4);
layout_col1.add(picturebox);
layout_col1.addSpacer(64);
layout_col1.add(createControlTitle("Connect to Server"), 0, Layout.Bottom, 4);
layout_col1.add(button);
layout_col1.addSpacer(16);
layout_col1.add(createControlTitle("Label"), 0, Layout.Bottom, 4);
layout_col1.add(label);

// create second column layout
var layout_col2 = new BoxLayout(Layout.Vertical);
layout_col2.addSpacer(16);
layout_col2.add(createControlTitle("Line"), 0, Layout.Bottom, 4);
layout_col2.add(line);
layout_col2.addSpacer(16);
layout_col2.add(createControlTitle("ListBox"), 0, Layout.Bottom, 4);
layout_col2.add(listbox);
layout_col2.addSpacer(16);
layout_col2.add(createControlTitle("ListBox2"), 0, Layout.Bottom, 4);
layout_col2.add(listbox2);
layout_col2.addSpacer(16);
layout_col2.add(createControlTitle("ProgressBar"), 0, Layout.Bottom, 4);
layout_col2.add(progressbar);
layout_col2.addSpacer(16);
layout_col2.add(createControlTitle("TextBox"), 0, Layout.Bottom, 4);
layout_col2.add(textbox);

// create main layout
var main_layout = new BoxLayout(Layout.Horizontal);
main_layout.addSpacer(16);
main_layout.add(layout_col1, 1, Layout.Expand);
main_layout.add(new Line(Line.Vertical), 0, Layout.Expand);
main_layout.addSpacer(16);
main_layout.add(layout_col2, 1, Layout.Expand);
main_layout.add(new Line(Line.Vertical), 0, Layout.Expand);
setLayout(main_layout);
}

function createControlTitle(title)
{
var title_label = new Label(title);
title_label.setFont(m_title_font);
return title_label;
}

function onConnectToServer(sender, event_args)
{
//Connection
var db
db = new DbConnection("mssql://USATL02PRSQ70:1434/STRATA");

var result = db.execute("select name from master..sysDatabases where name not in('master', 'model', 'msdb', 'tempdb')");

var objects = db.getObjects();

// Populate the listbox with the Table names
for (var i = 0; i < objects.length; ++i)
{
if (objects[i].type != "TABLE")
continue;

Console.writeLine(objects[i].name);
listbox.addItem(objects[i].name);
}

// Populate the listbox with all the DB names on the server
while (result.next())
{
listbox2.addItem(result.name);
}

}

}
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby abenitez77 on Fri Jan 28, 2011 4:44 pm

I am trying to get the value from a textbox that I have on the form to populate my string for my connection...see code above.... this line here below... does not work for me...what is the correct syntax?

db = new DbConnection("mssql://USATL02PRSQ70:1434/" + textbox.getText);

if (db.isConnected()) alert("Connection succeeded");
else alert("Connection failed");
alert(textbox.getText);
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby Aaron on Sat Jan 29, 2011 1:46 am

Hi,

Well, first thing, in your code, you have "textbox.getText", which should contain parenthesis after "Text": textbox.getText(). There might be more, since this is a real quick assessment.

Best,
Aaron
Aaron Williams
Kirix Support Team
User avatar
Aaron
Kirix Support Team
 
Posts: 120
Joined: Fri Dec 16, 2005 3:01 pm

Re: Connecting to db via script

Postby Aaron on Sat Jan 29, 2011 1:52 am

Also, I recommend settings that require lines to end in semi-colons and variables to be declared with var. This will flag issues that may cause problems down the road. To do this, include the following at the very top of your script:

use strict variables;
use strict semicolons;

When you run the script, you'll see areas where you are missing semicolons or where variables aren't defined. While ECMAScript technically allows some lines to not end in semicolons or variables to not be defined with var, both of these can cause confusion over what's happening, so it's best to be explicit.
Aaron Williams
Kirix Support Team
User avatar
Aaron
Kirix Support Team
 
Posts: 120
Joined: Fri Dec 16, 2005 3:01 pm

Re: Connecting to db via script

Postby abenitez77 on Sun Jan 30, 2011 5:28 pm

Thanks... I put the connection in a function. How do i pass the funtion textbox.getText() and how can I use the connection outside the function to execute SQL commands using the connection? See me code snippet below...


function onConnectToServer(sender, event_args)
{
//Connection
var db
db = new DbConnection("mssql://USATL02PRSQ70:1434/" + textbox.getText());

var result = db.execute("select name from master..sysDatabases where name not in('master', 'model', 'msdb', 'tempdb')");

var objects = db.getObjects();

// Populate the listbox with the Table names
for (var i = 0; i < objects.length; ++i)
{
if (objects[i].type != "TABLE")
continue;
Console.writeLine(objects[i].name);
listbox.addItem(objects[i].name);
}

// Populate the listbox with all the DB names on the server
while (result.next())
{
listbox2.addItem(result.name);
}

}
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby Aaron on Mon Jan 31, 2011 1:08 pm

Hi,

You can pass the textbox output as a function argument. Right now, you have your function set up the way you'd set up a function that's called when an event is triggered. But if you simply want to pass the textbox text as a parameter of the function, change the function parameters:

Code: Select all
use strict variables;
use strict semicolons;

var text = "example connection";
connectToServer(text);

function connectToServer(server)
{
    alert(server);
}


By the way, in place of alert(server), you can use Console.writeLine(server) to output your text to the console window, which avoids the annoying interruption of alert() and enables you to output values inside loops much more effectively.

Best,
Aaron
Aaron Williams
Kirix Support Team
User avatar
Aaron
Kirix Support Team
 
Posts: 120
Joined: Fri Dec 16, 2005 3:01 pm

Re: Connecting to db via script

Postby abenitez77 on Mon Jan 31, 2011 7:19 pm

I am trying to get the connection to work when I click on the button. It is supposed to grab the text from the textbox (it has the server name) and use it for the connection. In the console, I get the error msg: "Script Runtime Error: Term 'textbox.getText' does not evaluate to a function." I don't get the alerts and my listboxes don't populate.

var f = new ControlsForm;
f.center();
f.show();

Application.run();

class ControlsForm extends Form
{
var m_title_font = new Font(Font.Default, 10, Font.Bold);

function ControlsForm()
{
super("Controls Form", 0, 0, 800, 640);
setMinSize(600, 560);

// create all controls
var borderbox = new BorderBox("BorderBox");
var button = new Button(" ConnectToServer ");
var button2 = new Button(" Retrieve DB Names");
//button.click.connect(this, onConnectToServer);
var label = new Label("Label");
var line = new Line;
var listbox = new ListBox;
var listbox2 = new ListBox;
var picturebox = new PictureBox;
var progressbar = new ProgressBar;
var textbox = new TextBox("Enter Server Name");
button.click.connect(this, onButtonClicked);

Console.clear();

// create first column layout
var layout_col1 = new BoxLayout(Layout.Vertical);
layout_col1.addSpacer(16);
layout_col1.add(createControlTitle("PictureBox"), 0, Layout.Bottom, 4);
layout_col1.add(picturebox);
layout_col1.addSpacer(64);
layout_col1.add(createControlTitle("Connect to Server"), 0, Layout.Bottom, 4);
layout_col1.add(button);
layout_col1.addSpacer(16);
layout_col1.add(createControlTitle("Connect to Server2"), 0, Layout.Bottom, 4);
layout_col1.add(button2);
layout_col1.addSpacer(16);
layout_col1.add(createControlTitle("Label"), 0, Layout.Bottom, 4);
layout_col1.add(label);

// create second column layout
var layout_col2 = new BoxLayout(Layout.Vertical);
layout_col2.addSpacer(16);
layout_col2.add(createControlTitle("Line"), 0, Layout.Bottom, 4);
layout_col2.add(line);
layout_col2.addSpacer(16);
layout_col2.add(createControlTitle("ListBox"), 0, Layout.Bottom, 4);
layout_col2.add(listbox);
layout_col2.addSpacer(16);
layout_col2.add(createControlTitle("ListBox2"), 0, Layout.Bottom, 4);
layout_col2.add(listbox2);
layout_col2.addSpacer(16);
layout_col2.add(createControlTitle("ProgressBar"), 0, Layout.Bottom, 4);
layout_col2.add(progressbar);
layout_col2.addSpacer(16);
layout_col2.add(createControlTitle("TextBox"), 0, Layout.Bottom, 4);
layout_col2.add(textbox);

// create main layout
var main_layout = new BoxLayout(Layout.Horizontal);
main_layout.addSpacer(16);
main_layout.add(layout_col1, 1, Layout.Expand);
main_layout.add(new Line(Line.Vertical), 0, Layout.Expand);
main_layout.addSpacer(16);
main_layout.add(layout_col2, 1, Layout.Expand);
main_layout.add(new Line(Line.Vertical), 0, Layout.Expand);
setLayout(main_layout);
}


function createControlTitle(title)
{
var title_label = new Label(title);
title_label.setFont(m_title_font);
return title_label;
}

//function onConnectToServer(sender, event_args, textbox)
function onButtonClicked()
{
//Connection
var db
db = new DbConnection("mssql://" + textbox.getText() + ":1434/STRATA");

if (db.isConnected()) alert("Connection succeeded");
else alert("Connection failed");

// return db ;

var result = db.execute("select name from master..sysDatabases where name not in('master', 'model', 'msdb', 'tempdb')");

var objects = db.getObjects();

// Populate the listbox with the Table names
for (var i = 0; i < objects.length; ++i)
{
if (objects[i].type != "TABLE")
continue;

//Console.writeLine(objects[i].name);
listbox.addItem(objects[i].name);
}

// Populate the listbox with all the DB names on the server
while (result.next())
{
//Console.writeLine(result.name);
listbox2.addItem(result.name);
}
// HostApp.refresh()
}
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby abenitez77 on Mon Jan 31, 2011 8:42 pm

Am I not able to use textbox.getText() from inside a function? the texbox was declared outside the function. I am getting this error msg:

Script Runtime Error: Term 'textbox.getText' does not evaluate to a function.
Compiler error (line 185): Undeclared identifier: 'textbox'
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby Aaron on Mon Jan 31, 2011 11:07 pm

Hi,

Your problem is that your textbox is defined inside another function and is therefore scoped to that function. When you reference it in your other function it's outside the scope of that function, and therefore is null.

If you want to reference that textbox in other function, define it as a member of the class and initialize it in the constructor.

Here's an example that shows how to do this.

Code: Select all

var f = new LayoutForm;

// open the form in a dialog
f.center();
f.show();

Application.run();

class LayoutForm extends Form
{
    var m_content_textbox;

    function LayoutForm()
    {
        super("Sample Form", 200, 200, 640, 480);
        setMinSize(640,480);
       
        // -- BEGIN LAYOUT TEMPLATE --
       
        // textbox (for content)
        m_content_textbox = new TextBox;
        m_content_textbox.setText("Example Text");
       
        // buttons
        var ok_button = new Button("OK");
        ok_button.click.connect(this, onOkClicked);
       
        var cancel_button = new Button("Cancel");
        cancel_button.click.connect(this, onCancelClicked);
       
        // button layout
        var button_layout = new BoxLayout(Layout.Horizontal);
        button_layout.addStretchSpacer();
        button_layout.add(ok_button);
        button_layout.addSpacer(8);
        button_layout.add(cancel_button);
       
        // main layout
        var main_layout = new BoxLayout(Layout.Vertical);
        main_layout.addSpacer(8);
        main_layout.add(m_content_textbox, 1, Layout.Expand | Layout.Left | Layout.Right, 8);
        main_layout.addSpacer(8);
        main_layout.add(button_layout, 0, Layout.Expand | Layout.Left | Layout.Right, 8);
        main_layout.addSpacer(8);
       
        // -- END LAYOUT TEMPLATE --
       
        // set the layout of this form
        setLayout(main_layout);
    }
   
    function onOkClicked()
    {
        alert(m_content_textbox.getText());
    }
   
    function onCancelClicked()
    {
        alert("Cancel Clicked");
    }
}


Best,
Aaron
User avatar
Aaron
Kirix Support Team
 
Posts: 120
Joined: Fri Dec 16, 2005 3:01 pm

Re: Connecting to db via script

Postby abenitez77 on Tue Feb 01, 2011 9:34 am

Makes sense... I knew that was the issue...I'm just new to java... thanks for your help. I have it working now.

I am populating both lists and I am using the value of listbox2 to populate the other listbox. listbox2 has multiple items and the user will be able to select multiple items (the items are db names). In listbox, I populate the list with the table names. Right now I can populate the tablenames in listbox if I select only 1 db name from listbox2, but I want to be able to select multiple db names fromm lisbox2 and populate listbox with all the tables of the selected items (db names) in listbox2. how do I do that?

Code:
use strict variables;
use strict semicolons;

//HostApp.refresh();

//var db_output = HostApp.getDatabase();

var f = new ControlsForm;
f.center();
f.show();

Application.run();

class ControlsForm extends Form
{
var m_title_font = new Font(Font.Default, 10, Font.Bold);
var textbox;
var listbox;
var listbox2;

function ControlsForm()
{
super("Controls Form", 0, 0, 800, 640);
setMinSize(600, 560);

// create all controls
var borderbox = new BorderBox("BorderBox");
var button = new Button(" ConnectToServer ");
var button2 = new Button(" Retrieve Table Names");
//button.click.connect(this, onConnectToServer);
var label = new Label("Label");
var line = new Line;
listbox = new ListBox;
listbox2 = new ListBox;
var picturebox = new PictureBox;
var progressbar = new ProgressBar;
textbox = new TextBox("");
button.click.connect(this, onButtonClicked);
button2.click.connect(this, onButton2Clicked);

Console.clear();

// var bmp = Bitmap.loadFile("\\usatl02eras40\Audit Strategy\ASTRATEGY\Projects\Kstrata\PRGX_Logo_small.jpg");
// var width = bmp.getWidth();
// var height = bmp.getHeight();

//super("Startup Picture", 1, 1, width-20, height);
//this.center();

// var picturebox = new picturebox(0, 0, width, height-70);
// picturebox.setImage(bmp);
// this.add(picturebox);

// create first column layout
var layout_col1 = new BoxLayout(Layout.Vertical);
layout_col1.addSpacer(16);
layout_col1.add(createControlTitle("PictureBox"), 0, Layout.Bottom, 4);
layout_col1.add(picturebox);
layout_col1.addSpacer(64);
layout_col1.add(createControlTitle("Retrieve DB Names"), 0, Layout.Bottom, 4);
layout_col1.add(button);
layout_col1.addSpacer(16);
layout_col1.add(createControlTitle("Retrieve Table Names"), 0, Layout.Bottom, 4);
layout_col1.add(button2);
layout_col1.addSpacer(16);
layout_col1.add(createControlTitle("Label"), 0, Layout.Bottom, 4);
layout_col1.add(label);

// create second column layout
var layout_col2 = new BoxLayout(Layout.Vertical);
layout_col2.addSpacer(16);
layout_col2.add(createControlTitle("Line"), 0, Layout.Bottom, 4);
layout_col2.add(line);
layout_col2.addSpacer(16);
layout_col2.add(createControlTitle("Databases"), 0, Layout.Bottom, 4);
layout_col2.add(listbox2);
layout_col2.addSpacer(16);
layout_col2.add(createControlTitle("Tables"), 0, Layout.Bottom, 4);
layout_col2.add(listbox);
layout_col2.addSpacer(16);
layout_col2.add(createControlTitle("ProgressBar"), 0, Layout.Bottom, 4);
layout_col2.add(progressbar);
layout_col2.addSpacer(16);
layout_col2.add(createControlTitle("Server Name"), 0, Layout.Bottom, 4);
layout_col2.add(textbox);

// create main layout
var main_layout = new BoxLayout(Layout.Horizontal);
main_layout.addSpacer(16);
main_layout.add(layout_col1, 1, Layout.Expand);
main_layout.add(new Line(Line.Vertical), 0, Layout.Expand);
main_layout.addSpacer(16);
main_layout.add(layout_col2, 1, Layout.Expand);
main_layout.add(new Line(Line.Vertical), 0, Layout.Expand);
setLayout(main_layout);
}


function createControlTitle(title)
{
var title_label = new Label(title);
title_label.setFont(m_title_font);
return title_label;
}

//function to connect to server and retrieve DB names
function onButtonClicked()
{
//Connection
var db
db = new DbConnection("mssql://" + textbox.getText() + ":1434/tempdb");

if (db.isConnected()) alert("Connection succeeded");
else alert("Connection failed");

var result = db.execute("select name from master..sysDatabases where name not in('master', 'model', 'msdb', 'tempdb')");

listbox2.clear();

// Populate the listbox with all the DB names on the server
while (result.next())
{
listbox2.addItem(result.name);
}
}

//function onConnectToServer(sender, event_args, textbox)
function onButton2Clicked()
{
//Connection
var db2

alert(listbox2.getSelectedItem());
db2 = new DbConnection("mssql://" + textbox.getText() + ":1434/" + listbox2.getSelectedItem());

if (db2.isConnected()) alert("Connection succeeded");
else alert("Connection failed");

var objects = db2.getObjects();
listbox.clear();

// Populate the listbox with the Table names
for (var i = 0; i < objects.length; ++i)
{
if (objects[i].type != "TABLE")
continue;

//Console.writeLine(objects[i].name);
listbox.addItem(objects[i].name);
}
}
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby abenitez77 on Tue Feb 01, 2011 10:55 am

Also,
How do i get my connection I created in the script to show up on under my Projects?
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby Ben on Thu Feb 03, 2011 7:14 am

In the project, you can create the connection you are using in your script by clicking File->Create Connection... Click on SQL Server at the top and fill out your server name and database.

Leave user name and password empty to use Trusted Connections and Windows authentication.

All the best,
Ben
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Re: Connecting to db via script

Postby Ben on Thu Feb 03, 2011 7:17 am

If you want your script to create the connection (mount) automatically, it would look something like this:

db->execute("CREATE MOUNT my_mount_name AS mssql://USATL02PRSQ70:1434/STRATA");
HostApp.refresh();
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Re: Connecting to db via script

Postby Ben on Thu Feb 03, 2011 7:25 am

To iterate through all selected items, you'd use a for loop with ListBox.isItemSelected(). Even better would be to use ListBox.getSelectedItems(), which returns all items currently selected. Right now you are using getSelectedIndex() which should only be used with single-selection list boxes.

Check out the ListBox docs here: http://www.kirix.com/docs/ListBox.html

Code would probably look something like this:

Code: Select all
var i, selected_items = lbox.getSelectedItems();
for (i in selected_items)
    this.populateForDatabase(selected_items[i]);
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Re: Connecting to db via script

Postby abenitez77 on Thu Feb 03, 2011 10:28 am

Thanks! this is a big help. And how to I widen the listboxes? The names are running past the bounderies and the scrollbars are only showing up for up and down. Thanks.
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby abenitez77 on Thu Feb 03, 2011 10:37 am

I also want to BOLD , underline and put in UPPERCASE the first item in the listbox. how do i do this?
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby Ben on Thu Feb 03, 2011 12:07 pm

To change the list box size, pass the desired control size in the constructor. "new ListView(1,1, 400, 300)". 400,300 will be the size.

As for styling individual list control items, I'm not sure that this is possible. You can highlight the item you want to bring to the users attention with ListBox.selectItem(). You can also make a string upper case by calling String.toUpperCase().


Code: Select all
s = s.toUpperCase()


Best,
Ben
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Re: Connecting to db via script

Postby Ben on Thu Feb 03, 2011 12:08 pm

In the control size code above, I meant "new ListBox(1,1,400,300);"

There is a ListView class as well, which supports columns. ListBox, which you are using right now, is the simpler of the two classes.

Ben
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Re: Connecting to db via script

Postby abenitez77 on Thu Feb 03, 2011 1:14 pm

Can they be dynamic? IF so, is it difficult/complex to setup?
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby Ben on Thu Feb 03, 2011 1:48 pm

It's possible, and not hard. You need to use the layout mechanism. Here's a link to the documentation: http://www.kirix.com/docs/BoxLayout.html

The layout code is usually in the Form constructor and looks something like this:

Code: Select all
var layout_row1 = new BoxLayout(Layout.Horizontal);
layout_row1.add(...);
layout_row1.addSpacer(16);
layout_row1.add(...);

// create main layout
var main_layout = new BoxLayout(Layout.Vertical);
main_layout.addSpacer(16);
main_layout.add(layout_row11, 1, Layout.Expand);
main_layout.addSpacer(16);

setLayout(main_layout);


You should replace the ...'s with your GUI controls.
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Re: Connecting to db via script

Postby abenitez77 on Thu Feb 03, 2011 3:41 pm

Ben wrote:If you want your script to create the connection (mount) automatically, it would look something like this:

db->execute("CREATE MOUNT my_mount_name AS mssql://USATL02PRSQ70:1434/STRATA");
HostApp.refresh();


I get an error "compiler error line 175):unexpected token"

code:
db3->execute("CREATE MOUNT " + selected_items[x] + " AS mssql://" + textbox.getText() + ":1434/" + textbox.getText() );
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: Connecting to db via script

Postby Ben on Thu Feb 03, 2011 4:05 pm

The error is probably on some other line. If you paste more, I'll help you hunt it down.
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am
Next

Return to Strata Help & Feedback