⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 students.vb

📁 图书管理系统最新的
💻 VB
📖 第 1 页 / 共 2 页
字号:
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.Color.Silver
        Me.ClientSize = New System.Drawing.Size(804, 525)
        Me.ControlBox = False
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.GroupBox1)
        Me.Controls.Add(Me.btn_close)
        Me.Controls.Add(Me.dg_stud)
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "students"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Students Information"
        Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
        Me.GroupBox1.ResumeLayout(False)
        CType(Me.dg_stud, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region



    Private Sub students_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

            cn.Open() 'opening connection
            fillgrid() 'filling student data in the grid


        Catch ex As SqlException
            MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.Close()

        Finally
            GroupBox1.Focus()
            txt_sid.Focus()

        End Try
    End Sub

    'method to fetch student info and display it in the grid
    Sub fillgrid()

        Try

            da = New SqlDataAdapter("select * from stud_info", cn)
            ds = New DataSet
            da.Fill(ds, "stud_info")
            dv = New DataView(ds.Tables(0))
            dv.Sort = "S_id"

            dg_stud.DataSource = dv


        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 btn_add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_add.Click

        Try

            If txt_sid.Text <> "" AndAlso txt_name.Text <> "" _
    AndAlso txt_add.Text <> "" AndAlso txt_contact.Text <> "" _
    AndAlso cmb_year.Text <> "" AndAlso cmb_branch.Text <> "" Then

                'inserting record
                cmd = New SqlCommand
                cmd.Connection = cn
                cmd.CommandText = "insert into stud_info values('" & txt_sid.Text & "','" & txt_name.Text & "','" & txt_add.Text & "','" & txt_contact.Text & "','" & cmb_year.Text & "','" & cmb_branch.Text & "')"
                cmd.ExecuteNonQuery()
                MessageBox.Show("Student record added successfully!!", "LMS", MessageBoxButtons.OK, MessageBoxIcon.Information)
                fillgrid()
                clearfields()

            Else 'one or more fields are blank

                MessageBox.Show("All fields are mandatory to fill.", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Warning)

            End If

        Catch ex As SqlException
            MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txt_sid.Focus()

        Catch err As System.Exception
            MessageBox.Show(err.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txt_sid.Focus()

        Finally

            cmd = Nothing

        End Try

    End Sub
    Sub cleardatabind()
        txt_sid.DataBindings.Clear()
        txt_name.DataBindings.Clear()
        txt_add.DataBindings.Clear()
        txt_contact.DataBindings.Clear()
        cmb_branch.DataBindings.Clear()
        cmb_year.DataBindings.Clear()
    End Sub
    Sub clearfields()
        txt_sid.Clear()
        txt_name.Clear()
        txt_add.Clear()
        txt_contact.Clear()
        cmb_branch.SelectedIndex = -1
        cmb_year.SelectedIndex = -1
        txt_sid.Focus()
    End Sub


    Private Sub txt_sid_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_sid.KeyPress

        ch = e.KeyChar

        If ch.IsSymbol(ch) Or ch.IsPunctuation(ch) Then
            e.Handled = True
            MessageBox.Show("Symbols are not allowed.", "LMS", MessageBoxButtons.OK, MessageBoxIcon.Stop)
        End If

        If Asc(e.KeyChar) = Keys.Enter Then

            fill_on_keypress()

        End If

    End Sub

    Sub fill_on_keypress()

        Try
            cleardatabind()
            da = New SqlDataAdapter("select * from stud_info where S_Id='" & txt_sid.Text & "'", cn)
            ds = New DataSet
            da.Fill(ds, "stud_info")
            dv = New DataView(ds.Tables(0))


            If ds.Tables("stud_info").Rows.Count <> 0 Then 'no rows selected

                dv = New DataView(ds.Tables(0))

                txt_name.DataBindings.Add("Text", dv, "Name")
                txt_add.DataBindings.Add("Text", dv, "Address")
                txt_contact.DataBindings.Add("Text", dv, "Contact")
                cmb_year.DataBindings.Add("Text", dv, "A_year")
                cmb_branch.DataBindings.Add("Text", dv, "Branch")


            Else

                txt_name.Clear()
                txt_add.Clear()
                txt_contact.Clear()
                cmb_year.SelectedIndex = -1
                cmb_branch.SelectedIndex = -1

            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 txt_name_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_name.KeyPress

        ch = e.KeyChar

        If Not (ch.IsLetter(ch) Or ch.IsWhiteSpace(ch) Or Asc(e.KeyChar) = Keys.Back) Then
            e.Handled = True
        End If
    End Sub



    Private Sub btn_remove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_remove.Click
        Try

            If txt_sid.Text <> "" AndAlso txt_name.Text <> "" Then

                'deleting the record
                cmd = New SqlCommand
                cmd.Connection = cn
                cmd.CommandText = "delete from stud_info where S_id='" & txt_sid.Text & "' and S_id not in (select S_Id from trans)"
                i = cmd.ExecuteNonQuery()

                If i > 0 Then
                    MessageBox.Show("Student record deleted successfully.", "LMS", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    fillgrid()
                    clearfields()
                Else
                    MessageBox.Show("Can not delete because Book(s) is issued to this student.", "LMS", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    txt_sid.Focus()
                End If

            Else

                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 get_from_grid()
        Try
            i = dg_stud.CurrentRowIndex
            txt_sid.Text = dg_stud.Item(i, 0)
            txt_name.Text = dg_stud.Item(i, 1)
            txt_add.Text = dg_stud.Item(i, 2)
            txt_contact.Text = dg_stud.Item(i, 3)
            cmb_year.Text = dg_stud.Item(i, 4)
            cmb_branch.Text = dg_stud.Item(i, 5)


        Catch err As System.Exception

        End Try
    End Sub

    Private Sub dg_stud_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles dg_stud.Click
        get_from_grid()
    End Sub

    Private Sub dg_stud_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dg_stud.CurrentCellChanged
        get_from_grid()
    End Sub

    Private Sub btn_close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_close.Click
        Me.Close()
    End Sub

    Private Sub students_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_on_keypress()
        End If
    End Sub

    Private Sub lbl_help_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbl_help.Click
        MessageBox.Show("You will have to delete the record to be updated and insert it again with the new information.", "LMS Help", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -