So in seeing a few posts on U2UG.org website about uniobjects and seeing that they don't appear to be interested in creating a web or uniobject specific forum, I decided to share what I know on how to handle data using UniObjects. Below I show opening and reading a file using the LIST command, writing to a unifile and running a subroutine.
My opinion of uniobjects is that it is very fast for web sites. Check out www.keepsakequilting.com. This is a website I labored long and hard on for about a year. There was definitely a learning curve which got me more than frustrated, as you may be able to see from my other posts here. But now that I more or less know the ins and outs of uniobjects and even know a little about universe (i can create dictionary items!) I really do like the technology.
First and foremost is the opening of the session. I use a public shared function so I don't have to keep specifying the connection items.
Public Shared Function UOOpenSession() As UniSession
Dim us As UniSession = Nothing
us = UniObjects.OpenSession(UVServerIP, UVUserID, UVPassword, UVAccount) 'These variables are from the web.config file
Return us
End Function
and of course, you need to close every time you open.
Public Shared Function UOCloseSession(ByRef unisess As UniSession)
UniObjects.CloseSession(unisess)
End Function
Do not use the Finally in a Try statement to close a session. The biggest issue we had was that an existing connection errored on the UV side and caused the session in my script to basically lose it's mind so the Catch fired on the Try statement and the Finally never fired. I've researched this issue and from what I can see it shouldn't be happening, so to stay on the safe side, I put the close right after I'm done with the session or uniobject and is the first line in the Catch. This makes everyone happy.
Opening and reading a unifile is rather easy however not too recommended. In troubleshooting many issues with uniobjects I've come to learn that instead of opening a unifile using the LIST command on the unifile to extract your data for a read is easier and appears to work much better. However, you do need to open a file directly to write to it. I don't have an example of actually opening a file for the purpose of reading it, but it is similar to the Write method below.
Dim uniSession As UniSession = Nothing
Try
uniSession = UOOpenSession()
Dim unicom As UniCommand = uniSession.CreateUniCommand
Dim subtext As String = ""
Dim qry As String = "LIST PARTS.WEB " & strPartNo & " DESC ... more ... IMAGE TOXML ELEMENTS"
unicom.Command = qry
unicom.Execute()
subtext = Mid(unicom.Response, InStr(unicom.Response, "<"), Len(unicom.Response))
unicom.Dispose()
UOCloseSession(uniSession) 'Close the session as soon as we're done with it
Dim xmlSR As System.IO.StringReader = New System.IO.StringReader(subtext)
Dim ds As New DataSet
ds.ReadXml(xmlSR, XmlReadMode.Auto)
' Now do what you want with the Dataset of data
Catch ex As Exception
UOCloseSession(uniSession)
Return False
End Try
Opening and writing a file is rather simple. I've found using the field numbers for specifying your fields while reading and writing is a guarantee to work. Using the names in the dictionary does not always work. I don't know why, may have something to do with how the dict items are setup? I don't know but it works this way.
Dim uniSession As UniSession = Nothing
Try
uniSession = UOOpenSession()
Dim uf As UniFile = uniSession.CreateUniFile("FILENAME")
With uf
.RecordID = RecID
.WriteField(7, Value1)
.WriteField(8, value2)
.Close()
End With
return true
Catch ex As Exception
UOCloseSession(uniSession)
Return False
End Try
And following that, reading a file is just as simple but use the
Running a subroutine is rather easy as well. Simply connect to it, send some arguments and get some back.
Dim uniSession As UniSession = Nothing
Try
uniSession = UOOpenSession
Dim uniSub As UniSubroutine = uniSession.CreateUniSubroutine("SUBR", 7) 'the number is the total number of arguments the routine has
uniSub.SetArg(0, CompanyCode) 'specifies the first value in the list of arguements. routines specify arguments using a zero-based index.
uniSub.Call()
If uniSub.GetArg(1) = "1" Then 'Important to have some error handling from the routine to gracefully handle the visitors request
Throw New Exception("Subroutine returned error: " & uniSub.GetArg(2)) 'Grabs return value in position 2, zero based index
End If
Dim uniDyn As New UniDynArray(uniSession, uniSub.GetArg(3)) 'Grabs return value in position 3, zero based index
' Now grab any other return values and do your magic
Return True
Catch ex As Exception
UOCloseSession(uniSession)
Return False
End Try