📄 transaction.vb
字号:
Try
i = dg_books.CurrentRowIndex
If rd_issue.Checked = True Then
txt_bookid.Text = dg_books.Item(i, 0)
txt_booktitle.Text = dg_books.Item(i, 1)
Else
txt_sid.Text = dg_books.Item(i, 0)
txt_sname.Text = dg_books.Item(i, 1)
txt_branch.Text = dg_books.Item(i, 2)
txt_bookid.Text = dg_books.Item(i, 3)
txt_booktitle.Text = dg_books.Item(i, 4)
txt_issue.Text = Format(dg_books.Item(i, 5), "dd-MM-yyyy")
End If
Cmb_desc.SelectedIndex = 0
Catch err As System.Exception
End Try
End Sub
Private Sub txt_sid_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_sid.KeyPress
If Asc(e.KeyChar) = Keys.Enter Then
fill_data()
End If
End Sub
Sub cleardatabind()
txt_sid.DataBindings.Clear()
txt_sname.DataBindings.Clear()
txt_branch.DataBindings.Clear()
txt_bookid.DataBindings.Clear()
txt_booktitle.DataBindings.Clear()
txt_issue.DataBindings.Clear()
End Sub
Sub clearfields()
txt_sid.Clear()
txt_sname.Clear()
txt_branch.Clear()
txt_bookid.Clear()
txt_booktitle.Clear()
txt_sid.Focus()
End Sub
Private Sub dg_books_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles dg_books.Click
get_from_grid()
If rd_return.Checked = True Then
cal_fine()
End If
End Sub
Private Sub dg_books_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dg_books.CurrentCellChanged
get_from_grid()
If rd_return.Checked = True Then
cal_fine()
End If
End Sub
Private Function check_if_issued() As Short
Dim bid As String
Try
cmd = New SqlCommand
With cmd
.Connection = cn
.CommandText = "select Book_Id from trans where S_id='" & txt_sid.Text & "'"
End With
' storing fine charge in a variable
bid = CType(cmd.ExecuteScalar, String)
If bid <> "" Then
Return 1
Else
Return 0
End If
Catch ex As SqlException
MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
Me.Close()
Finally
cmd = Nothing
End Try
End Function
Private Sub btn_new_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
clearfields()
End Sub
Private Sub btn_return_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_return.Click
Try
If txt_sid.Text <> "" AndAlso txt_sname.Text <> "" AndAlso txt_branch.Text <> "" _
AndAlso txt_bookid.Text <> "" AndAlso txt_booktitle.Text <> "" Then
'deleting the record
cmd = New SqlCommand
cmd.Connection = cn
cmd.CommandText = "delete from trans where S_Id='" & txt_sid.Text & "'"
i = cmd.ExecuteNonQuery()
If i > 0 Then
If lbl_fine_disp.Text <> "0" Then
cmd = New SqlCommand
cmd.Connection = cn
cmd.CommandText = "insert into fine_info values('" & txt_sid.Text & "'," & Val(lbl_fine_disp.Text) & ",'" & Cmb_desc.Text & "','" & Date.Today.ToShortDateString & "') "
cmd.ExecuteNonQuery()
End If
MessageBox.Show("Book successfully returned.", "LMS", MessageBoxButtons.OK, MessageBoxIcon.Information)
return_fillgrid()
clearfields()
lbl_fine_disp.Text = ""
Cmb_desc.SelectedIndex = 0
txt_issue.Clear()
Else
MessageBox.Show("Book is not present.", "LMS", MessageBoxButtons.OK, MessageBoxIcon.Stop)
txt_bookid.Focus()
End If
Else
MessageBox.Show("All fields are mandatory to fill." & vbNewLine & "Enter the student ID or select from the list shown by clicking on it.", "LMS", MessageBoxButtons.OK, MessageBoxIcon.Warning)
clearfields()
txt_sid.Focus()
End If
Catch ex As SqlException
MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch err As System.Exception
MessageBox.Show(err.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
cmd = Nothing
End Try
End Sub
Sub cal_fine()
Try
If txt_sid.Text <> "" AndAlso txt_sname.Text <> "" AndAlso txt_branch.Text <> "" _
AndAlso txt_bookid.Text <> "" AndAlso txt_booktitle.Text <> "" Then
If Cmb_desc.SelectedItem = "Late fine" Then
'calculating fine
cmd = New SqlCommand
With cmd
.Connection = cn
.CommandText = "select issue_date from trans where S_Id='" & txt_sid.Text & "'"
End With
' storing fine charge in a variable
d = CType(cmd.ExecuteScalar, Date)
If (DateDiff(DateInterval.Day, d, Date.Today) - 7) > 0 Then
lbl_fine_disp.Text = f_charge * (DateDiff(DateInterval.Day, d, Date.Today) - 7)
Else
lbl_fine_disp.Text = "0"
End If
Else 'book lost
cmd = New SqlCommand
With cmd
.Connection = cn
.CommandText = "select price from book_info where Book_Id='" & txt_bookid.Text & "'"
End With
' storing fine charge in a variable
f = CType(cmd.ExecuteScalar, Integer)
lbl_fine_disp.Text = f
End If
End If
Catch ex As SqlException
MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch err As System.Exception
MessageBox.Show(err.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
'dereferencing objects
da = Nothing
ds = Nothing
dv = Nothing
End Try
End Sub
Private Sub Cmb_desc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cmb_desc.SelectedIndexChanged
cal_fine()
End Sub
Private Sub transaction_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
cn.Close()
End Sub
Private Sub txt_sid_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_sid.LostFocus
If txt_sid.Text <> "" Then
fill_data()
End If
End Sub
Sub fill_data()
Try
cleardatabind()
da = New SqlDataAdapter("select Name,Branch from stud_info where S_Id='" & txt_sid.Text & "'", cn)
ds = New DataSet
da.Fill(ds, "stud_info")
If ds.Tables(0).Rows.Count > 0 Then
dv = New DataView(ds.Tables(0))
txt_sname.DataBindings.Add("Text", dv, "Name")
txt_branch.DataBindings.Add("Text", dv, "Branch")
If rd_issue.Checked = False Then
da = New SqlDataAdapter("select b1.Book_Id,b1.Title,t1.issue_date from book_info b1,trans t1 where b1.Book_Id=(select t2.Book_id from trans t2 where t2.S_Id='" & txt_sid.Text & "') and t1.Book_Id=b1.Book_Id", cn)
ds = New DataSet
da.Fill(ds, "stud_info")
dv = New DataView(ds.Tables(0))
If ds.Tables(0).Rows.Count > 0 Then
txt_bookid.DataBindings.Add("Text", dv, "Book_Id")
txt_booktitle.DataBindings.Add("Text", dv, "Title")
txt_issue.DataBindings.Add("Text", dv, "issue_date")
'displaying fine
cal_fine()
Else
txt_sname.Clear()
txt_branch.Clear()
txt_bookid.Clear()
txt_booktitle.Clear()
End If
End If
Else
txt_sname.Clear()
txt_branch.Clear()
txt_bookid.Clear()
txt_booktitle.Clear()
End If
Catch ex As SqlException
MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch err As System.Exception
MessageBox.Show(err.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
'dereferencing objects
da = Nothing
ds = Nothing
dv = Nothing
Cmb_desc.SelectedIndex = 0
End Try
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -