我正在使用MS Visual Basic 2012,我正在创建一个简单的编程,我面临的问题是“没有给出一个或多个必需参数的值”请帮帮我,看下面的代码
Public Class frmStudent
Dim cnn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\VBDatabase.accdb")
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
Me.txtstdID.Text = ""
Me.txtStdName.Text = ""
Me.txtFName.Text = ""
Me.txtMName.Text = ""
Me.txtAddress.Text = ""
Me.txtstdID.Tag = ""
'enable button edit
Me.btnEdit.Enabled = True
'set button add to add label
Me.btnAdd.Text = "Add"
'
Me.txtstdID.Focus()
End Sub
Private Sub RefreshData()
If Not cnn.State = ConnectionState.Open Then
'open connection
cnn.Open()
End If
Dim da As New OleDb.OleDbDataAdapter("SELECT StdID as [ID], " & _
"StdName as [Name], Gender, ClassName, FName, MName, Address" & _
" FROM tblStudent ORDER BY StdID", cnn)
Dim dt As New DataTable
'fill data to datatable
da.Fill(dt)
'offer data in data table into datagridview
Me.dgvData.DataSource = dt
'close connection
cnn.Close()
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim cmd As New OleDb.OleDbCommand
If Not cnn.State = ConnectionState.Open Then
'open connection if it is not yet open
cnn.Open()
End If
cmd.Connection = cnn
'check whether add new or update
If Me.txtstdID.Tag & "" = "" Then
'add new
'add data to table
cmd.CommandText = "INSERT INTO tblstudent(StdID, StdName, Gender, ClassName, FName, MName, Address) " & _
" VALUES(" & Me.txtstdID.Text & ",'" & Me.txtStdName.Text & "','" & _
Me.cboGender.Text & "','" & Me.cboClassName.Text & "','" & Me.txtFName.Text & "','" & Me.txtMName.Text & "','" & _
Me.txtAddress.Text & "')"
cmd.ExecuteNonQuery()
Else
'update data in table
cmd.CommandText = "UPDATE tblStudent " & _
" SET StdID=" & Me.txtstdID.Text & _
", StdName='" & Me.txtStdName.Text & "'" & _
", Gender='" & Me.cboGender.Text & "'" & _
", FName='" & Me.txtFName.Text & "'" & _
", MName='" & Me.txtMName.Text & "'" & _
", Address='" & Me.txtAddress.Text & "'" & _
" WHERE StdID=" & Me.txtstdID.Tag
cmd.ExecuteNonQuery()
End If
'refresh data in list
RefreshData()
'clear form
Me.btnClear.PerformClick()
'close connection
cnn.Close()
End Sub
Private Sub frmStudent_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'TODO: This line of code loads data into the 'VBDatabaseDataSet1.tblStudent' table. You can move, or remove it, as needed.
Me.TblStudentTableAdapter.Fill(Me.VBDatabaseDataSet1.tblStudent)
cnn = New OleDb.OleDbConnection
cnn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\VBDatabase.accdb"
'
'get data into list
Me.RefreshData()
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
'check for the selected item in list
If Me.dgvData.Rows.Count > 0 Then
If Me.dgvData.SelectedRows.Count > 0 Then
Dim intStdID As Integer = Me.dgvData.SelectedRows(0).Cells("ID").Value
'get data from database followed by student id
'open connection
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
'get data into datatable
Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM tblStudent " & _
" WHERE StdID=" & intStdID, cnn)
Dim dt As New DataTable
da.Fill(dt)
Me.txtstdID.Text = intStdID
Me.txtStdName.Text = dt.Rows(0).Item("StdName")
Me.cboGender.Text = dt.Rows(0).Item("Gender")
Me.cboClassName.Text = dt.Rows(0).Item("ClassName")
Me.txtFName.Text = dt.Rows(0).Item("FName")
Me.txtMName.Text = dt.Rows(0).Item("MName")
Me.txtAddress.Text = dt.Rows(0).Item("Address")
'
'hide the id to be edited in TAG of txtstdid in case id is changed
Me.txtstdID.Tag = intStdID
'change button add to update
Me.btnAdd.Text = "Update"
'disable button edit
Me.btnEdit.Enabled = False
'close connection
cnn.Close()
End If
End If
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
'check for the selected item in list
If Me.dgvData.Rows.Count > 0 Then
If Me.dgvData.SelectedRows.Count > 0 Then
Dim intStdID As Integer = Me.dgvData.SelectedRows(0).Cells("ID").Value
'open connection
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
'delete data
Dim cmd As New OleDb.OleDbCommand
cmd.Connection = cnn
cmd.CommandText = "DELETE FROM tblStudent WHERE StdID=" & intStdID
cmd.ExecuteNonQuery()
'refresh data
Me.RefreshData()
'close connection
cnn.Close()
End If
End If
End Sub
End Class