555~~~穷死了,最后40分了,希望大家帮我看看,谢谢!
我要实现在DataGrid中对数据库的更新,数据源是sql server中的student表,主键是Sno,现在的问题是,不能修改学号!真的是很郁闷!我看了半天代码也没有发现错误,大家帮我看看吧!谢谢了,最后的40分全给大家了,呵呵(明天就不能来了,穷死了)
update函数代码如下:
sub Stu_Update(sender as object,E as DataGridCommandEventArgs)
dim UComm as SqlCommand
dim UpdateCmd as string
dim EditText1,EditText2,EditText3 as textbox
UpdateCmd="update student set Sno=@Sno,Sname=@Sname,Sbirth=@Sbirth Where Sno=@Sno"
UComm=new SQLCommand(UpdateCmd,objConnection)
UComm.Parameters.Add(New SqlParameter("@Sno", SqlDbType.VarChar))
UComm.Parameters.Add(New SqlParameter("@Sname", SqlDbType.VarChar))
UComm.Parameters.Add(New SqlParameter("@Sbirth", SqlDbType.VarChar))
UComm.Parameters("@Sno").Value = _
StuData.DataKeys(CInt(e.Item.ItemIndex))
Dim cols() As String = {"@Sno","@Sname","@Sbirth"}
Dim numCols As Integer = E.Item.Cells.Count
'Dim i As Integer
Dim colvalue As String
Dim txtBox As Textbox
txtBox =E.Item.FindControl("txtSno")
colvalue = txtBox.Text
If (colvalue = "")
ErrorMessage.text = "ERROR: Null values not allowed for 学号"
ErrorMessage.Style("color") = "red"
Exit Sub
End If
UComm.Parameters(cols(0)).Value = colvalue
txtBox = E.Item.FindControl("txtSname")
colvalue = txtBox.Text
If (colvalue = "")
ErrorMessage.text = "ERROR: Null values not allowed for 姓名"
ErrorMessage.Style("color") = "red"
Exit Sub
End If
UComm.Parameters(cols(1)).Value = colvalue
txtBox = E.Item.FindControl("txtSbirth")
colvalue = txtBox.Text
If (colvalue = "")
ErrorMessage.text = "ERROR: Null values not allowed for 出生年月"
ErrorMessage.Style("color") = "red"
Exit Sub
End If
UComm.Parameters(cols(2)).Value = colvalue
UComm.Connection.Open()
try
UComm.ExecuteNonQuery()
Session("MyData")=nothing
StuData.EditItemIndex=-1
Catch exp as SQLexception
if exp.number=2627 then
ErrorMessage.text="已存在同样的记录"
ErrorMessage.Style("Color")="red"
Else
ErrorMessage.Text="不能更新记录"
End if
end try
'关闭连接
UComm.Connection.Close()
BindGrid()
ErrorMessage.text=""
end sub