导航
  • 主页
  • VBS
  • .NET技术
  • VBA
  • VB网络编程
  • VB多媒体
  • VB数据库
  • VB控件
  • VB COM/DCOM
  • VB基础类
  • VB API
  • 问答

急哦DATAGRID数据无法更新删除 求解

ch00549476 2006-06-03 02:43:46
Imports System.Data
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page

#Region " Web 窗体设计器生成的代码 "

'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents lblError As System.Web.UI.WebControls.Label

'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub

#End Region

Dim mycn As SqlConnection
Dim myda As SqlDataAdapter
Dim ds As DataSet
Dim strConn, strSQL As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
strConn = "server=127.0.0.1;uid=sa;database=xs;pwd=1234;"
If Not Page.IsPostBack Then
BindGrid()
End If
'在此处放置初始化页的用户代码
End Sub
Sub BindGrid()
mycn = New SqlConnection(strConn)
strSQL = "Select * from [dd] "
myda = New SqlDataAdapter(strSQL, mycn)
ds = New DataSet
myda.Fill(ds, "Table")
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub


Private Sub DataGrid1_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.CancelCommand
DataGrid1.EditItemIndex = -1
BindGrid()
End Sub

Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
DataGrid1.EditItemIndex = CInt(e.Item.ItemIndex)
BindGrid()
End Sub

Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand
Dim hwname As String = CType(e.Item.Cells(3).Controls(0), TextBox).Text
Dim sj As String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
Dim userid As Integer = CInt(DataGrid1.DataKeys(CInt(e.Item.ItemIndex)))
Try
Dim updateCmd As String = "UPDATE [dd] SET hwname = @hwname," + "sj = @sj ,where userid =@userid "
Dim cn As New SqlConnection(strConn)
Dim myCommand As New SqlCommand(updateCmd, cn)
myCommand.Parameters.Add(New SqlParameter("@hwname", Convert.ToDecimal(hwname)))
myCommand.Parameters.Add(New SqlParameter("@sj", Convert.ToInt16(sj)))
myCommand.Parameters.Add(New SqlParameter("@userid", userid))
cn.Open()
myCommand.ExecuteNonQuery()
DataGrid1.EditItemIndex = -1
BindGrid()
Catch ex As Exception
lblError.Visible = True
lblError.Text = ex.Message

End Try

End Sub

Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand
Dim userid As Integer = CInt(DataGrid1.DataKeys(CInt(e.Item.ItemIndex)))

Dim deleteCmd As [String] = "DELETE from dd where userid = @userid "

Dim cn As New SqlConnection(strConn)

Dim myCommand As New SqlCommand(deleteCmd, cn)

myCommand.Parameters.Add(New SqlParameter("@userid", SqlDbType.Int))

myCommand.Parameters("@userid").Value = DataGrid1.DataKeys(CInt(e.Item.ItemIndex))

myCommand.Connection.Open()

Try

myCommand.ExecuteNonQuery()

Catch

lblError.Text = "ERROR: Could not delete record"

End Try

myCommand.Connection.Close()

BindGrid()

End Sub

End Class
显示 lblError.Text = "ERROR: Could not delete record" 郁闷ING 更新也不成 火了上来求教下
...全文
58 点赞 收藏 4
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
HALOcn 2006-06-03
UP
回复
youlinga 2006-06-03
把 DataGrid1.DataKeys(CInt(e.Item.ItemIndex))用一个LABLE显示看看 有没有
回复
youlinga 2006-06-03
把lblError.Text=ex.Message 改成这个具体是什么提示?
回复
liujiayu10 2006-06-03
你首先去掉这个Try,然后单步调试,看看错误到底出在哪里!
回复
发动态
发帖子
VB
创建于2007-09-28

1.5w+

社区成员

VB技术相关讨论,主要为经典vb,即VB6.0
申请成为版主
社区公告
暂无公告