Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 29th, 2005, 11:15 AM
Authorized User
 
Join Date: Aug 2005
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default Cannot update datagrid

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" />&nbsp;
            <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>
 
Old August 29th, 2005, 10:57 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Try changing this line:
  if UpdateDataStore then
To:
  if UpdateDataStore(e) then

How you are not getting a compiler error on this or a blue line is beyond me. Try it out and see how it goes ..
Jim







Similar Threads
Thread Thread Starter Forum Replies Last Post
Update a datagrid debbiecoates Beginning VB 6 0 January 4th, 2008 05:51 AM
datagrid update sumith ASP.NET 1.0 and 1.1 Basics 0 February 9th, 2007 02:14 AM
Datagrid.update() and DataAdapter.Update aarunlal ASP.NET 2.0 Professional 2 February 23rd, 2006 11:41 PM
Datagrid Update madhavigujja ASP.NET 1.0 and 1.1 Basics 1 December 18th, 2005 03:10 PM
Datagrid Does Not Update ... wecka .NET Framework 2.0 1 March 28th, 2005 04:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.