|
-
Aug 4th, 2004, 11:52 PM
#1
Thread Starter
Lively Member
Please help to find bugs!
Hi,
The following aspx file (ASP.NET in C#) is copy out from a text book but it had bugs in it and can't work.
Could someone help to tell me where the bugs are? Unfortunately, the writer is not responsible for this. He just published the book without leaving any help website to follow.
The file is like this:
----------
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:\\inetpub\\wwwroot\\tyaspnet21days\\data\\banking.mdb");
void Page_Load(Object Sender, EventArgs e) {
if (!Page.IsPostBack) {
FillDataGrid();
}
}
void Submit(Object Sender, EventArgs e) {
int i, j;
string[] paramso = new string [7];
string strText;
bool blnGo = true;
j = 0;
for ( i = 0; i <= AddPanel.Controls.Count - 1; i++) {
if (AddPanel.Controls[i].GetType() == typeof(TextBox)) {
strText = ((TextBox)AddPanel.Controls[i]).Text;
if (strText != ""){
paramso[j] = strText;
} else {
blnGo = false;
lblMessage.Text += "You forgot to enter a value for " + AddPanel.Controls[i].ID.ToString() + "<p>";
lblMessage.Style["ForeColor"] = "Red";
}
j++;
}
}
if (!blnGo) {
return;
}
string strSQL = "INSERT INTO tblUsers (FirstName, LastName, Address, City, State, Zip, Phone) VALUES (" +
"'" + paramso[0] + "'," +
"'" + paramso[1] + "'," +
"'" + paramso[2] + "'," +
"'" + paramso[3] + "'," +
"'" + paramso[4] + "'," +
"'" + paramso[5] + "'," +
"'" + paramso[6] + "')";
ExecuteStatement(strSQL);
FillDataGrid();
}
void dgData_Edit(Object Sender, DataGridCommandEventArgs e) {
FillDataGrid(e.Item.ItemIndex);
}
void dgData_Delete(Object Sender, DataGridCommandEventArgs e) {
string strSQL = "DELETE FROM tblUsers " +
"WHERE UserID = " + (e.Item.ItemIndex + 1).ToString();
ExecuteStatement(strSQL);
FillDataGrid();
}
void dgData_Update(Object Sender, DataGridCommandEventArgs e) {
// if (UpdateDataStore) {
FillDataGrid(-1);
// }
}
void dgData_Cancel(Object Sender, DataGridCommandEventArgs e) {
FillDataGrid(-1);
}
void dgData_PageIndexChanged(Object Sender, DataGridPageChangedEventArgs e) {
dgData.DataBind();
}
bool UpdateDataStore(DataGridCommandEventArgs e) {
int i, j;
string[] paramso = new string[7];
string strText;
bool blnGo = true;
j = 0;
for (i = 1; i <= e.Item.Cells.Count - 3; i++) {
strText = ((TextBox)e.Item.Cells[i].Controls[0]).Text;
if (strText != ""){
paramso[j] = strText;
j++;
} else {
blnGo = false;
lblMessage.Text += "You forgot to enter " +
"a value<p>";
}
}
if (!blnGo) {
return false;
}
string strSQL = "UPDATE tblUsers SET " +
"FirstName = '" + paramso[0] + "'," +
"LastName = '" + paramso[1] + "'," +
"Address = '" + paramso[2] + "'," +
"City = '" + paramso[3] + "'," +
"State = '" + paramso[4] + "'," +
"Zip = '" + paramso[5] + "'," +
"Phone = '" + paramso[6] + "'" +
" WHERE UserID = " + ((Label)e.Item.Cells[0].Controls[1]).Text;
ExecuteStatement(strSQL);
return blnGo;
}
void FillDataGrid() {
FillDataGrid(-1);
}
void FillDataGrid(int EditIndex) {
OleDbCommand objCmd = new OleDbCommand("select * from tblUsers", Conn);
OleDbDataReader objReader;
try {
objCmd.Connection.Open();
objReader = objCmd.ExecuteReader();
dgData.DataSource = objReader;
if (!EditIndex.Equals(null)) {
dgData.EditItemIndex = EditIndex;
}
dgData.DataBind();
objReader.Close();
objCmd.Connection.Close();
}catch(Exception ex) {
lblMessage.Text = "Error retrieving from the database. Please" +
" make sure all values are correctly input";
}
}
void ExecuteStatement(String strSQL) {
OleDbCommand objCmd = new OleDbCommand(strSQL, Conn);
try {
objCmd.Connection.Open();
objCmd.ExecuteNonQuery();
} catch(Exception ex) {
lblMessage.Text = "Error updating the database. Please" +
" make sure all values are correctly input";
}
objCmd.Connection.Close();
}
</script>
<html>
<body>
<asp:Label ID="lblMessage" runat="server" />
<form runat="server">
<asp ataGrid id="dgData" runat="server"
BorderColor="black" GridLines="Vertical"
cellpadding="4" CellSpacing="0" Width="100%"
AutoGenerateColumns="false"
OnDeleteCommand="dgData_Delete"
OnEditCommand="dgData_Edit"
OnCancelCommand="dgData_Cancel"
OnUpdateCommand="dgData_Update"
OnPageIndexChanged="dgData_PageIndexChanged" >
<Columns>
<asp:TemplateColumn HeaderText="ID">
<ItemTemplate>
<asp:Label id="Name" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.UserID") %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="Firstname" DataField="FirstName" />
<asp:BoundColumn HeaderText="Lastname" DataField="LastName" />
<asp:BoundColumn HeaderText="Address" DataField="Address" />
<asp:BoundColumn HeaderText="City" DataField="City" />
<asp:BoundColumn HeaderText="State" DataField="State" />
<asp:BoundColumn HeaderText="Zip" DataField="Zip" />
<asp:BoundColumn HeaderText="Phone" DataField="Phone" />
<asp:EditCommandColumn
EditText="Edit"
CancelText="Cancel"
UpdateText="Update"
HeaderText="Edit" />
<asp:ButtonColumn HeaderText="" Text="Delete" CommandName="delete" />
</Columns>
</asp ataGrid><p>
<asp:Panel ID="AddPanel" runat="server">
<table>
<tr>
<td width="100" valign="top">First and last name:</td>
<td width="300" valign="top"><asp:TextBox id="tbFName" runat="server" />
<asp:TextBox ID="tbLName" runat="server" /></td>
</tr>
<tr>
<td valign="top">Address:</td>
<td valign="top"><asp:TextBox ID="tbAddress" runat="server" /></td>
</tr>
<tr>
<td valign="top">City, State, ZIP:</td>
<td valign="top"><asp:textbox ID="tbCity" runat="server" />,
<asp:TextBox ID="tbState" runat="server" size=2 /> <asp:TextBox ID="tbZIP" runat="server" size=5 />
</td>
</tr>
<tr>
<td valign="top">Phone:</td>
<td valign="top"><asp:TextBox ID="tbPhone" runat="server" size=11 /></td>
</tr>
<tr>
<td colspan="2" align="right" valign="top"><asp:Button ID="btSubmit" runat="server" Text="Add" OnClick="Submit" /></td>
</tr>
</table>
</asp:Panel>
</form>
</body>
</html>
------------
Thanks for your help
Stella
-
Aug 6th, 2004, 04:33 PM
#2
Sleep mode
You may get better responses in ASP.NET forum .
-
Aug 8th, 2004, 07:27 PM
#3
Thread Starter
Lively Member
Thanks Pirat,
I will do it now.
Stella
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|