Imports System.Data.SqlClient

Public Class frmBorrow
    Public BorrowID As Integer

    Dim State As FormState
    Dim conn As New SqlClient.SqlConnection(My.Settings.LibSysConnectionString)

    Public WriteOnly Property getFormState()
        Set(ByVal value)
            State = value
        End Set
    End Property

    Private Sub BorrowBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BorrowBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.BorrowBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.BorrowDataSet)

        MessageBox.Show("Record successfully saved.", "Saving", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub frmBorrow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MembersTableAdapter.Fill(Me.MembersDataSet.Members)
        Me.BooksTableAdapter.Fill(Me.BooksDataSet.Books)
        Me.BorrowDetailsTableAdapter.Fill(Me.BorrowDataSet.BorrowDetails)
        Me.BorrowTableAdapter.Fill(Me.BorrowDataSet.Borrow)

        If State = FormState.adStateAddMode Then
            BindingNavigatorAddNewItem.PerformClick()
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        BorrowBindingNavigatorSaveItem_Click(sender, e)
    End Sub

    Private Sub txtSearchMemberID_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSearchMemberID.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            Try
                If Me.txtSearchMemberID.Text = "" Then Exit Sub

                conn.Open()

                Dim sqlQRY As String = "SELECT * FROM Members WHERE MemberID = " & Me.txtSearchMemberID.Text
                Dim cmd As SqlCommand = New SqlCommand(sqlQRY, conn)
                Dim rdr As SqlDataReader = cmd.ExecuteReader

                If rdr.HasRows Then
                    rdr.Read()

                    Me.MemberIDComboBox.SelectedValue = rdr("MemberID").ToString
                Else
                    Me.MemberIDComboBox.SelectedValue = 0
                End If

                SendKeys.Send("{Tab}")
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error!!!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Finally
                conn.Close()
            End Try
        End If
    End Sub

    Private Sub txtSearchAccNo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSearchAccNo.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            Try
                If Me.txtSearchAccNo.Text = "" Then Exit Sub

                conn.Open()

                Dim sqlQRY As String = "SELECT AccessionNo FROM Books WHERE AccessionNo = " & Me.txtSearchAccNo.Text
                Dim cmd As SqlCommand = New SqlCommand(sqlQRY, conn)
                Dim rdr As SqlDataReader = cmd.ExecuteReader

                If rdr.HasRows Then

                    rdr.Read()

                    Dim BookID As Integer = rdr("AccessionNo").ToString
                    Dim newRow As BorrowDataSet.BorrowDetailsRow = BorrowDataSet.BorrowDetails.NewBorrowDetailsRow

                    newRow.BorrowID = BorrowIDTextBox.Text
                    newRow.BookID = BookID

                    BorrowDataSet.BorrowDetails.Rows.Add(newRow)

                    Me.txtSearchAccNo.Text = ""
                Else
                    MessageBox.Show("No Record found.", "No Record", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                    Me.txtSearchAccNo.Text = ""
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error!!!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Finally
                conn.Close()
            End Try
        End If
    End Sub

   
End Class