Solved

javascript question for inserting record in sql server

Posted on 2010-11-16
4
579 Views
Last Modified: 2012-05-10
I'm trying to use this code and I get an error  saying:

Error in connector "Database Writer" at Javascript:
Error on line 5: missing ; before statement


It is hard to tell here but line 5 is where I assign the var sql= statement.

the variables ${FirstName} are predifined variables in the MirthConnect application. Just know that they contain the info needed.



var dbConn = DatabaseConnectionFactory.createDatabaseConnection ('net.sourceforge.jtds.jdbc.Driver','jdbc:jtds:sqlserver://localhost:1433/Demographics','MirthUser','b9isnice');



if (MsgType == 'ADTA08')

{

  var sql="INSERT INTO Patient (PatientID,FirstName,MiddleName,LastName,Gender,SSN,HomePhone,HomeAddress1,City,State,Zip) values("+${PatientID}+",'"+${FirstName}+"','"+${MiddleName}+"','"+${LastName}+"','"+${Sex}+"','"+${myssn}+"','"+${HomePhone}+"','"+${Address1}+"','"+${City}+"','"+${State}+"','"+${Zip}+"')";



logger.error("Query is: " + sql);







var status=dbConn.executeUpdate(sql);







}





dbConn.close();

Open in new window

0
Comment
Question by:rutledgj
4 Comments
 
LVL 7

Expert Comment

by:celazkon
Comment Utility
Try to use simple quotes instead of double quotes.
I don't program in Javascript now for some time, but from what I remember, this has been always an issue

Hope it helps, good luck
0
 
LVL 16

Accepted Solution

by:
jmatix earned 500 total points
Comment Utility
Braces - {} are not valid in a javascript variable name. So if the code above is javascript then those variables need to be changed. Are you sure it is  ${FirstName} and not $FirstName?
0
 
LVL 29

Expert Comment

by:Badotz
Comment Utility
Are you doing this on the client? Have you given a thought to security?
0
 

Author Comment

by:rutledgj
Comment Utility
OK. So I changed it to use diff variables and it doesn't complain about line 5. Now it says the syntac of the db driver is incorrect.  My code now looks like this:

 
var dbConn = DatabaseConnectionFactory.createDatabaseConnection('net.sourceforge.jtds.jdbc.Driver','jdbc:jtds:sqlserver://host:port/dbname','','');

dbConn.close();var dbConn = DatabaseConnectionFactory.createDatabaseConnection ('net.sourceforge.jtds.jdbc.Driver','jdbc:jtds:sqlserver://localhost:1433/Demographics','MirthUser','b9isnice');

var fname = $('FirstName');
var mname = $('MiddleName');
var lname = $('LastName');
var patientid = $('PatientID');
var gender = $('Sex');
var ssn = $('myssn');
var phone = $('HomePhone');
var addr = $('Address1');
var city = $('City');
var st = $('State');
var zip = $('Zip');

if (MsgType == 'ADTA08')
{
 
var sql="INSERT INTO Patient (PatientID,FirstName,MiddleName,LastName,Gender,SSN,HomePhone,HomeAddress1,City,State,Zip) values("+patientid+",'"+fname+"','"+mname+"','"+lname+"','"+gender+"','"+ssn+"','"+phone+"','"+addr+"','"+city+"','"+st+"','"+zip+"')";

logger.error("Query is: " + sql);



var status=dbConn.executeUpdate(sql);



}


dbConn.close();

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now