3

I want to conditionally call update statement on sqldatasource. I have a gridview and sqldatasource assigned to it. I handle gridview_updating event. If I do not assign sqldatasource.updatecommand to it (when some condition does not meet), I get error: "Updating is not supported by data source unless UpdateCommand is specified."

How can I suppress this error message?

Thanks in advance:)

EDIT:

This is my command event of gridview:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {


            if (e.CommandName == "Update")
            { 
               if(someconditionmatch)
                  sqldatasource1.UpdateCommand = "some update query";
               else
               {
                   //do nothing...but when do do nothing...it throws error that some update command has to compulsorily assigned...This is my problem...which updateCommand should I assign if I do not want to update anything???
               }
            }
        }
4
  • Why not assign the UpdateCommand and handle the conditional updating in the GridView_Updating event? I.e., decide whether or not to update in the event handler based on whatever your criteria is/are?
    – Tim
    Jul 28, 2011 at 6:11
  • @Tim: I am alredy handling the GridView_Updating event. But I do not specify UpdateCommand inside it I get the error. I hope I am clear. I have to compulsorily assign UpdateCommand to SqlDataSource else I get this error.
    – Jaggu
    Jul 28, 2011 at 6:13
  • I'm not entirely clear. Can you post the relevant code and markup?
    – Tim
    Jul 28, 2011 at 6:15
  • @Tim: Please read my updated post. Let me know if you are still not clear.
    – Jaggu
    Jul 28, 2011 at 6:19

1 Answer 1

7

Instead of handling this in the RowCommand event handler, you might try it in the RowUpdating event handler, because you can set the Cancel property to true:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
   if(someconditionmatch)
       sqldatasource1.UpdateCommand = "some update query";
   else
       e.Cancel = true; //Cancels the impending update.
}

I don't know if you have to have the UpdateCommand with some value in the RowCommand event handler - if you do, you can always set the UpdateCommand to the one you use for the condition (assuming you have only one update command) in the RowCommand event handler (or the markup for the SqlDataSource), and then do the RowUpdating like this:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
   if (!someconditionmatch)
       e.Cancel = true;
}

Hopefully the above will help, or at least you get you pointed in the right direction.

2
  • Excellent. I want to insert or update based upon some condition and your suggestion to do some query and then e.Cancel is superb +1
    – Moons
    Jan 15, 2014 at 16:14
  • Why e.Cancel = true keep gridview in Edit mode only? how to make it normal Labels. Nov 29, 2017 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.