我在编写一个程序时,在试着运行时,发现没次都只能成功运行第一次操作,第二次操作时,总会有错误提示.关闭程序再运行程序后,还是第一次运行能成功,第二次就失败,总是提示"变量名已申明"或者提示"参数过多"等信息.请哪位高手能指出问题所在!!!!谢谢.
主体代码如下:
Imports System.Data.SqlClient
Public Class frmAdminisOpenNewClass
Inherits System.Windows.Forms.Form
Dim strCommTeacher As String = "SELECT EmpName FROM EmployeerInf"
Dim strCommDelete As String = "DELETE FROM ClassInf WHERE ClassID=@ClassID"
Dim btnType As String
Dim myCommand As New SqlCommand(strCommTeacher, myConnection)
Dim myDataAdapter As New SqlDataAdapter(myCommand)
Dim myDataSet As New DataSet
Dim myReader As SqlDataReader
Private Sub frmAdminisOpenNewClass_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'从表EmployeerInf获取班主任(教师)信息
myConnection.Open()
myReader = myCommand.ExecuteReader
While myReader.Read
cbbClassTeacher.Items.Add(myReader(0))
End While
myReader.Close()
myConnection.Close()
DataGrid()
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If MsgBox("您确定要删除该班级信息吗?请谨慎操作!!", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
btnType = "delete"
MsgBox("请‘保存’删除操作或按‘取消’功能键取消该操作!")
End If
End Sub
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
If myConnection.State = ConnectionState.Closed Then
myConnection.Open()
End If
'判断何种操作并获取相关操作数据
Select Case btnType
Case "delete"
myCommand.CommandText = strCommDelete
myCommand.Parameters.Add("@ClassID", SqlDbType.Char, 10)
myCommand.Parameters(0).Value = Format(txtClassID.Text)
End Select
Try
myCommand.ExecuteNonQuery()
Catch er As System.Data.SqlClient.SqlException
MsgBox(er.Message)
End Try
'刷新数据显示
DataGrid()
myConnection.Close()
End Sub
'添加参数并赋值
Sub DataGrid()
Dim strCommDataGrid As String = "SELECT ClassID as 班级编号,ClassName as 班级名 称,ClassType as 班级类型,ClassStudent as 班级人数,ClassAddress as 教室地点,ClassTeacher as 班主任,ClassRemark as 其他备注 FROM ClassInf"
Dim myCommandDataGrid As New SqlCommand(strCommDataGrid, myConnection)
Dim myAdapter As New SqlDataAdapter(myCommandDataGrid)
Dim mySet As New DataSet
mySet.Clear()
myAdapter.Fill(mySet, "ClassInf")
'绑定数据到控件
myDataGrid.SetDataBinding(mySet, "ClassInf")
End Sub
Private Sub myDataGrid_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles myDataGrid.DoubleClick
'选定要修改的数据
Dim intRow As Integer
Dim strClassID As String
intRow = myDataGrid.CurrentCell.RowNumber
strClassID = myDataGrid.Item(intRow, 0)
txtClassID.Text = Format(myDataGrid.Item(intRow, 0))
txtClassName.Text = Format(myDataGrid.Item(intRow, 1))
cbbClassType.Text = Format(myDataGrid.Item(intRow, 2))
txtClassStudent.Text = Format(myDataGrid.Item(intRow, 3))
txtClassAddress.Text = Format(myDataGrid.Item(intRow, 4))
cbbClassTeacher.Text = Format(myDataGrid.Item(intRow, 5))
txtClassRemark.Text = Format(myDataGrid.Item(intRow, 6))
End Sub
End Class
错误图形:
[此贴子已经被作者于2006-3-6 19:22:42编辑过]