locked
I can't delete Record <pages enableEventValidation="true"/> RRS feed

  • Question

  • User-909867351 posted

    Hi

    I need to delete one recor but got the error  

    <pages enableEventValidation="true"/>

    Here is my code:

     protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txtData.Text = DateTime.UtcNow.ToString("yyyy-MM-dd");
                SqlDataSource1.SelectParameters.Clear();
                SqlDataSource1.SelectCommand = "select * from correspondencia where data=?";
                SqlDataSource1.SelectParameters.Add("@a", txtData.Text);
                SqlDataSource1.DataBind();
                GridView1.DataBind();
            }
            else
            {
                SqlDataSource1.SelectParameters.Clear();
                SqlDataSource1.SelectCommand = "select * from correspondencia where data=?";
                SqlDataSource1.SelectParameters.Add("@a", txtData.Text);
                SqlDataSource1.DataBind();
                GridView1.DataBind();
            }
            
           
        }
    
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName != "Delete") return;
            GuardaId.Value = e.CommandArgument.ToString();       
            SqlDataSource1.DeleteParameters.Clear();
            SqlDataSource1.DeleteCommand = "delete from correspondencia where id=?";
            SqlDataSource1.DeleteParameters.Add("@a", GuardaId.ToString());
            SqlDataSource1.Delete();
           
        }

    What is wrong with this code? If I write <pages enableEventValidation="false"/> the button des not work therefore I have to use <pages enableEventValidation="true"/>

    but got this error. The GuardaId it's a HiddenField

    Thank you

    Sunday, September 23, 2018 7:15 PM

All replies

  • User-1716253493 posted

    don't change anything in sqlds commands or parameters, simply set default value or change the control parameter

    For example

    hiddenfieldid.value="123";
    sqldatasource1.delete();

    or

    SqlDataSource1.DeleteParameters["id"].DefaultValue = "123";
    SqlDataSource1.Delete();

    If you want another delete command then use SqlDataSource2, for example delete deferent table records

    Changing commands in sqldatasource can make headache, if you want deferent select result you can do simple way

    txtdata.text = "abc";
    grid.DataBind();

    Monday, September 24, 2018 1:19 AM
  • User-893317190 posted

    Hi mariolopes,

    If you use sqldatasource, the gridview will be bound by default , so you don't need to bind your gridview manually.

    Please remove the code  GridView1.DataBind(); and the exception should not happen.

    Or if you want to specify the where clause of your select sql, you could config your sqldatasource.

    Please click the where option ,select your column and your control id. Finally please click the Add button.

    Best regards,

    Ackerly Xu

    Monday, September 24, 2018 5:22 AM
  • User-909867351 posted

    HI 

    I need the 

    GridView1.DataBind();  oterwhise I can't see the messages to delete.

    No luck oned_gk

    got the same error with your suggestion

    No luck. I try to use another datasource to delete but got the same problem

    Monday, September 24, 2018 8:55 AM
  • User-893317190 posted

    Hi mariolopes,

     The gridview will be bound by default, that is to say, the gridview's DataBind method will still be called , even you don't write the code GridView1.DataBind();.

    The data you choose with the code

      else
            {
                SqlDataSource1.SelectParameters.Clear();
                SqlDataSource1.SelectCommand = "select * from correspondencia where data=?";
                SqlDataSource1.SelectParameters.Add("@a", txtData.Text);
                SqlDataSource1.DataBind();
                GridView1.DataBind();
            }

     will  also show even you don't write GridView1.DataBind().

    So I couldn't know clearly about  what  "I can't see the messages to delete" means.

    Best regards,

    Ackerly Xu

    Monday, September 24, 2018 9:54 AM
  • User-909867351 posted

    Hi 

    Please can you go to

    https://correspondencia.programamos.pt/default.aspx

    If I change the date I'll see the messages of that day.

    Try to delete one record and will get the error.

    If you don't do the gridview1.databind() and change the date nothing happens therefore I need to gridview1.databind() 

    Monday, September 24, 2018 10:12 AM
  • User-909867351 posted

    Found it.

    First 

      protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txtData.Text = DateTime.UtcNow.ToString("yyyy-MM-dd");
                SqlDataSource1.SelectParameters.Clear();
                SqlDataSource1.SelectCommand = "select * from correspondencia where data=?";
                SqlDataSource1.SelectParameters.Add("@popo", txtData.Text);
                SqlDataSource1.DataBind();
                GridView1.DataBind();
            }
            else
            {        
                    SqlDataSource1.SelectParameters.Clear();
                    SqlDataSource1.SelectCommand = "select * from correspondencia where data=?";
                    SqlDataSource1.SelectParameters.Add("@ripas", txtData.Text);
                    SqlDataSource1.DataBind();
                                                    
            }               
        }

    But in this case when I change the date nothing happens, therefore I have to do the following:

    protected void txtData_TextChanged(object sender, EventArgs e)
        {
            SqlDataSource1.SelectParameters.Clear();
            SqlDataSource1.SelectCommand = "select * from correspondencia where data=?";
            SqlDataSource1.SelectParameters.Add("@ripas", txtData.Text);
            SqlDataSource1.DataBind();
            GridView1.DataBind();
        }

    Monday, September 24, 2018 11:11 AM