Вы находитесь на странице: 1из 6

CODING

~~~~~~~
Imports System.Data.OleDb

Public Class Form1


Public con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; DataSource=|
DataDirectory|
\student.accdb")
Dim cmd As New OleDbCommand
Dim ds As New DataSet
Dim da As New OleDbDataAdapter("SELECT * FROM Mark", con)
Dim t As New DataTable
Dim bs As New BindingSource
Dim s As String
Dim st As String
Private Sub LoadGrid()
con.Open()
cmd.Connection = con
da.Fill(ds, "Mark")
DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = ds.Tables(0)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


MyBase.Load

'TODO: This line of code loads data into the 'StudentDataSet2.Mark' table. You can move, or
remove it, as needed.
Me.MarkTableAdapter.Fill(Me.StudentDataSet2.Mark)
con.Open()
cmd.Connection = con
da.Fill(ds, "Mark")
DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = ds.Tables(0)

End Sub
Private Sub add_Click(ByVal ByVader As System.Object, ByVal e As System.EventArgs) Handles
add.Click

If String.IsNullOrEmpty(TextBox1.Text) Or String.IsNullOrEmpty(TextBox2.Text) Or
String.IsNullOrEmpty(TextBox3.Text) Or String.IsNullOrEmpty(TextBox4.Text) Or
String.IsNullOrEmpty(TextBox5.Text) Or String.IsNullOrEmpty(TextBox6.Text) Or
String.IsNullOrEmpty(TextBox7.Text) Or String.IsNullOrEmpty(TextBox8.Text) Or
String.IsNullOrEmpty(TextBox9.Text) Then
MessageBox.Show("Field is empty")
Else
cmd.CommandText = "INSERT INTO
Mark(ID,Name,Class,Net,OST,COM,TOTAL,AVERAGE,Grade)
VALUES('" & TextBox1.Text & "','" & TextBox2.Text & "','" &
TextBox3.Text &
"','" & TextBox4.Text & "','" & TextBox5.Text & "','" &
TextBox6.Text
& "','" & TextBox7.Text & "','" & TextBox8.Text & "','" &
TextBox9.Text & "')"
cmd.ExecuteNonQuery()
MessageBox.Show("New Record Inserted", "Insert Success", MessageBoxButtons.OK,
MessageBoxIcon.Information)
con.Close()
ds.Clear()
LoadGrid()
End If
End Sub

Private Sub search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


search.Click
Dim i As Integer
i = DataGridView1.CurrentRow.Index
If DataGridView1.CurrentRow.IsNewRow Then
add.Enabled = True

updatecmd.Enabled = False
delete.Enabled = False
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox6.Clear()
TextBox7.Clear()
TextBox8.Clear()
MessageBox.Show("Empty Row Selected", "Row Selection", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Else
add.Enabled = False
updatecmd.Enabled = True
delete.Enabled = True
TextBox1.Text = DataGridView1.Item(0, i).Value
TextBox2.Text = DataGridView1.Item(1, i).Value
TextBox3.Text = DataGridView1.Item(2, i).Value
TextBox4.Text = DataGridView1.Item(3, i).Value
TextBox5.Text = DataGridView1.Item(4, i).Value
TextBox6.Text = DataGridView1.Item(5, i).Value
TextBox7.Text = DataGridView1.Item(6, i).Value
TextBox8.Text = DataGridView1.Item(7, i).Value
TextBox9.Text = DataGridView1.Item(8, i).Value
End If
End Sub

Private Sub delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


delete.Click

If String.IsNullOrEmpty(TextBox1.Text) Or String.IsNullOrEmpty(TextBox2.Text) Or
String.IsNullOrEmpty(TextBox3.Text) Or String.IsNullOrEmpty(TextBox4.Text) Or
String.IsNullOrEmpty(TextBox5.Text) Or String.IsNullOrEmpty(TextBox6.Text) Or

String.IsNullOrEmpty(TextBox7.Text) Or String.IsNullOrEmpty(TextBox8.Text) Or
String.IsNullOrEmpty(TextBox9.Text) Then
MessageBox.Show("Field is empty")

else
cmd.CommandText = "delete from Mark where Name ='" & TextBox2.Text & "'"
cmd.ExecuteNonQuery()
MessageBox.Show("Record Deleted", "Delete Success", MessageBoxButtons.OK,
MessageBoxIcon.Information)
con.Close()
ds.Clear()
DataGridView1.DataSource = Nothing
LoadGrid()
End If
End Sub

Private Sub updatecmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles updatecmd.Click

If String.IsNullOrEmpty(TextBox1.Text) Or String.IsNullOrEmpty(TextBox2.Text) Or
String.IsNullOrEmpty(TextBox3.Text) Or String.IsNullOrEmpty(TextBox4.Text) Or
String.IsNullOrEmpty(TextBox5.Text) Or String.IsNullOrEmpty(TextBox6.Text) Or
String.IsNullOrEmpty(TextBox7.Text) Or String.IsNullOrEmpty(TextBox8.Text) Or
String.IsNullOrEmpty(TextBox9.Text) Then
MessageBox.Show("Field is empty")
Else
cmd.CommandText = "UPDATE Mark SET Name='" & TextBox2.Text & "',Class='" &
TextBox3.Text &
"',Net='" & TextBox4.Text & "',OST='" & TextBox5.Text & "',COM='" &
TextBox6.Text & "',Total='" & TextBox7.Text & "',Average='" &
TextBox8.Text &
"',Grade='" & TextBox9.Text & "'WHERE id= " & TextBox1.Text & ";"
cmd.ExecuteNonQuery()

MessageBox.Show("Updated Successfully", "Update Success", MessageBoxButtons.OK,


MessageBoxIcon.Information)
con.Close()
ds.Clear()
DataGridView1.DataSource = Nothing
LoadGrid()
End If
End Sub

Private Sub clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


clear.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox9.Text = ""
End Sub

Private Sub TextBox7_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles


TextBox7.GotFocus

TextBox7.Text = Val(TextBox4.Text) + Val(TextBox5.Text) + Val(TextBox6.Text)


TextBox8.Text = Val(TextBox7.Text) / 3
If (Val(TextBox4.Text) > 50 And Val(TextBox5.Text) > 50 And Val(TextBox6.Text) > 50) Then
If (TextBox8.Text >= 90) Then
TextBox9.Text = "O+"
ElseIf (TextBox8.Text >= 80 And TextBox8.Text <= 90) Then
TextBox9.Text = "A"
ElseIf (TextBox8.Text >= 50 And TextBox8.Text <= 80) Then
TextBox9.Text = "B"

Else
TextBox9.Text = "Re-appear"
End If

Else
TextBox9.Text = "Re-appear"
End If
End Sub
End Class

Оценить