📄 update-check-errors.aspx
字号:
<%@Page Language="VB"%>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %>
<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Validating Edits in a Table with the RowUpdated Event</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Validating Edits in a Table with the RowUpdated Event</span><hr />
<!--------------------------------------------------------------------------->
<%'-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server"/>
<div>Connection string: <b><span id="outConnect" runat="server"></span></b></div>
<div>SELECT command: <b><span id="outSelect" runat="server"></span></b></div>
<div id="outError" runat="server"> </div>
<b>Changed records in the Books table after editing:</b>
<asp:datagrid id="dgrResult" runat="server" /><p />
<div id="outResult" runat="server"></div>
<script language="vb" runat="server">
Sub Page_Load()
'get connection string from ..\global\connect-strings.ascx user control
Dim strConnect = ctlConnectStrings.OLEDBConnectionString
outConnect.innerText = strConnect 'and display it
'specify the SELECT statement to extract the data
Dim strSelect As String
strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '18610027%'"
outSelect.innerText = strSelect 'and display it
Dim strResult As String = "" 'to hold the result
'create a new DataSet object
Dim objDataSet As New DataSet()
'create a new Connection object using the connection string
Dim objConnect As New OleDbConnection(strConnect)
'create a new DataAdapter using the connection object and select statement
Dim objDataAdapter As New OleDbDataAdapter(strSelect, objConnect)
Try
'fill the dataset with data from the DataAdapter object
objDataAdapter.Fill(objDataSet, "Books")
Catch objError As Exception
'display error details
outError.innerHTML = "<b>* Error while accessing data</b>.<br />" _
& objError.Message & "<br />" & objError.Source
Exit Sub ' and stop execution
End Try
'accept the changes to "fix" the current state of the DataSet contents
objDataSet.AcceptChanges()
'now set up event handler to react to changes to the rows in the table
Dim objTable As DataTable = objDataSet.Tables("Books")
AddHandler objTable.RowChanged, New DataRowChangeEventHandler(AddressOf OnRowChanged)
'now change some records in the Books table
objTable.Rows(0)("Title") = "Amateur Theatricals for Windows 2000"
objTable.Rows(2)("PublicationDate") = "11-02-2001"
'declare a variable to hold a DataView object
Dim objDataView As DataView
'get DataView and set to show only modified rows
objDataView = objDataSet.Tables(0).DefaultView
objDataView.RowStateFilter = DataRowState.Modified
'display the contents of the modified rows
dgrResult.DataSource = objDataView
dgrResult.DataBind()
'see if there are any update errors anywhere in the DataSet
If objDataSet.HasErrors Then
'check each table for errors in that table
Dim objThisTable As DataTable
For Each objThisTable In objDataSet.Tables
If objThisTable.HasErrors Then
strResult += "One or more errors found in table '<b>" _
& objThisTable.TableName & "</b>'<br />"
'check each row in this table for errors
Dim objThisRow As DataRow
For Each objThisRow In objThisTable.Rows
If objThisRow.HasErrors Then
strResult += "* Row with ISBN=<b>" _
& objThisRow("ISBN") _
& "</b> has error <b>" _
& objThisRow.RowError & "</b><br />"
End If
Next 'row
End If
Next 'table
End If
outResult.InnerHtml = strResult 'display the result
End Sub
'event handler for the RowChanged event
Sub OnRowChanged(objSender As Object, objArgs As DataRowChangeEventArgs)
'only react if the action is "Change"
If objArgs.Action = DataRowAction.Change Then
'validate a new title
If InStr(objArgs.Row("Title"), "Amateur") > 0 Then
objArgs.Row.RowError = "'Amateur' is not a recognized book series prefix"
End If
'validate a new publication date
If objArgs.Row("PublicationDate").DayOfWeek = 0 _
Or objArgs.Row("PublicationDate").DayOfWeek = 6 Then
objArgs.Row.RowError += "Publication date must be a weekday"
End If
End If
End Sub
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -