update-check-errors.aspx
来自「东软内部材料(四)asp等相关的教学案例 」· ASPX 代码 · 共 144 行
ASPX
144 行
<%@Page Language="C#"%>
<%@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="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
// get connection string from ..\global\connect-strings.ascx user control
string strConnect = ctlConnectStrings.OLEDBConnectionString;
outConnect.InnerText = strConnect; // and display it
// specify the SELECT statement to extract the data
string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '18610027%'";
outSelect.InnerText = strSelect; // and display it
string strResult = "" ; // to hold the result
// create a new DataSet object
DataSet objDataSet = new DataSet();
// create a new Connection object using the connection string
OleDbConnection objConnect = new OleDbConnection(strConnect);
// create a new DataAdapter using the connection object and select statement
OleDbDataAdapter objDataAdapter = new OleDbDataAdapter(strSelect, objConnect);
try
{
// fill the dataset with data from the DataAdapter object
objDataAdapter.Fill(objDataSet, "Books");
}
catch (Exception objError)
{
// display error details
outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
+ objError.Message + "<br />" + objError.Source;
return; // and stop execution
}
// 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
DataTable objTable = objDataSet.Tables["Books"];
objTable.RowChanged += new DataRowChangeEventHandler(OnRowChanged);
// now change some records in the Books table
objTable.Rows[0]["Title"] = "Amateur Theatricals for Windows 2000";
objTable.Rows[2]["PublicationDate"] = new DateTime(2001, 2, 11);
// declare a variable to hold a DataView object
DataView objDataView;
// get DataView and set to show only modified rows
objDataView = objDataSet.Tables[0].DefaultView;
objDataView.RowStateFilter = (DataViewRowState)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)
{
// check each table for errors in that table
foreach (DataTable objThisTable in objDataSet.Tables)
{
if (objThisTable.HasErrors)
{
strResult += "One or more errors found in table '<b>"
+ objThisTable.TableName + "</b>'<br />";
// check each row in this table for errors
foreach (DataRow objThisRow in objThisTable.Rows)
{
if (objThisRow.HasErrors)
{
strResult += "* Row with ISBN=<b>"
+ objThisRow["ISBN"]
+ "</b> has error <b>"
+ objThisRow.RowError + "</b><br />";
}
} // row
}
} // table
}
outResult.InnerHtml = strResult; // display the result
}
// event handler for the RowChanged event
void OnRowChanged(Object objSender, DataRowChangeEventArgs objArgs)
{
// only react if the action is "Change"
if (objArgs.Action == DataRowAction.Change)
{
// validate a new title
if (objArgs.Row["Title"].ToString().IndexOf("Amateur") != -1)
objArgs.Row.RowError = "'Amateur' is not a recognized book series prefix";
// validate a new publication date
if (((DateTime)objArgs.Row["PublicationDate"]).DayOfWeek == DayOfWeek.Sunday
|| ((DateTime)objArgs.Row["PublicationDate"]).DayOfWeek == DayOfWeek.Saturday)
objArgs.Row.RowError += "Publication date must be a weekday";
}
}
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?