急哦DATAGRID数据无法更新删除 求解
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 更新也不成 火了上来求教下