我的更新内容包括:Id,Code,Name,Type,Publisher,PurDate,Comment。为什么我的更新不能实现:要不就是没改,要不就是为空。。请专家帮我诊断!谢谢!我的毕业设计其他全部完成了,就差这个更新功能了,急人啊!!拜托各位大侠了,小弟拜谢!
程序如下:
Private Sub dgbook_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgbook.UpdateCommand
Dim ds As DataSet = bookInfo.GetBookInfo
Dim Id As Integer
Dim Code As String
Dim Name As String
Dim Type As String
Dim Publisher As String
Dim PurDate As String
Dim Comment As String
'execute the update
Try
Id = CType(e.Item.Cells(0).Text, Integer)
Code = CType(e.Item.Cells(1).Controls(0), TextBox).Text
Name = CType(e.Item.Cells(2).Controls(0), TextBox).Text
Type = CType(e.Item.Cells(3).Controls(0), TextBox).Text
Publisher = CType(e.Item.Cells(4).Controls(0), TextBox).Text
PurDate = CType(e.Item.Cells(5).Controls(0), TextBox).Text
Comment = CType(e.Item.Cells(6).Controls(0), TextBox).Text
updBookInfo.chgbook(Id, Code, Name, Type, Publisher, PurDate, Comment)
Catch ex As SqlException
lbl1.Text = "Sql exception:" & ex.Message
Catch ex As Exception
lbl1.Text = "General exception:" & ex.Message
Finally
dgbook.EditItemIndex = -1
BindGrid(ds)
End Try
End Sub
程序如下:
Private Sub dgbook_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgbook.UpdateCommand
Dim ds As DataSet = bookInfo.GetBookInfo
Dim Id As Integer
Dim Code As String
Dim Name As String
Dim Type As String
Dim Publisher As String
Dim PurDate As String
Dim Comment As String
'execute the update
Try
Id = CType(e.Item.Cells(0).Text, Integer)
Code = CType(e.Item.Cells(1).Controls(0), TextBox).Text
Name = CType(e.Item.Cells(2).Controls(0), TextBox).Text
Type = CType(e.Item.Cells(3).Controls(0), TextBox).Text
Publisher = CType(e.Item.Cells(4).Controls(0), TextBox).Text
PurDate = CType(e.Item.Cells(5).Controls(0), TextBox).Text
Comment = CType(e.Item.Cells(6).Controls(0), TextBox).Text
updBookInfo.chgbook(Id, Code, Name, Type, Publisher, PurDate, Comment)
Catch ex As SqlException
lbl1.Text = "Sql exception:" & ex.Message
Catch ex As Exception
lbl1.Text = "General exception:" & ex.Message
Finally
dgbook.EditItemIndex = -1
BindGrid(ds)
End Try
End Sub
如果没有值,说明上面的赋值语句有问题
如果取到值了,说明你的updBookInfo.chgbook有问题,可以把updBookInfo.chgbook的代码贴出来看看。
Code = CType(e.Item.Cells(1).Controls(0), TextBox).Text
Name = CType(e.Item.Cells(2).Controls(0), TextBox).Text
Type = CType(e.Item.Cells(3).Controls(0), TextBox).Text
Publisher = CType(e.Item.Cells(4).Controls(0), TextBox).Text
PurDate = CType(e.Item.Cells(5).Controls(0), TextBox).Text
Comment = CType(e.Item.Cells(6).Controls(0), TextBox).Text 能取到值吗
Code = CType(e.Item.Cells(1).Controls(0), TextBox).Text
Name = CType(e.Item.Cells(2).Controls(0), TextBox).Text
Type = CType(e.Item.Cells(3).Controls(0), TextBox).Text
Publisher = CType(e.Item.Cells(4).Controls(0), TextBox).Text
PurDate = CType(e.Item.Cells(5).Controls(0), TextBox).Text
Comment = CType(e.Item.Cells(6).Controls(0), TextBox).Text这样不可以么? 如果加一个修改函数该怎么写啊/?小弟我不会啊!
Public Function chgbook(ByVal id As Integer, _
ByVal code As String, _
ByVal name As String, _
ByVal type As String, _
ByVal publisher As String, _
ByVal Purchase As String, _
ByVal Comment As String) Dim myCommand As SqlCommand
Dim paraid As SqlParameter
Dim paraName As SqlParameter
Dim paraCode As SqlParameter
Dim paraType As SqlParameter
Dim paraPublisher As SqlParameter
Dim paraPurchase As SqlParameter
Dim paraComment As SqlParameter myCommand = New SqlCommand("ChangeBookInfo", conn)
myCommand.CommandType = CommandType.StoredProcedure paraid = New SqlParameter("@BookId", SqlDbType.Int, 9)
paraCode = New SqlParameter("@BookCode", SqlDbType.Char, 10)
paraName = New SqlParameter("@BookName", SqlDbType.VarChar, 500)
paraType = New SqlParameter("@BookType", SqlDbType.Char, 10)
paraPublisher = New SqlParameter("@BookPublisher", SqlDbType.VarChar, 500)
paraPurchase = New SqlParameter("@BookPurchaseTime", SqlDbType.Char, 10)
paraComment = New SqlParameter("@BookComment", SqlDbType.VarChar, 500) paraid.Value = id
paraCode.Value = code
paraName.Value = name
paraType.Value = type
paraPublisher.Value = publisher
paraPurchase.Value = Purchase
paraComment.Value = Comment myCommand.Parameters.Add(id)
myCommand.Parameters.Add(paraCode)
myCommand.Parameters.Add(paraName)
myCommand.Parameters.Add(paraType)
myCommand.Parameters.Add(paraPublisher)
myCommand.Parameters.Add(paraPurchase)
myCommand.Parameters.Add(paraComment) If conn.ConnectionString = "" Or conn Is Nothing Then
getConnection()
End If
myCommand.ExecuteNonQuery()
myCommand.Connection.Close()
End Function
在代码窗口中,找到
updBookInfo.chgbook(Id, Code, Name, Type, Publisher, PurDate, Comment)
按F9,添加一个断点,执行WEB程序,看一下Id,Code等变量是否有值。
按同样的办法看一下程序是否执行到myCommand.ExecuteNonQuery()
改为
myCommand.Parameters.Add(paraid)
Id = CType(e.Item.Cells(0).Text, Integer)
Code = CType(e.Item.Cells(1).Controls(0), TextBox).Text
Name = CType(e.Item.Cells(2).Controls(0), TextBox).Text
Type = CType(e.Item.Cells(3).Controls(0), TextBox).Text
Publisher = CType(e.Item.Cells(4).Controls(0), TextBox).Text
PurDate = CType(e.Item.Cells(5).Controls(0), TextBox).Text
Comment = CType(e.Item.Cells(6).Controls(0), TextBox).Text
并不能取到我修改的值,,,这个是为什么呢?
在DataGrid中把你的EditTemplate中的TextBox,Label都标上Id,然后在UPdate_command中
使用Id = Ctype(CType(e.Item.findcontrol("lblxxx"), Label).Text)
的方法
第8章第8个例子看一下