Index was outside the bounds of the array
-------------------------------------------------------------
The system "Exception Details:"Index was outside the bounds of the array"" -----------How wrong? who can tell me?
<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e) {
string connectionString;
string queryString;
DataSet data = new DataSet();
OleDbConnection dbConnection;
OleDbDataAdapter dataAdapter;
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; " +
"Data Source=C:\\BegASPNet11\\data\\Northwind.mdb";
queryString = "SELECT FirstName, LastName FROM Employees";
dbConnection = new OleDbConnection(connectionString);
dataAdapter = new OleDbDataAdapter(queryString, dbConnection);
dataAdapter.Fill(data, "Employees");
DataGrid1.DataSource = data;
DataGrid1.DataBind();
// ---------------------------------------------------------
// Marker 1
DataTable table;
DataRow newRow;
table = data.Tables["Employees"];
newRow = table.NewRow();
newRow["FirstName"] = "Norman";
newRow["LastName"] = "Blake";
table.Rows.Add(newRow);
// bind the second grid to the new data
DataGrid2.DataSource = table;
DataGrid2.DataBind();
// ---------------------------------------------------------
// Marker 2
DataRow[] selectedRows;
// find the row to change
selectedRows = table.Select("FirstName='Margaret' AND LastName='Peacock'");
selectedRows[0]["FirstName"] = "John";
selectedRows[0]["LastName"] = "Hartford";
// bind the third grid to this new data
DataGrid3.DataSource = table;
DataGrid3.DataBind();
// ---------------------------------------------------------
// Marker 3
// The Rows collection is 0 indexed, therefore
// this deletes the sixth row
table.Rows[5].Delete();
// bind the fourth grid to the new data
DataGrid4.DataSource = table;
DataGrid4.DataBind();
}
</script>
<html>
<head>
</head>
<body>
<table width="100%">
<tbody>
<tr>
<td>
Original Data</td>
<td>
Data with new Row</td>
<td>
Data with edited Row</td>
<td>
Data with deleted Row</td>
</tr>
<tr>
<td valign="top">
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
</td>
<td valign="top">
<asp:DataGrid id="DataGrid2" runat="server"></asp:DataGrid>
</td>
<td valign="top">
<asp:DataGrid id="DataGrid3" runat="server"></asp:DataGrid>
</td>
<td valign="top">
<asp:DataGrid id="DataGrid4" runat="server"></asp:DataGrid>
</td>
</tr>
</tbody>
</table>
</body>
</html>
-------------------------------------------------------------
|