Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

javascript question for inserting record in sql server

Posted on 2010-11-16
4
584 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
ID: 34146508
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
ID: 34146635
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
ID: 34146962
Are you doing this on the client? Have you given a thought to security?
0
 

Author Comment

by:rutledgj
ID: 34148700
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

856 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