Advertisement
Guest User

Untitled

a guest
Jul 5th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 11.49 KB | None | 0 0
  1. Public Class frmAgent
  2.  
  3.     Private Sub frmAgent_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  4.         ClearFields()
  5.         BindGrid("", "")
  6.         SetGridStyleFillAll(dgvListing, 2)
  7.     End Sub
  8.  
  9.     Private Sub ClearFields()
  10.         txtMode.Text = ""
  11.         txtCode.Text = ""
  12.         txtDescription.Text = ""
  13.     End Sub
  14.  
  15.     Private Sub EnableFields(ByVal strMode As String)
  16.         Select Case strMode
  17.             Case "N"
  18.                 txtCode.Focus()
  19.                 txtCode.ReadOnly = False
  20.                 txtDescription.ReadOnly = False
  21.             Case "E"
  22.                 txtDescription.Focus()
  23.                 txtCode.ReadOnly = True
  24.                 txtDescription.ReadOnly = False
  25.             Case Else
  26.                 txtCode.Focus()
  27.                 txtCode.ReadOnly = True
  28.                 txtDescription.ReadOnly = True
  29.         End Select
  30.     End Sub
  31.  
  32.     Public Sub BindGrid(ByVal strCode As String, ByVal strCond As String)
  33.         Dim strSQL As String = ""
  34.  
  35.         Try
  36.             strSQL = "SELECT AGENT AS 'Agent', DESP AS 'Description' FROM AGENT (NOLOCK) WHERE AGENT <> ''"
  37.             If strCode <> "" Then
  38.                 strSQL = strSQL & " AND AGENT = " & CSql(strCode)
  39.             End If
  40.             If strCond <> "" Then
  41.                 strSQL = strSQL & " AND " & strCond
  42.             End If
  43.             strSQL = strSQL & " ORDER BY AGENT"
  44.  
  45.             BindDataGridView(strSQL, dgvListing)
  46.         Catch ex As Exception
  47.             InsertSysErrorLog(Me.Name, "BindGrid", ex.Message, strSQL)
  48.         End Try
  49.     End Sub
  50.  
  51.     Private Function ChkValid() As Boolean
  52.         Dim strMsg As String = ""
  53.         ChkValid = True
  54.  
  55.         If txtCode.Text.Trim = "" Then
  56.             strMsg = strMsg & "Required field - Agent." & vbCrLf
  57.         Else
  58.             If txtMode.Text = "N" Then
  59.                 If CheckIsExist("AGENT", "AGENT = " & CSql(txtCode.Text)) = True Then
  60.                     strMsg = strMsg & "Agent Duplicate." & vbCrLf
  61.                 End If
  62.             End If
  63.         End If
  64.  
  65.         If txtDescription.Text = "" Then
  66.             strMsg = strMsg & "Required field - Description." & vbCrLf
  67.         End If
  68.  
  69.         If strMsg <> "" Then
  70.             DisplayMessage(strMsg, "")
  71.             ChkValid = False
  72.         End If
  73.     End Function
  74.  
  75.     Private Sub gvListing_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvListing.CellDoubleClick
  76.         btnEdit_Click(sender, e)
  77.     End Sub
  78.  
  79.     Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
  80.         Me.Close()
  81.     End Sub
  82.  
  83.     Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
  84.         If txtDescription.ReadOnly = False Then
  85.             If ChkValid() = True Then
  86.                 If txtMode.Text = "N" Then
  87.                     If InsertRecord() = True Then
  88.                         DisplayMessage("RSS", txtCode.Text)
  89.                         ClearFields()
  90.                         BindGrid("", "")
  91.                         EnableFields(txtMode.Text)
  92.                     End If
  93.                 ElseIf txtMode.Text = "E" Then
  94.                     If UpdateRecord() = True Then
  95.                         DisplayMessage("RU", txtCode.Text)
  96.                         ClearFields()
  97.                         BindGrid("", "")
  98.                         EnableFields(txtMode.Text)
  99.                     End If
  100.                 End If
  101.             End If
  102.         End If
  103.     End Sub
  104.  
  105.     Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
  106.         ClearFields()
  107.         txtMode.Text = "N"
  108.         EnableFields(txtMode.Text)
  109.     End Sub
  110.  
  111.     Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
  112.         Try
  113.             If dgvListing.CurrentRow.Index > -1 Then
  114.                 ClearFields()
  115.                 txtMode.Text = "E"
  116.                 txtCode.Text = dgvListing.CurrentRow.Cells("Agent").Value
  117.                 txtDescription.Text = dgvListing.CurrentRow.Cells("Description").Value
  118.                 EnableFields(txtMode.Text)
  119.             Else
  120.                 DisplayMessage("PSRE", "")
  121.             End If
  122.         Catch ex As Exception
  123.             InsertSysErrorLog(Me.Name, "btnEdit_Click", ex.Message, "")
  124.         End Try
  125.     End Sub
  126.  
  127.     Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
  128.         Dim strCode As String = ""
  129.  
  130.         Try
  131.             strCode = dgvListing.CurrentRow.Cells("Agent").Value
  132.         Catch ex As Exception
  133.             InsertSysErrorLog(Me.Name, "btnDelete_Click", ex.Message, "")
  134.         End Try
  135.  
  136.         If strCode <> "" Then
  137.             If CheckIsExist("CUSTOMER", "AGENTCODE = " & CSql(strCode)) = True Or CheckIsExist("SUPPLIER", "AGENTCODE = " & CSql(strCode)) = True Then
  138.                 DisplayMessage("Agent in-used!", "")
  139.             Else
  140.                 If MsgBox("Confirm Delete Agent [" & strCode & "]?", vbCritical + vbYesNo, "Warning") = vbYes Then
  141.                     If DeleteRecord(strCode) = True Then
  142.                         DisplayMessage("RD", strCode)
  143.                         ClearFields()
  144.                         BindGrid("", "")
  145.                         EnableFields(txtMode.Text)
  146.                     End If
  147.                 End If
  148.             End If
  149.         End If
  150.     End Sub
  151.  
  152.     Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click
  153.         With frmPopUpSearch
  154.             .txtTable.Text = "AGENT"
  155.             .txtForm.Text = Me.Name
  156.             .BindSearchCriteria()
  157.             .BringToFront()
  158.             .Show()
  159.         End With
  160.     End Sub
  161.  
  162.     Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
  163.         BindGrid("", "")
  164.     End Sub
  165.  
  166.     Private Sub txtCode_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtCode.KeyPress
  167.         e.KeyChar = Chr(KeyAscCode(Asc(e.KeyChar)))
  168.     End Sub
  169.  
  170.     Private Sub txtDescription_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtDescription.KeyPress
  171.         e.KeyChar = Chr(KeyAscCode(Asc(e.KeyChar)))
  172.     End Sub
  173.  
  174.     Private Sub txtCode_KeyDown(sender As Object, e As KeyEventArgs) Handles txtCode.KeyDown
  175.         SetNextFocusManual(e, txtDescription)
  176.     End Sub
  177.  
  178.     Private Sub txtDescription_KeyDown(sender As Object, e As KeyEventArgs) Handles txtDescription.KeyDown
  179.         If e.KeyCode = Keys.F2 Or e.KeyCode = Keys.Enter Then
  180.             btnSave_Click(sender, e)
  181.         End If
  182.     End Sub
  183.  
  184.     Private Function InsertRecord() As Boolean
  185.         Dim sqlConn As New SqlClient.SqlConnection
  186.         Dim sqlTrans As SqlClient.SqlTransaction = Nothing
  187.         Dim sqlCMD As SqlClient.SqlCommand = Nothing
  188.         Dim strSQL As String = ""
  189.         Dim intErrorCode As Integer = 0
  190.  
  191.         InsertRecord = False
  192.  
  193.         Try
  194.             sqlConn.ConnectionString = GetConnectionString()
  195.             If sqlConn.State = Data.ConnectionState.Closed Then
  196.                 sqlConn.Open()
  197.             End If
  198.  
  199.             sqlTrans = sqlConn.BeginTransaction
  200.  
  201.             strSQL = "AGENT_SPINSERTNEW"
  202.             sqlCMD = New SqlClient.SqlCommand(strSQL, sqlConn, sqlTrans)
  203.             sqlCMD.CommandType = CommandType.StoredProcedure
  204.  
  205.             InsertWithVarchar(sqlCMD, "@MPROPERTY", txtCode.Text)
  206.             InsertWithVarchar(sqlCMD, "@DESP", txtDescription.Text)
  207.             InsertWithInteger(sqlCMD, "@ERRORCODE", intErrorCode)
  208.             sqlCMD.Parameters("@ERRORCODE").Direction = ParameterDirection.Output
  209.             sqlCMD.ExecuteNonQuery()
  210.             intErrorCode = sqlCMD.Parameters("@ERRORCODE").Value.ToString()
  211.  
  212.             sqlTrans.Commit()
  213.  
  214.             InsertRecord = True
  215.         Catch ex As Exception
  216.             sqlTrans.Rollback()
  217.             InsertSysErrorLog(Me.Name, "InsertRecord", ex.Message, strSQL)
  218.         Finally
  219.             If sqlConn.State = ConnectionState.Open Then
  220.                 sqlConn.Close()
  221.             End If
  222.  
  223.             If Not sqlTrans Is Nothing Then
  224.                 sqlTrans.Dispose()
  225.             End If
  226.  
  227.             If Not sqlCMD Is Nothing Then
  228.                 sqlCMD.Dispose()
  229.             End If
  230.         End Try
  231.     End Function
  232.  
  233.     Private Function UpdateRecord() As Boolean
  234.         Dim sqlConn As New SqlClient.SqlConnection
  235.         Dim sqlTrans As SqlClient.SqlTransaction = Nothing
  236.         Dim sqlCMD As SqlClient.SqlCommand = Nothing
  237.         Dim strSQL As String = ""
  238.         Dim intErrorCode As Integer = 0
  239.  
  240.         UpdateRecord = False
  241.  
  242.         Try
  243.             sqlConn.ConnectionString = GetConnectionString()
  244.             If sqlConn.State = Data.ConnectionState.Closed Then
  245.                 sqlConn.Open()
  246.             End If
  247.  
  248.             sqlTrans = sqlConn.BeginTransaction
  249.  
  250.             strSQL = "AGENT_SPUPDATEEDIT"
  251.             sqlCMD = New SqlClient.SqlCommand(strSQL, sqlConn, sqlTrans)
  252.             sqlCMD.CommandType = CommandType.StoredProcedure
  253.  
  254.             InsertWithVarchar(sqlCMD, "@MPROPERTY", txtCode.Text)
  255.             InsertWithVarchar(sqlCMD, "@DESP", txtDescription.Text)
  256.             InsertWithInteger(sqlCMD, "@ERRORCODE", intErrorCode)
  257.             sqlCMD.Parameters("@ERRORCODE").Direction = ParameterDirection.Output
  258.             sqlCMD.ExecuteNonQuery()
  259.             intErrorCode = sqlCMD.Parameters("@ERRORCODE").Value.ToString()
  260.  
  261.             sqlTrans.Commit()
  262.  
  263.             UpdateRecord = True
  264.         Catch ex As Exception
  265.             sqlTrans.Rollback()
  266.             InsertSysErrorLog(Me.Name, "UpdateRecord", ex.Message, strSQL)
  267.         Finally
  268.             If sqlConn.State = ConnectionState.Open Then
  269.                 sqlConn.Close()
  270.             End If
  271.  
  272.             If Not sqlTrans Is Nothing Then
  273.                 sqlTrans.Dispose()
  274.             End If
  275.  
  276.             If Not sqlCMD Is Nothing Then
  277.                 sqlCMD.Dispose()
  278.             End If
  279.         End Try
  280.     End Function
  281.  
  282.     Private Function DeleteRecord(ByVal strCode As String) As Boolean
  283.         Dim sqlConn As New SqlClient.SqlConnection
  284.         Dim sqlTrans As SqlClient.SqlTransaction = Nothing
  285.         Dim sqlCMD As SqlClient.SqlCommand = Nothing
  286.         Dim strSQL As String = ""
  287.         Dim intErrorCode As Integer = 0
  288.  
  289.         DeleteRecord = False
  290.  
  291.         Try
  292.             sqlConn.ConnectionString = GetConnectionString()
  293.             If sqlConn.State = Data.ConnectionState.Closed Then
  294.                 sqlConn.Open()
  295.             End If
  296.  
  297.             sqlTrans = sqlConn.BeginTransaction
  298.  
  299.             strSQL = "AGENT_SPDELETEAGENT"
  300.             sqlCMD = New SqlClient.SqlCommand(strSQL, sqlConn, sqlTrans)
  301.             sqlCMD.CommandType = CommandType.StoredProcedure
  302.  
  303.             InsertWithVarchar(sqlCMD, "@MPROPERTY", strCode)
  304.             InsertWithInteger(sqlCMD, "@ERRORCODE", intErrorCode)
  305.             sqlCMD.Parameters("@ERRORCODE").Direction = ParameterDirection.Output
  306.             sqlCMD.ExecuteNonQuery()
  307.             intErrorCode = sqlCMD.Parameters("@ERRORCODE").Value.ToString()
  308.  
  309.             sqlTrans.Commit()
  310.  
  311.             DeleteRecord = True
  312.         Catch ex As Exception
  313.             sqlTrans.Rollback()
  314.             InsertSysErrorLog(Me.Name, "DeleteRecord", ex.Message, strSQL)
  315.         Finally
  316.             If sqlConn.State = ConnectionState.Open Then
  317.                 sqlConn.Close()
  318.             End If
  319.  
  320.             If Not sqlTrans Is Nothing Then
  321.                 sqlTrans.Dispose()
  322.             End If
  323.  
  324.             If Not sqlCMD Is Nothing Then
  325.                 sqlCMD.Dispose()
  326.             End If
  327.         End Try
  328.     End Function
  329. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement