PDA

Click to See Complete Forum and Search --> : Could someone help to debug?


stellafung2000
Aug 8th, 2004, 07:30 PM
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. It can display, edit and delete the data which is in Mircosoft Access format.

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:DataGrid 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:DataGrid><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 />&nbsp;<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

pvb
Aug 14th, 2004, 09:30 AM
Well, prolly not gonna get too many volunteers to debug all of your code for ya. Out of curiousity could you post the book title? i might have the same book. DataGridGirl (http://www.datagridgirl.com) is a good site for everything you ever wanted to know about datagrids.

stellafung2000
Aug 16th, 2004, 06:29 PM
Hi pvb,

Yes, the book's name is SAMS Teach Yourself ASP.NET in 21 days by Chris Payne.


I am now studing up to chapter 13 and I find some coding are wrong therefore the samples cannot work out. And because of that it cause me a lot of time to see if that is my typing mistake.

I have search for a lot of helping in internet, including send email to the publisher, but no reply.

Also this book does not come with CD too. Make it very difficult to study. I have to key in the code before I can see the result. Not very helpful.

But it is a good book to let the ASP developer to change to ASP.NET.

It said this book is for both C# and VB developer but I find out that most of the script is for VB. A bit disappointed with this.

Hope that you can give me some good suggestion.

Thank you

Stella

pvb
Aug 16th, 2004, 08:03 PM
What doesn't work? how far have you gotten in getting it to work? post some specific error messages or conditions you're getting. Did the DataGridGirl site give you any ideas on what could be wrong with your code?

stellafung2000
Aug 17th, 2004, 07:29 PM
Hi pvb,

This aspx looks OK. But when you want to Delete or Update the data, the error will come out and not allow you to carry on.

I had visited the web site you mentioned. Is it your website? I think I am too beginner to understand some of them. But certainly will go back again. Thanks.

Regards

Stella

nemaroller
Aug 18th, 2004, 06:47 AM
You have to post the error message it is giving you, and providing the line number it references would speed along the process.

stellafung2000
Aug 18th, 2004, 06:23 PM
Hi nemaroller,

There is no error message comes out. But when you click on Update button and modify the data, the data still can't change.

This aspx file can Update, Delete, Cancel and Add the data in the data file.

I can upload the data file for you to download and try this aspx file but the dial out to server has problem at the moment. I can do it later on if you need it.

Thanks

Stella

stellafung2000
Aug 18th, 2004, 07:53 PM
Hi nemaroller,

The data file can be download from here:


http://users.tpg.com.au/stella_f/banking.mdb


Thanks

Stella