Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.64 KB | None | 0 0
  1.     Private Sub btnPISubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPISubmit.Click
  2.         'check if txtbox is empty
  3.         If Not (txtPIStudNo.Text = "" Or txtPILname.Text = "" Or txtPIFname.Text = "" Or txtPIMname.Text = "" Or cboPSCourse.SelectedItem = "") Then
  4.             Dim msg As MsgBoxResult = MsgBox("Are you sure you want to save?", MsgBoxStyle.YesNo) 'Prompt for saving
  5.             'Check user choice
  6.             If msg = MsgBoxResult.Yes Then ' If Yes
  7.                 'Check for valid student number
  8.                 If txtPIStudNo.TextLength = 7 Then
  9.                     Dim strLname As String = StrConv(txtPILname.Text, VbStrConv.ProperCase)
  10.                     Dim strFname As String = StrConv(txtPIFname.Text, VbStrConv.ProperCase)
  11.                     Dim strMname As String = StrConv(txtPIMname.Text, VbStrConv.ProperCase)
  12.  
  13.                     'Execute SQL statements
  14.                     Dim connPIcmd As New SqlCommand("SELECT s.Stud_Number, p.Prob_Type, p.School_Year FROM Student_Information s" & _
  15.                                                     " INNER JOIN Academic_Probation p ON s.Stud_Number = p.Stud_Number" _
  16.                                                     & " WHERE s.Stud_Number = " & Convert.ToInt32(txtPIStudNo.Text) & _
  17.                                                     "ORDER BY p.Prob_Type DESC", connMain.Conn)
  18.                     Dim connPIdr As SqlDataReader = connPIcmd.ExecuteReader()
  19.  
  20.                     'Check if student has record
  21.                     If connPIdr.HasRows = 0 Then
  22.                         'Change SQL statement
  23.                         connPIcmd.CommandText = "INSERT INTO Student_Information(Stud_Number, Stud_Fname," _
  24.                                                 & "Stud_Lname, Stud_Mname, Stud_Course, Stud_YearLevel) VALUES (" _
  25.                                                 & Convert.ToInt32(txtPIStudNo.Text) & ",'" & strLname & _
  26.                                                 "','" & strFname & "','" & strMname & "','" & _
  27.                                                 cboPICourse.SelectedItem & "','" & cboPIYearLvl.Text & "')"
  28.                         connPIdr.Close() 'Close reader
  29.                         connPIcmd.ExecuteNonQuery()
  30.  
  31.                         Dim strProbLvl As String = ""
  32.                         Dim intProbIndex As Integer = cboPIProbLvl.SelectedIndex
  33.  
  34.                         If intProbIndex = 0 Then
  35.                             strProbLvl = "FAP"
  36.                         ElseIf intProbIndex = 1 Then
  37.                             strProbLvl = "SAP"
  38.                         Else
  39.                             strProbLvl = "TAP"
  40.                         End If
  41.  
  42.                         'Change SQL statement
  43.                         connPIcmd.CommandText = "INSERT INTO Academic_Probation(Prob_Type, School_Year, Prob_Reason, Stud_Number) VALUES ('" _
  44.                                                 & strProbLvl & "','SY" & cboPISY.Text & cboPISem.Text & " Sem" & _
  45.                                                 "','" & txtPIReason.Text & "'," & Convert.ToInt32(txtPIStudNo.Text) & ")"
  46.                         connPIdr.Close()
  47.                         connPIcmd.ExecuteNonQuery()
  48.  
  49.                         MsgBox("The database has been updated.", MsgBoxStyle.Information)   'Display info msg
  50.                     Else
  51.                         If connPIdr.Read Then
  52.                             'Check Probation Level
  53.                             If (connPIdr.GetString(1) = "FAP" And cboPIProbLvl.SelectedIndex = 0) Or (connPIdr.GetString(1) = "SAP" And cboPIProbLvl.SelectedIndex = 0) _
  54.                                 Or (connPIdr.GetString(1) = "TAP" And cboPIProbLvl.SelectedIndex = 0) Then
  55.                                 MsgBox("The probation information is already in the database. Please try again.", MsgBoxStyle.Exclamation)
  56.                             Else
  57.                                 Dim strProbLvl As String = ""
  58.                                 Dim intProbIndex As Integer = cboPIProbLvl.SelectedIndex
  59.  
  60.                                 If intProbIndex = 0 Then
  61.                                     strProbLvl = "FAP"
  62.                                 ElseIf intProbIndex = 1 Then
  63.                                     strProbLvl = "SAP"
  64.                                 Else
  65.                                     strProbLvl = "TAP"
  66.                                 End If
  67.  
  68.                                 'Change SQL statement
  69.                                 connPIcmd.CommandText = "INSERT INTO Academic_Probation(Prob_Type, School_Year, Prob_Reason, Stud_Number) VALUES ('" _
  70.                                                         & strProbLvl & "','SY" & cboPISY.Text & cboPISem.Text & "Sem" & _
  71.                                                         "','" & txtPIReason.Text & "'," & Convert.ToInt32(txtPIStudNo.Text) & ")"
  72.                                 connPIdr.Close()
  73.                                 connPIcmd.ExecuteNonQuery()
  74.  
  75.                                 MsgBox("The database has been updated.", MsgBoxStyle.Information)   'Display info msg
  76.                             End If
  77.                         End If
  78.  
  79.                         connPIdr.Close() 'Close reader
  80.                     End If
  81.  
  82.                     'Reset Insert GUI
  83.                     txtPIStudNo.Text = ""
  84.                     txtPILname.Text = ""
  85.                     txtPIFname.Text = ""
  86.                     txtPIMname.Text = ""
  87.                     cboPSCourse.SelectedItem = ""
  88.                     txtPIReason.Text = ""
  89.                     txtPIStudNo.Focus()
  90.  
  91.                     cboPISY.SelectedIndex = 0
  92.                     cboPISem.SelectedIndex = 0
  93.                     cboPIYearLvl.SelectedIndex = 0
  94.                     cboPIProbLvl.SelectedIndex = 0
  95.                 Else
  96.                     MsgBox("Invalid student number. Please try again.", MsgBoxStyle.Exclamation) 'Displat error msg
  97.  
  98.                     'Reset txtPIStudNo
  99.                     txtPIStudNo.Text = ""
  100.                     txtPIStudNo.Focus()
  101.                 End If
  102.             ElseIf msg = MsgBoxResult.No Then 'If No
  103.                 txtPIStudNo.Focus() 'set focus to txtPIStudNo
  104.             End If
  105.         Else
  106.             MsgBox("Please fill in the required fields.", MsgBoxStyle.Exclamation)  'display error msg
  107.             'check which txtbox to set focus
  108.             If txtPIStudNo.Text = "" Then
  109.                 txtPIStudNo.Focus()
  110.             ElseIf txtPILname.Text = "" Then
  111.                 txtPILname.Focus()
  112.             ElseIf txtPIFname.Text = "" Then
  113.                 txtPIFname.Focus()
  114.             ElseIf txtPIMname.Text = "" Then
  115.                 txtPIMname.Focus()
  116.             ElseIf cboPSCourse.SelectedItem = "" Then
  117.                 cboPSCourse.Focus()
  118.             End If
  119.         End If
  120.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement