Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class frmAgent
- Private Sub frmAgent_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- ClearFields()
- BindGrid("", "")
- SetGridStyleFillAll(dgvListing, 2)
- End Sub
- Private Sub ClearFields()
- txtMode.Text = ""
- txtCode.Text = ""
- txtDescription.Text = ""
- End Sub
- Private Sub EnableFields(ByVal strMode As String)
- Select Case strMode
- Case "N"
- txtCode.Focus()
- txtCode.ReadOnly = False
- txtDescription.ReadOnly = False
- Case "E"
- txtDescription.Focus()
- txtCode.ReadOnly = True
- txtDescription.ReadOnly = False
- Case Else
- txtCode.Focus()
- txtCode.ReadOnly = True
- txtDescription.ReadOnly = True
- End Select
- End Sub
- Public Sub BindGrid(ByVal strCode As String, ByVal strCond As String)
- Dim strSQL As String = ""
- Try
- strSQL = "SELECT AGENT AS 'Agent', DESP AS 'Description' FROM AGENT (NOLOCK) WHERE AGENT <> ''"
- If strCode <> "" Then
- strSQL = strSQL & " AND AGENT = " & CSql(strCode)
- End If
- If strCond <> "" Then
- strSQL = strSQL & " AND " & strCond
- End If
- strSQL = strSQL & " ORDER BY AGENT"
- BindDataGridView(strSQL, dgvListing)
- Catch ex As Exception
- InsertSysErrorLog(Me.Name, "BindGrid", ex.Message, strSQL)
- End Try
- End Sub
- Private Function ChkValid() As Boolean
- Dim strMsg As String = ""
- ChkValid = True
- If txtCode.Text.Trim = "" Then
- strMsg = strMsg & "Required field - Agent." & vbCrLf
- Else
- If txtMode.Text = "N" Then
- If CheckIsExist("AGENT", "AGENT = " & CSql(txtCode.Text)) = True Then
- strMsg = strMsg & "Agent Duplicate." & vbCrLf
- End If
- End If
- End If
- If txtDescription.Text = "" Then
- strMsg = strMsg & "Required field - Description." & vbCrLf
- End If
- If strMsg <> "" Then
- DisplayMessage(strMsg, "")
- ChkValid = False
- End If
- End Function
- Private Sub gvListing_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvListing.CellDoubleClick
- btnEdit_Click(sender, e)
- End Sub
- Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
- Me.Close()
- End Sub
- Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
- If txtDescription.ReadOnly = False Then
- If ChkValid() = True Then
- If txtMode.Text = "N" Then
- If InsertRecord() = True Then
- DisplayMessage("RSS", txtCode.Text)
- ClearFields()
- BindGrid("", "")
- EnableFields(txtMode.Text)
- End If
- ElseIf txtMode.Text = "E" Then
- If UpdateRecord() = True Then
- DisplayMessage("RU", txtCode.Text)
- ClearFields()
- BindGrid("", "")
- EnableFields(txtMode.Text)
- End If
- End If
- End If
- End If
- End Sub
- Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
- ClearFields()
- txtMode.Text = "N"
- EnableFields(txtMode.Text)
- End Sub
- Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
- Try
- If dgvListing.CurrentRow.Index > -1 Then
- ClearFields()
- txtMode.Text = "E"
- txtCode.Text = dgvListing.CurrentRow.Cells("Agent").Value
- txtDescription.Text = dgvListing.CurrentRow.Cells("Description").Value
- EnableFields(txtMode.Text)
- Else
- DisplayMessage("PSRE", "")
- End If
- Catch ex As Exception
- InsertSysErrorLog(Me.Name, "btnEdit_Click", ex.Message, "")
- End Try
- End Sub
- Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
- Dim strCode As String = ""
- Try
- strCode = dgvListing.CurrentRow.Cells("Agent").Value
- Catch ex As Exception
- InsertSysErrorLog(Me.Name, "btnDelete_Click", ex.Message, "")
- End Try
- If strCode <> "" Then
- If CheckIsExist("CUSTOMER", "AGENTCODE = " & CSql(strCode)) = True Or CheckIsExist("SUPPLIER", "AGENTCODE = " & CSql(strCode)) = True Then
- DisplayMessage("Agent in-used!", "")
- Else
- If MsgBox("Confirm Delete Agent [" & strCode & "]?", vbCritical + vbYesNo, "Warning") = vbYes Then
- If DeleteRecord(strCode) = True Then
- DisplayMessage("RD", strCode)
- ClearFields()
- BindGrid("", "")
- EnableFields(txtMode.Text)
- End If
- End If
- End If
- End If
- End Sub
- Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click
- With frmPopUpSearch
- .txtTable.Text = "AGENT"
- .txtForm.Text = Me.Name
- .BindSearchCriteria()
- .BringToFront()
- .Show()
- End With
- End Sub
- Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
- BindGrid("", "")
- End Sub
- Private Sub txtCode_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtCode.KeyPress
- e.KeyChar = Chr(KeyAscCode(Asc(e.KeyChar)))
- End Sub
- Private Sub txtDescription_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtDescription.KeyPress
- e.KeyChar = Chr(KeyAscCode(Asc(e.KeyChar)))
- End Sub
- Private Sub txtCode_KeyDown(sender As Object, e As KeyEventArgs) Handles txtCode.KeyDown
- SetNextFocusManual(e, txtDescription)
- End Sub
- Private Sub txtDescription_KeyDown(sender As Object, e As KeyEventArgs) Handles txtDescription.KeyDown
- If e.KeyCode = Keys.F2 Or e.KeyCode = Keys.Enter Then
- btnSave_Click(sender, e)
- End If
- End Sub
- Private Function InsertRecord() As Boolean
- Dim sqlConn As New SqlClient.SqlConnection
- Dim sqlTrans As SqlClient.SqlTransaction = Nothing
- Dim sqlCMD As SqlClient.SqlCommand = Nothing
- Dim strSQL As String = ""
- Dim intErrorCode As Integer = 0
- InsertRecord = False
- Try
- sqlConn.ConnectionString = GetConnectionString()
- If sqlConn.State = Data.ConnectionState.Closed Then
- sqlConn.Open()
- End If
- sqlTrans = sqlConn.BeginTransaction
- strSQL = "AGENT_SPINSERTNEW"
- sqlCMD = New SqlClient.SqlCommand(strSQL, sqlConn, sqlTrans)
- sqlCMD.CommandType = CommandType.StoredProcedure
- InsertWithVarchar(sqlCMD, "@MPROPERTY", txtCode.Text)
- InsertWithVarchar(sqlCMD, "@DESP", txtDescription.Text)
- InsertWithInteger(sqlCMD, "@ERRORCODE", intErrorCode)
- sqlCMD.Parameters("@ERRORCODE").Direction = ParameterDirection.Output
- sqlCMD.ExecuteNonQuery()
- intErrorCode = sqlCMD.Parameters("@ERRORCODE").Value.ToString()
- sqlTrans.Commit()
- InsertRecord = True
- Catch ex As Exception
- sqlTrans.Rollback()
- InsertSysErrorLog(Me.Name, "InsertRecord", ex.Message, strSQL)
- Finally
- If sqlConn.State = ConnectionState.Open Then
- sqlConn.Close()
- End If
- If Not sqlTrans Is Nothing Then
- sqlTrans.Dispose()
- End If
- If Not sqlCMD Is Nothing Then
- sqlCMD.Dispose()
- End If
- End Try
- End Function
- Private Function UpdateRecord() As Boolean
- Dim sqlConn As New SqlClient.SqlConnection
- Dim sqlTrans As SqlClient.SqlTransaction = Nothing
- Dim sqlCMD As SqlClient.SqlCommand = Nothing
- Dim strSQL As String = ""
- Dim intErrorCode As Integer = 0
- UpdateRecord = False
- Try
- sqlConn.ConnectionString = GetConnectionString()
- If sqlConn.State = Data.ConnectionState.Closed Then
- sqlConn.Open()
- End If
- sqlTrans = sqlConn.BeginTransaction
- strSQL = "AGENT_SPUPDATEEDIT"
- sqlCMD = New SqlClient.SqlCommand(strSQL, sqlConn, sqlTrans)
- sqlCMD.CommandType = CommandType.StoredProcedure
- InsertWithVarchar(sqlCMD, "@MPROPERTY", txtCode.Text)
- InsertWithVarchar(sqlCMD, "@DESP", txtDescription.Text)
- InsertWithInteger(sqlCMD, "@ERRORCODE", intErrorCode)
- sqlCMD.Parameters("@ERRORCODE").Direction = ParameterDirection.Output
- sqlCMD.ExecuteNonQuery()
- intErrorCode = sqlCMD.Parameters("@ERRORCODE").Value.ToString()
- sqlTrans.Commit()
- UpdateRecord = True
- Catch ex As Exception
- sqlTrans.Rollback()
- InsertSysErrorLog(Me.Name, "UpdateRecord", ex.Message, strSQL)
- Finally
- If sqlConn.State = ConnectionState.Open Then
- sqlConn.Close()
- End If
- If Not sqlTrans Is Nothing Then
- sqlTrans.Dispose()
- End If
- If Not sqlCMD Is Nothing Then
- sqlCMD.Dispose()
- End If
- End Try
- End Function
- Private Function DeleteRecord(ByVal strCode As String) As Boolean
- Dim sqlConn As New SqlClient.SqlConnection
- Dim sqlTrans As SqlClient.SqlTransaction = Nothing
- Dim sqlCMD As SqlClient.SqlCommand = Nothing
- Dim strSQL As String = ""
- Dim intErrorCode As Integer = 0
- DeleteRecord = False
- Try
- sqlConn.ConnectionString = GetConnectionString()
- If sqlConn.State = Data.ConnectionState.Closed Then
- sqlConn.Open()
- End If
- sqlTrans = sqlConn.BeginTransaction
- strSQL = "AGENT_SPDELETEAGENT"
- sqlCMD = New SqlClient.SqlCommand(strSQL, sqlConn, sqlTrans)
- sqlCMD.CommandType = CommandType.StoredProcedure
- InsertWithVarchar(sqlCMD, "@MPROPERTY", strCode)
- InsertWithInteger(sqlCMD, "@ERRORCODE", intErrorCode)
- sqlCMD.Parameters("@ERRORCODE").Direction = ParameterDirection.Output
- sqlCMD.ExecuteNonQuery()
- intErrorCode = sqlCMD.Parameters("@ERRORCODE").Value.ToString()
- sqlTrans.Commit()
- DeleteRecord = True
- Catch ex As Exception
- sqlTrans.Rollback()
- InsertSysErrorLog(Me.Name, "DeleteRecord", ex.Message, strSQL)
- Finally
- If sqlConn.State = ConnectionState.Open Then
- sqlConn.Close()
- End If
- If Not sqlTrans Is Nothing Then
- sqlTrans.Dispose()
- End If
- If Not sqlCMD Is Nothing Then
- sqlCMD.Dispose()
- End If
- End Try
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement