LS to SQL - Can read, but not update?

Hi

I’ve used some code from the Designer help file (posted below) to be able to connect to an SQL server. I can easily read the table in, but if I try and update it, I get an error.

Has anyone seen this before - and since I’m completely new to doing this can anyone see any rookie mistakes??

Thanks in advance,

Paul.

Sub Initialize

On Error Goto trap

Dim ses As New Lcsession

 ' In an LEI Scripted Agent, use the following syntax instead:

 ' Dim session As New LCSession ("mySession")

Dim con As New Lcconnection("oledb")

con.provider = "SQLOLEDB" ' use SQL Data

con.server = "DEVTEST"

con.database = "Outbound"

con.metadata = "ProjectDetail"

con.userid = "----"

con.password = "----"

con.Connect

Dim confldlst As New Lcfieldlist, count As Long

Dim TRID As LCField, USERI As LCField



con.writeback = True

count = con.Select(Nothing, 1, confldlst)

’ Get a “handle” to each of the fields in the list returned by Selection.

Set TRID = confldlst.GetField(1)   ' locate field by position

Set USERI = confldlst.GetField(5)   ' locate field by position





While (con.Fetch (conFldLst) > 0)

	Print "TID=" & TRID.Text(0) & ", User ID=" & USERI.Text(0)

' If this dog's name is Tracy, change it to Tyler.

	If USERI.text(0) = "Admin2" Then

		USERI.text = "Admin1"

  ' Now write the change back to the database.

		Call con.Update(confldlst)

		Print "Changed User ID."

	End If

	Print "<br>"

Wend

con.Action(LCACTION_COMMIT)

con.Disconnect

Exit Sub

trap:

Dim stat$, errcode As Long, msg$

If ses.Status <> LCSUCCESS Then

	ses.Getstatus stat, errcode, msg

	If (ses.Status = LCFAIL_EXTERNAL)Then

		Print "ODBC message: " & msg & "  code #" & Cstr(errcode), 0, _

		"error number " & Err & " line " & Erl

	Else

		Print "Connector message: " & Text, 0, "error number " & _ 

		Err & " line " & Erl

	End If

Else

	Print Error, 0, "error number " & Err & " line " & Erl

End If

con.Disconnect

Exit Sub

End Sub

Subject: SQL error

Can you include the error in your post? Check log.nsf to see if the error is logged. Also, if you use a SQL editor and authenticate with the same credentials used in your script and try to insert the same row(s) to the database are you able to?

Subject: Error Information

Hi Ed

Thanks for the reply. The error is this:

TID=62, User ID=Admin4 ODBC message: ErrorRecord: HResult: 0x80040e21 Description: Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done. Source: Microsoft OLE DB Provider for SQL Server code #12325 0 error number 12325 line 30

The first part (TID=62, User ID=Admin4) is as expected, but the “Call con.Update(confldlst)” is the line that generates the error.

Being very new to SQL (but having some help from those who don’t know Domino) I’ve been able to check error logs (of which there seems to be no reference) and test the user account. There’s no mention of it in the Domino log, but I sort of expected that since it’s an SQL error.

Also - I get the same error when running the script on the Notes client and the Domino server.

Thanks again,

Paul.

Subject: Code Sample

Hi Paul, I’ve included a working code sample you can modify to use your credentials and tables.

==================================================

Option Public

Uselsx “*LSXODBC”

Sub Initialize

' Holy... Cow! exported 1600 contacts to MySQL in a few seconds

Dim con As New ODBCConnection

Dim qry As New ODBCQuery

Dim result As New ODBCResultSet

Dim workspace As New NotesUIWorkspace

Dim uidoc As NotesUIDocument

Set uidoc = workspace.CurrentDocument

Set qry.Connection = con

Set result.Query = qry

Set session = New NotesSession

Set db = session.CurrentDatabase

Set view = db.GetView("MySQLTest")

'connects to the database

con.ConnectTo("NOFTest")

'queries the table Contacts.

qry.SQL = "SELECT * FROM Contact"

'result is what temp stores the value and is pointing to the execute command.

result.Execute



Set doc = view.GetFirstDocument

While Not(doc Is Nothing)

'If the value is empty then get the next document else get the document that you are pointing at.

	' If result.IsValueNull(uidoc.CurrentField) Then

		' Set doc = view.GetNextDocument(doc)

'update the value and adds it to the row

	result.AddRow

	Call result.SetValue("First_Name",doc.FirstName(0))

	Call result.SetValue("Last_Name",doc.LastName(0))

	Call result.SetValue("Company_Name",doc.Company(0))

	result.UpdateRow

	Set doc = view.GetNextDocument(doc)

'Else

	'result.AddRow

	'Call result.SetValue("FirstName",doc.FirstName(0))

	'Call result.SetValue("LastName",doc.LastName(0))

	'Call result.SetValue("Company",doc.Company(0))

	'result.UpdateRow

	'Set doc = view.GetNextDocument(doc)

' End If

Wend

result.Close(DB_CLOSE)

con.Disconnect

End Sub

================================================

Subject: Sorted!

Hi Ed

Really appreciate your comments, although I managed to get it running a different way. Here’s the code if it helps anyone else. The main thing I seemed to notice was that even though the result of the “execute” statement is set to “nothing” it still couldn’t be part of the “while” process. As a result I copied the information into an array and processed that afterwards.

Sub Initialize()

On Error Goto handle_error

REM **************************************************

REM Define temporary variables

REM **************************************************

Dim vWebSession As New notessession

Dim vThisDatabase As notesdatabase



Dim vThisDocument As notesdocument

Dim newDoc As NotesDocument



Dim ses As New LCSession

Dim con As New LCConnection ("oledb")



Dim MyFldList As New LCFieldList

Dim MyFldList2 As New LCFieldList

Dim MyFld As LCField 

Dim MyFld2 As LCField 

Dim MyFld3 As LCField 

Dim MyFld4 As LCField 

Dim SQL As String

Dim MyResult As String



Dim count As Integer

Dim X As Integer

Dim Y As Integer

REM **************************************************

REM Initialize variables

REM **************************************************

Set vThisDatabase = vWebSession.CurrentDatabase

'Set vThisDocument = vWebSession.DocumentContext

ses.ClearStatus 



'Set connection details

With con

	.Provider = "SQLOLEDB" 

	.Server = "Server"

	.Database = "Database" 

	.Metadata = "TableName"

	.userid = "-----"

	.password = "-----"

End With



On Error Resume Next

con.Connect

con.writeback = True





If Err = False Then 

Else

	Print "Connection failed with error " & Err & ": " & Error

	Exit Sub

End If	

REM **************************************************

REM Do stuff here

REM **************************************************

SQL = "SELECT TransactionID, UserID, projectId, questionnaireId from ProjectDetail"

con.Execute sql,myfldlist



Set myfld = myfldlist.GetField(1)

Set myfld2 = myfldlist.GetField(2)

Set myfld3 = myfldlist.GetField(3)

Set myfld4 = myfldlist.GetField(4)



'Print Open of page

Print "Successfully connected to SQLServer."

Print "<table>"

Print "<tr>"

Print "<td>Row</td>"

Print "<td>Transaction ID</td>"

Print "<td>User ID</td>"

Print "<td>Project ID</td>"

Print "<td>Questionnaire ID</td>"

Print "<td>Notes UNID</td>"



'Check for empty list

If con.Fetch(myfldlist) = 0 Then

	Print "No Records Located" 

	Goto handle_error

End If



'Set initial Value of Y

Y = 0



'Process the list

While (con.Fetch(myfldlist) > 0)

	

	If myfld2.text(0) = "AdminA" Then

		

		Redim Preserve toUpdate(4, Y) 

		

		toUpdate(0,Y) = myfld.Text(0) 'Tr ID

		toUpdate(1,Y) = myfld2.Text(0) 'User ID

		toUpdate(2,Y) = myfld3.Text(0) 'Project ID

		toUpdate(3,Y) = myfld4.Text(0) 'Questionnaire ID

		toUpdate(4,Y) = Cstr(y+1)

		

		Y = Y+1

		

	End If

	

Wend



'Loop through all results located

For X = 0 To Y-1

	'Create a NotesDocument

	Set newDoc = vThisDatabase.CreateDocument

	

	'Populate It

	newDoc.transID = toUpdate(0,X)

	newDoc.userID = toUpdate(1,X)

	newDoc.projectID = toUpdate(2,X)

	newDoc.quID = toUpdate(3,X)

	

	'Save it

	Call newDoc.Save(True,True)

	

	'Write update back to SQL

	con.Execute  "UPDATE ProjectDetail SET NotesUNID='"+newDoc.UniversalID+"' WHERE transactionid="+toUpdate(0,X), Nothing

	

	'Print Result

	Print "<tr>"

	Print "<td>" & toUpdate(4,X) & "</td>"

	Print "<td>" & toUpdate(0,X) & "</td>"

	Print "<td>" & toUpdate(1,X) & "</td>"

	Print "<td>" & toUpdate(2,X) & "</td>"

	Print "<td>" & toUpdate(3,X) & "</td>"

	Print "<td>" & newDoc.UniversalID & "</td>"

	Print "</tr>"

Next



'Print Table Close

Print "</table>"



'Close session

con.Disconnect

Set myfldlist = Nothing

Set myfld = Nothing

Set con = Nothing

Set ses = Nothing



Exit Sub

REM **************************************************

REM Error Handler

REM **************************************************

handle_error:

Dim stat$, errcode As Long, msg$

If ses.Status <> LCSUCCESS Then

	ses.Getstatus stat, errcode, msg

	If (ses.Status = LCFAIL_EXTERNAL)Then

		Print "ODBC message: " & msg & "  code #" & Cstr(errcode), 0, _

		"error number " & Err & " line " & Erl

	Else

		Print "Connector message: " & Text, 0, "error number " & _ 

		Err & " line " & Erl

	End If

Else

	Print Error, 0, "error number " & Err & " line " & Erl

End If

Exit Sub

End Sub