You must Sign In to post a response.
  • Category: ASP.NET

    How to stop/cancel query from cancel button of asp.net web form

    I have one web form having 2 button show and cancel.In show button click event I am showing cancel button and executing query,If query take long time.I want to cancel that query from cancel button.I searched lot, sqlcommand.cancel() method may need to use.How to use that because in button1 writing like this

    Button1_click()
    {
    String connectionString = WebConfigurationManager.ConnectionStrings["smitaConnectionString"].ConnectionString;
    String sqlselect = "select PName from Item";
    SqlConnection con = new SqlConnection(connectionString);
    try
    {
    con.Open();
    SqlCommand cmd = new SqlCommand(sqlselect, con);

    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(ds);

    GridView1.DataSource = ds;
    GridView1.DataBind();

    }
    catch (Exception ex)
    {
    ex.ToString();
    }
    finally
    {
    con.Close();
    }
    }
  • #765908
    Hi,
    Instead of declaring SqlCommand cmd inside Button1_Click() event, declare it globally so that it will be accessible to you on both the buttons.

  • #765956
    declare the following command object as public.

    public SqlCommand cmd;

    Now you can access this command object from both buttons "Execute" and "Cancel".
    Run the query execution in the thread. Now you can control the thread and the query using your cancel button.
    I hope you got my point

    By Nathan
    Direction is important than speed


  • Sign In to post your comments