Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics 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 June 21st, 2006, 08:38 AM
Authorized User
 
Join Date: May 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default Can't update!!

Hello!

I'm trying to set an edit button on my page but I can't get it working.
Could someone tell me what am I doing wrong?

(sorry for the size)

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDB" %>
  <%@ Page Language="VB" Debug="true" %>

<html>

<script language="VB" runat="server">

    Sub Page_Load(Sender As Object, E As EventArgs)
        If Not Request.QueryString("id") Is Nothing

                Dim DS As DataSet
                Dim MyConnection As OleDBConnection
                Dim MyCommand As OleDBDataAdapter

                Dim Query As String
                Query = "select * from Tabela where id = @id"

                MyConnection = New OleDBConnection("Provider=Microsoft.Jet.OLEDB.4.0; " & "Data Source=C:\Inetpub\wwwroot\DOT\pessoal\ECO.PATROL - PESSOAL.mdb")
                MyCommand = New OleDBDataAdapter(Query, MyConnection)

                Dim param As OleDBParameter = New OleDBParameter("@id", DbType.String)
                param.Value = Server.HtmlEncode(Request.QueryString("id"))
                myCommand.SelectCommand.Parameters.Add(param)

                DS = New DataSet()
                MyCommand.Fill(DS, "Tabela")

                MyDataList.DataSource = DS.Tables("Tabela").DefaultView
                MyDataList.DataBind()
            End If

    End Sub

    Sub MyDataList_Edit(Sender As Object, E As DataListCommandEventArgs)

        MyDataList.EditItemIndex = CInt(e.Item.ItemIndex)

                MyDataList.DataBind()
    End Sub

    Sub MyDataList_Update(Sender As Object, E As DataListCommandEventArgs)

        Dim MyConnection as New OleDBConnection("Provider=Microsoft.Jet.OLEDB.4.0; " & "Data Source=C:\Inetpub\wwwroot\DOT\pessoal\ECO.PATROL - PESSOAL.mdb")
    Dim MyCommand As New OleDBDataAdapter("update Tabela set nacionalidade=@nacionalidade where id=@id", MyConnection)

        Dim EditText As HtmlInputText
        EditText = E.Item.FindControl("edit")

        Message.InnerHtml = "Actualização efectuada: " & EditText.Value
        MyDataList.EditItemIndex = -1

                MyDataList.DataBind()
    End Sub

    Sub MyDataList_Cancel(Sender As Object, E As DataListCommandEventArgs)

        MyDataList.EditItemIndex = -1
                MyDataList.DataBind()
    End Sub

</script>

<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">



  <form runat="server">

    <table>
      <tr>
        <td valign="top">

          <ASP:DataList id="MyDataList" OnEditCommand="MyDataList_Edit" OnUpdateCommand="MyDataList_Update" OnCancelCommand="MyDataList_Cancel" runat="server">

              <ItemTemplate>

                <table cellpadding=10 style="font: 10pt verdana">
                  <tr>
                    <td width=1 bgcolor="BD8672"/>
                    <td valign="top">
                      <IMG alt="foto" SRC='fotos/<%# DataBinder.Eval(Container.DataItem, "fotografia") %>' width="100" Border="1"><br>
                    </td>
                    <td valign="top">
                      <b>Nome: </b><%# DataBinder.Eval(Container.DataItem, "nome") %><br>
                      <b>Morada: </b><%# DataBinder.Eval(Container.DataItem, "morada") %><br>
                      <b>Nacionalidade: </b><%# DataBinder.Eval(Container.DataItem, "nacionalidade") %><br>
             <b>Número: </b><%# DataBinder.Eval(Container.DataItem, "numero") %><br>
            <asp:linkbutton CommandName="Edit" runat="server">
                <img alt="edit book" border="0" src="images/edit_book.gif" >
              </asp:linkbutton>
                    </td>
                  </tr>
                </table>

              </ItemTemplate>

      <EditItemTemplate>

        <table cellpadding=10 style="font: 10pt verdana">
          <tr>
            <td width=1 bgcolor="BD8672"/>
            <td valign="top">
              <b>Title: </b><%# DataBinder.Eval(Container.DataItem, "nome") %><br>
              <b>Category: </b><%# DataBinder.Eval(Container.DataItem, "morada") %><br>
              <b>Publisher ID: </b><%# DataBinder.Eval(Container.DataItem, "numero") %><br>
              <b>Price: </b><input id="edit" type="text" value='<%# DataBinder.Eval(Container.DataItem, "nacionalidade") %>' runat="server"/>
              <p>
              <asp:linkbutton CommandName="Update" runat="server"><img alt="update" border="0" src="images/update_book.gif" ></asp:linkbutton>
              <asp:linkbutton CommandName="Cancel" runat="server"><img alt="cancel" border="0" src="images/cancel_book.gif" ></asp:linkbutton>
            </td>
          </tr>
        </table>

      </EditItemTemplate>

          </ASP:DataList>

        </td></tr>

    </table>



  </form>
  <div style="font: 10pt verdana;padding:0,15,15,15" id="Message" runat="server"/>



