locked
Sub Update() not working RRS feed

  • Question

  • User-1310354684 posted

    Hi everyone!

    I have a asp:imagebutton and in the onclick I want to update a field in my access db.

    But is not working.

    I create a test.aspx with the simple code I want, that is working:

     

     <script runat="server">

    Dim StrCon As String = ConfigurationSettings.AppSettings("strconexao")    
    Dim Con As OleDbConnection
    Dim Cmd As OleDbCommand
    Dim Dtr As OleDbDataReader
    Dim Adpt as OleDbDataAdapter
    Dim Cmb As OleDbCommandBuilder
    Dim Dset As DataSet
    Dim Drw As Datarow

    Sub Update()
        
      Con = New OleDbConnection(StrCon)    
      Cmd = New OleDbCommand("update TabEntrevistado set EntrevistadoConcluido=false where EntrevistadoCodigo = 8", Con) 
      Con.Open()
      Dtr = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
      Con.Close()
        

    End Sub

    Sub Page_Load()
      Update()
    End Sub

    </script>

     

    So I copy this code to my aspx page to a sub and is not working....

    my page.aspx:

     

    in body section inside a datalist:

    <asp:ImageButton runat="server" ID="BtnCancelar" ImageUrl="images/btn_cancelar.png" AlternateText="Retirar Agendamento" BorderWidth="0" Width="20" Height="20" OnClick="Update"></asp:ImageButton>

    in code:

    <script runat="server">

    Dim StrCon As String = ConfigurationSettings.AppSettings("strconexao")    
    Dim Con As OleDbConnection
    Dim Cmd As OleDbCommand
    Dim Dtr As OleDbDataReader
    Dim Adpt as OleDbDataAdapter
    Dim Cmb As OleDbCommandBuilder
    Dim Dset As DataSet
    Dim Drw As Datarow

    Sub BindDataList()
        
      Con = New OleDbConnection(StrCon)
      Cmd = New OleDbCommand("SELECT TabEntrevistado....", Con) 
      Con.Open()
      Dtr = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
      ListaPendentes.DataSource = Dtr
      ListaPendentes.DataBind()
      Con.Close()
        
    End Sub
     
    Sub Update(sender As Object, e As ImageClickEventArgs)
      Con = New OleDbConnection(StrCon)
      Cmd = New OleDbCommand("update TabEntrevistado set EntrevistadoConcluido=true where EntrevistadoCodigo = 8", Con) 
      Con.Open()
      Dtr = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
      ListaPendentes.DataSource = Dtr
      ListaPendentes.DataBind()
      Con.Close()

    End Sub
     


    Sub Page_Load()
      BindDataList()
    End Sub

    </script>

     

    About the error:

    Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration ......

    What I made:

    in the webconfig I wrote in system section: <pages enableEventValidation="false" /> and the error disapear, but now the sub update don't update anything and not showing my data list after click in the button...

     

     

    WHAT'S WRONG????????????

     

    Thanks and sorry my english :)

    Thursday, January 6, 2011 1:00 PM

Answers

  • User-1199946673 posted

    ExecuteReader returns a datareader and sgould be used with Select queries. Action queries (Update, Delete, Insert) should be executed using the ExecuteNonQuery method

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 6, 2011 2:06 PM

All replies

  • User-1199946673 posted

    ExecuteReader returns a datareader and sgould be used with Select queries. Action queries (Update, Delete, Insert) should be executed using the ExecuteNonQuery method

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 6, 2011 2:06 PM
  • User-1310354684 posted

     silly i am... you right

    simply put BindDataList() and it works fine :D

    in my case:

     

    Sub Cancela(sender As Object, e As ImageClickEventArgs)
      Con = New OleDbConnection(StrCon)
      Cmd = New OleDbCommand("update TabEntrevistado set EntrevistadoConcluido=true where EntrevistadoCodigo =" + LblInfoEditar.text, Con) 
      Con.Open()
      Dtr = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
      ListaPendentes.DataSource = Dtr
      ListaPendentes.DataBind()
      BindDataList()
      Con.Close()

    End Sub

    Thursday, January 6, 2011 2:14 PM
  • User-1199946673 posted

    I think you should read my previous reply again! 

    Thursday, January 6, 2011 2:25 PM
  • User-1310354684 posted

    OK

    I will try ExecuteNonQuery

    I only try ExecuteReader but you right when I made a insert or delete, I remember to use ExecuteNonQuery so it's logic that I have to use here this.

    Many thanks

    I will try now.

    Thursday, January 6, 2011 3:18 PM