Hello all,
I am new to the world of ASP.Net. It has been a strong learning curve for me. I am in the process of making a Datagrid. However I get an error message that reads on line 126:
Compiler Error Message: BC30451: Name 'e' is not declared.
Source Error:
Line 124: "Zip = '" & params (5) & "'," & _
Line 125: "Phone = '" & params (6) & "'" & _
Line 126: "WHERE UserID = " & Ctype(e.Item.Cells(0).Controls(1),Label).text
Line 127:
Line 128: ExecuteStatement(strSQL)
Can anyone please help me. I do not know how to fix this. Here is the code I used:
Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
'Declare Connection
dim Conn as new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\Clients\navigatir\navigatir.com\www\aspnet\banking.mdb")
sub Page_Load(Sender as object, e as EventArgs)
if Not Page.IsPostback then
FillDataGrid()
end if
end sub
sub Submit(Sender as object, e as eventargs)
'Insert New Data
dim i, j as integer
dim params(7) as string
dim strText as string
dim blnGo as boolean = true
j=0
for i=0 to AddPanel.Controls.Count - 1
if AddPanel.Controls(i).GetType Is _
GetType(TextBox) then
strText = Ctype(AddPanel.Controls(i),TextBox).Text
if strText<> "" then
params(j) = strText
else
blnGo = false
lblMessage.Text =lblMessage.Text & _
"You forgot to enter a value for " & _
AddPanel.Controls(i).ID & "<p>"
lblMessage.Style("ForeColor") ="Red"
end if
j = j+1
end if
next
if not blnGo then
exit sub
end if
dim strSQL as string = "INSERT INTO yblUsers " & _
"(FirstName, LastName, Address, City, State, " & _
"Zip, Phone) Values (" & _
"'" & params (0) & "'," & _
"'" & params (1) & "'," & _
"'" & params (2) & "'," & _
"'" & params (3) & "'," & _
"'" & params (4) & "'," & _
"'" & params (5) & "'," & _
"'" & params (6) & "')"
ExecuteStatement(strSQL)
FillDataGrid()
end sub
sub dgData_Edit(Sender as object, e as DataGridCommandEventArgs)
FillDataGrid(e.Item.ItemIndex)
end sub
sub dgData_Delete(Sender as object, e as DataGridCommandEventArgs)
dim strSQL as string = "DELETE FROM tblUsers " & _
"WHERE UserID = " & e.Item.ItemIndex + 1
ExecuteStatement(strSQL)
FillDataGrid()
end sub
sub dgData_Update(Sender as object, e as DataGridCommandEventArgs)
if UpdateDataStore then
FillDataGrid(-1)
end if
end sub
sub dgData_Cancel(Sender as object, e as DataGridCommandEventArgs)
FillDataGrid(-1)
end sub
sub dgData_PageIndexChanged(Sender as object, e as DataGridPageChangedEventArgs)
dgData.DataBind()
end sub
function UpdateDataStore(e as DataGridCommandEventArgs) as boolean
dim i, j as integer
dim params(7) as string
dim strText as String
dim blnGo as boolean = true
j=0
for i=1 to e.Item.Cells.Count -3
strText = Ctype(e.Item.Cells(i).Controls(0), Textbox).Text
if strText<>"" then
params(j) = strText
j = j +1
else
blnGo = false
lblMessage.Text = lblMessage.Text & _
"You Forgot to Enter a value<p>"
end if
next
if not blnGo then
return false
exit function
end if
dim strSQL as string = "UPDATE tblUSers SET " & _
"FirstName = '" & params(0) & "'," & _
"LastName = '" & params(1) & "'," & _
"Address = '" & params(2) & "'," & _
"City = '" & params(3) & "'," & _
"State = '" & params(4) & "'," & _
"Zip = '" & params(5) & "'," & _
"Phone = '" & params(6) & "'" & _
"WHERE UserID = " & Ctype(e.Item.Cells(0).Controls(1), Label).text
ExecuteStatement(strSQL)
return blnGo
end function
sub FillDataGrid(Optional EditIndex as integer = -1)
'Open Connection
dim objCmd as new OleDbCommand _
("select * from tblUsers", Conn)
dim objReader as OleDbDataReader
try
objCmd.Connection.Open()
objReader = objCmd.ExecuteReader()
catch ex as Exception
lblMessage.Text = "Error retrieving from the database."
end try
dgData.DataSource = objReader
if not EditIndex.Equals(Nothing) then
dgData.EditItemIndex = EditIndex
end if
dgData.Databind()
objReader.Close
objCmd.Connection.Close()
end sub
function ExecuteStatement(strSQL)
dim objCmd as new OleDbCommand(strSQL, Conn)
try
objCmd.Connection.Open()
objCmd.ExecuteNonQuery()
catch ex as Exception
lblMessage.Text= "Error updating the Database."
end try
objCmd.Connection.Close()
end function
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Listing1010</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<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='<%# 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>
<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" size=2 runat="server" />
<asp:textbox ID="tbZip" size=5 runat="server" />
</td>
</tr>
<tr>
<td valign="top">Phone</td>
<td valign="top">
<asp:textbox ID="tbPhone" runat="server" size=11 /><br>
</td>
</tr>
<tr>
<td colspan="2" valign="top" align="right">
<asp:button ID="btSubmit" runat="server" Text="Add" OnClick="Submit" />
</td>
</tr>
</table>
</asp:panel>
</form>
</body>
</html>