</body>
</html>


 
Old June 21st, 2006, 12:23 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

You didn't mention what the problem or symtoms are???

I just use this code:

<asp:Button ID="Button1" Text="Update" OnClick="UpdateDate" Runat="server" />

The onclick value points to the actual sub that does the updating.


 
Old June 22nd, 2006, 03:57 AM
Authorized User
 
Join Date: May 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hallo!

I want to let the user edit a record. Right now the code doesn't update the DB, keeping the same old values and I got no errors.

If u would mind the find the errors and explain me what the problem is like if I was really dum... LOL.

Thanks a lot...

 
Old June 22nd, 2006, 10:52 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

I don't see your link button doing anything. There's no sub that handles the action of the button. In my example there is an onclick event associated with the button. However, you can also accomplish this in the code behind.

Protected WithEvents btnUpdate As Button

Sub getComponentGroups(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
'Put your update code in here.
    End Sub

Also, I don't see an ID="btnUpdate" in your code either, to identify your button. I'm not an expert. Hope this helps.

 
Old June 22nd, 2006, 12:06 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

The linkbuttons do not have a click event assoiated with them, his buttons SHOULD trigger the sub routines defined in his datagrid On[Command] properties.

"The one language all programmers understand is profanity."
 
Old June 26th, 2006, 07:15 AM
Authorized User
 
Join Date: May 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey!

But shouldn't the CommandName="Update" and
Sub MyDataList_Update... be enough?

I'm learning from here:
http://www.asp.net/QuickStart/util/s.../DataList9.src

Thanx in advance...

 
Old June 26th, 2006, 07:59 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Do you know for sure that MyDataList_Update is being called? e.g., Have you stepped through the code to make sure that sub is being called, or put a Response.write to write some text out to the screen?

This will at least narrow the problem down to: is it a problem with the datagrid or is it a problem with the routine.

Hth.

"The one language all programmers understand is profanity."
 
Old June 26th, 2006, 10:38 AM
Authorized User
 
Join Date: May 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm using the following code:
  <div style="font: 10pt verdana;padding:0,15,15,15" id="Message" runat="server"/>
and I get the new value printes but the record keeps the old value.

I've made the following changes in the Update procedure:
(I think I mixing up everything...)
    Sub MyDataList_Update(Sender As Object, E As DataListCommandEventArgs)

    Dim Conn as OleDbConnection
        Dim MyConnection as String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Inetpub\wwwroot\DOT\pessoal\bd1.mdb"
        Dim SQL as String = "update Tabela set numero=@numero"
    Dim Cmd as OleDbCommand

        Dim EditText As HtmlInputText
        EditText = E.Item.FindControl("edit_price")
    conn = New OleDbConnection(MyConnection)
    Cmd = New OleDbCommand(SQL, conn)
    Cmd.Parameters.Add(New OleDbParameter("@numero", EditText.value))

    Conn.Open()
    cmd.ExecuteNonQuery()
        MyDataList.EditItemIndex = -1
        Message.InnerHtml = "Price Updated: " & EditText.Value
    Conn.close
        PopulateList()
    End Sub

...and now I get the error (translated):

"The operation must use an updatable query"

Suggestions?







Similar Threads
Thread Thread Starter Forum Replies Last Post
update multiple columns in an update statement debbiecoates SQL Server 2000 1 August 17th, 2008 04:01 AM
Update FT BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 September 20th, 2007 12:34 AM
Update link doesn't update in FormsView shaly ASP.NET 2.0 Basics 0 December 6th, 2006 04:33 PM
Datagrid.update() and DataAdapter.Update aarunlal ASP.NET 2.0 Professional 2 February 23rd, 2006 11:41 PM
cant update coolerbob VB.NET 2002/2003 Basics 2 February 27th, 2004 09:25 AM





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