Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.66 KB | None | 0 0
  1.     Private Sub btnCIInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCIInsert.Click
  2.         If txtCIStudNo.Text = "" Or txtCILname.Text = "" Or txtCIFname.Text = "" Then
  3.             MsgBox("Please fill in the required fields.", MsgBoxStyle.Exclamation)
  4.  
  5.             'focus on the correct field
  6.             If txtCIStudNo.Text = "" Then
  7.                 txtCIStudNo.Focus()
  8.             ElseIf txtCIFname.Text = "" Then
  9.                 txtCIFname.Focus()
  10.             ElseIf txtCILname.Text = "" Then
  11.                 txtCILname.Focus()
  12.             End If
  13.         Else
  14.             Dim msg As MsgBoxResult = MsgBox("Are you sure you want to save?", MsgBoxStyle.YesNo) 'Prompt for saving
  15.             'Check user choice
  16.             If msg = MsgBoxResult.Yes Then ' If Yes
  17.                 'Check for valid student number
  18.                 If txtCIStudNo.TextLength = 7 Then
  19.                     Dim dteSession As String = dtpCIInsert.Value.Date
  20.                     Dim strLname As String = StrConv(txtCILname.Text, VbStrConv.ProperCase)
  21.                     Dim strFname As String = StrConv(txtCIFname.Text, VbStrConv.ProperCase)
  22.                     Dim intStudNo As Integer = Convert.ToInt32(txtCIStudNo.Text)
  23.  
  24.                     'Execute SQL statements
  25.                     Dim connCIcmd As New SqlCommand("SELECT Stud_Number FROM Student_Information WHERE Stud_Number = " _
  26.                                                     & intStudNo, connMain.Conn)
  27.                     Dim connCIdr As SqlDataReader = connCIcmd.ExecuteReader
  28.  
  29.                     'Check if student has record
  30.                     If connCIdr.HasRows = 0 Then
  31.                         MsgBox("The student doesn't exist in the database.", MsgBoxStyle.Exclamation)
  32.                     Else
  33.                         connCIcmd.CommandText = "SELECT Stud_Number, Coun_Date FROM Counseling WHERE Stud_Number = " _
  34.                                                 & intStudNo & " AND Coun_Date = '" & dteSession & "'"
  35.                         connCIdr.Close()
  36.                         Dim connCIdr2 As SqlDataReader = connCIcmd.ExecuteReader
  37.                         If connCIdr2.HasRows = 0 Then
  38.                             'Change SQL statement
  39.                             connCIcmd.CommandText = "INSERT INTO Counseling(Coun_Lname, Coun_Fname, Coun_Date, Stud_Number)" _
  40.                                                     & "VALUES('" & strLname & "','" & strFname & "','" & dteSession & "'," _
  41.                                                     & intStudNo & ")"
  42.                             connCIdr2.Close() 'Close reader
  43.                             connCIcmd.ExecuteNonQuery()
  44.  
  45.                             MsgBox("The database has been updated.", MsgBoxStyle.Information)
  46.  
  47.                             calCSCalendar.Dates.Clear()
  48.                             calCSCalendar_Populate()
  49.  
  50.                             'reset fields
  51.                             txtCIStudNo.Text = ""
  52.                             txtCILname.Text = ""
  53.                             txtCIFname.Text = ""
  54.                             txtCIStudNo.Focus()
  55.                         Else
  56.                             MsgBox("The counseling information already exist.", MsgBoxStyle.Exclamation)
  57.                         End If
  58.                     End If
  59.                 Else
  60.                     MsgBox("Invalid student number. Please try again.", MsgBoxStyle.Exclamation) 'Displat error msg
  61.  
  62.                     'Reset txtPIStudNo
  63.                     txtCIStudNo.Text = ""
  64.                     txtCIStudNo.Focus()
  65.                 End If
  66.             ElseIf msg = MsgBoxResult.No Then 'If No
  67.                 txtCIStudNo.Focus() 'set focus to txtPIStudNo
  68.             End If
  69.         End If
  70.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement