Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
Dim strUpdateSelected, strReadOnlySelected As String
Dim strErrorMessage, strSql, strUpdateList, strReadOnlyList As String
Dim intIndex, intModuleType, intSelectedFunctionIndex As Integer
Dim drData As SqlDataReader
Dim clsPraxisDB As New AdminTools.prx.db_lib()
clsPraxisDB.DBConnectionString = ConfigurationSettings.AppSettings("ConnectionString")
clsPraxisDb.makeSQLConnection()
If Trim(txtMessage.Text) = "" Or Trim(txtDescription.Text) = "" Then
If Trim(txtMessage.Text) = "" And Trim(txtDescription.Text) = "" Then
yada yada yaa
End If
Else
intSelectedFunctionIndex = cboFunctions.SelectedItem.Value
' clsPraxisDB.DBConnectionString = Application.Contents("ConnectionString")
strErrorMessage = ""
For intIndex = 0 To chkLstUpdate.Items.Count - 1
If chkLstUpdate.Items(intIndex).Selected And chkLstReadOnly.Items(intIndex).Selected Then
If Trim(strErrorMessage) = "" Then
strErrorMessage = "The following position(s) had been selected as Update and Read only: " & chkLstUpdate.Items(intIndex).Text.ToString
Else
strErrorMessage = strErrorMessage & ", " & chkLstUpdate.Items(intIndex).Text.ToString
End If
End If
Next
If Trim(strErrorMessage) = "" Then
strSql = "SELECT Description FROM xaFunction WHERE (Description = '" & Replace(txtDescription.Text, "'", "''") & "')"
If intSelectedFunctionIndex <> 0 Then
strSql = strSql & " And (ModuleType <> " & intSelectedFunctionIndex & ")"
End If
'clsPraxisDB.makeSQLConnection()
clsPraxisDB.SQLStatement = strSql
drData = clsPraxisDB.getRecords
While drData.Read
strErrorMessage = "Function Description '" & txtDescription.Text & "' already exists"
End While
End If
If Trim(strErrorMessage) = "" Then
If cboFunctions.SelectedItem.Value.ToString = "0" Then
'New function
strSql = "INSERT INTO xaFunction (Description, ErrorMessage, DteUpd, UserID) VALUES " & _
"('" & Replace(txtDescription.Text, "'", "''") & "', '" & Replace(txtMessage.Text, "'", "''") & "',GetDate()," & Session("UserPK") & ");select @@IDENTITY AS ModuleType"
'Response.Write(strSql)
'Response.End()
clsPraxisDB.SQLStatement = strSql
intModuleType = clsPraxisDB.getScalarValue
' Response.End()
For intIndex = 0 To chkLstUpdate.Items.Count - 1
If chkLstUpdate.Items(intIndex).Selected Then
strSql = "INSERT INTO xaFuncPos (ModuleType, PositionCd, AccessType, DteUpd, UserId) VALUES " & _
"(" & intModuleType & ",'" & chkLstUpdate.Items(intIndex).Value.ToString & "','U',GetDate(), " & Session("UserPK") & ")"
clsPraxisDB.execNonQuery(strSql)
ElseIf chkLstReadOnly.Items(intIndex).Selected Then
strSql = "INSERT INTO xaFuncPos (ModuleType, PositionCd, AccessType, DteUpd, UserId) VALUES " & _
"(" & intModuleType & ",'" & chkLstReadOnly.Items(intIndex).Value.ToString & "','R',GetDate(), " & Session("UserPK") & ")"
clsPraxisDB.execNonQuery(strSql)
End If
Next
Call PopulateScreenValues()
Else
'intSelectedFunctionIndex = cboFunctions.SelectedItem.Value
'clsPraxisDB.makeSQLConnection()
strSql = "UPDATE xaFunction SET Description = '" & Replace(txtDescription.Text, "'", "''") & "', ErrorMessage = '" & _
Replace(txtMessage.Text, "'", "''") & "', DteUpd = GetDate() WHERE (ModuleType = " & intSelectedFunctionIndex & ")"
clsPraxisDB.execNonQuery(strSql)
strUpdateList = ""
strReadOnlyList = ""
'Get all the positions which got access to this function
strSql = "SELECT PositionCd, AccessType " & _
"FROM xaFunction LEFT JOIN xaFuncPos ON (xaFunction.ModuleType = xaFuncPos.ModuleType) " & _
"WHERE(xaFunction.ModuleType = " & intSelectedFunctionIndex & ") ORDER BY PositionCd"
' clsPraxisDB = New AdminTools.prx.db_lib()
' clsPraxisDB.DBConnectionString = Application.Contents("ConnectionString")
' clsPraxisDB.makeSQLConnection()
'Response.Write(strSql)
'Response.End()
clsPraxisDB.SQLStatement = strSql
drData = clsPraxisDB.getRecords
While drData.Read
If Not IsDBNull(drData("PositionCd")) Then
If drData("AccessType") = "U" Then
strUpdateList = strUpdateList & ", '" & drData("PositionCd") & "'"
Else
strReadOnlyList = strReadOnlyList & ", '" & drData("PositionCd") & "'"
End If
End If
End While
For intIndex = 0 To chkLstUpdate.Items.Count - 1
If chkLstUpdate.Items(intIndex).Selected Then
If InStr(strUpdateList, "'" & chkLstUpdate.Items(intIndex).Value & "'") > 0 Then
'do nothing
ElseIf InStr(strReadOnlyList, "'" & chkLstReadOnly.Items(intIndex).Value & "'") > 0 Then
'Update the access type
strSql = "UPDATE xaFuncPos SET AccessType = 'U', DteUpd = GetDate() WHERE (ModuleType = " & intSelectedFunctionIndex & _
") AND (PositionCd = '" & chkLstReadOnly.Items(intIndex).Value & "')"
clsPraxisDB.execNonQuery(strSql)
Else
'Insert record which had just been selected
strSql = "INSERT INTO xaFuncPos (ModuleType, PositionCd, AccessType, DteUpd, UserId) VALUES " & _
"(" & intSelectedFunctionIndex & ",'" & chkLstUpdate.Items(intIndex).Value.ToString & "','U',getdate(), " & Session("UserPK") & ")"
clsPraxisDB.execNonQuery(strSql)
'Response.Write(strSql)
'Response.End()
End If
ElseIf chkLstReadOnly.Items(intIndex).Selected Then
If InStr(strUpdateList, "'" & chkLstUpdate.Items(intIndex).Value & "'") > 0 Then
'Update the access type
strSql = "UPDATE xaFuncPos SET AccessType = 'R', DteUpd = GetDate() WHERE (ModuleType = " & intSelectedFunctionIndex & _
") AND (PositionCd = '" & chkLstUpdate.Items(intIndex).Value & "')"
clsPraxisDB.execNonQuery(strSql)
ElseIf InStr(strReadOnlyList, "'" & chkLstReadOnly.Items(intIndex).Value & "'") > 0 Then
'Do nothing
Else
'Insert record which had just been selected
strSql = "INSERT INTO xaFuncPos (ModuleType, PositionCd, AccessType, DteUpd, UserId) VALUES " & _
"(" & intSelectedFunctionIndex & ",'" & chkLstReadOnly.Items(intIndex).Value.ToString & "','R',GetDate(), " & Session("UserPK") & ")"
clsPraxisDB.execNonQuery(strSql)
End If
ElseIf (InStr(strUpdateList, "'" & chkLstUpdate.Items(intIndex).Value & "'") > 0) Or _
(InStr(strReadOnlyList, "'" & chkLstReadOnly.Items(intIndex).Value & "'") > 0) Then
'Delete access for that position to the selected function
strSql = "DELETE FROM xaFuncPos WHERE (ModuleType = " & intSelectedFunctionIndex & _
") AND (PositionCd = '" & chkLstReadOnly.Items(intIndex).Value & "')"
clsPraxisDB.execNonQuery(strSql)
End If
Next
'Close connection here before calling a new function
clsPraxisDB.closeSQLConnection()
Call PopulateScreenValues(intSelectedFunctionIndex)
End If
Else
Response.Write("<b>" & strErrorMessage)
End If
End If
clsPraxisDB = Nothing
End Sub