Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating is not supported by data source unless UpdateCommand is specified

Tags:

c#

asp.net

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 an error:

Updating is not supported by data source unless UpdateCommand is specified.

How can I suppress this error message?

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???
       }
    }
}
like image 924
Jaggu Avatar asked Nov 25 '25 23:11

Jaggu


1 Answers

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.

like image 195
Tim Avatar answered Nov 27 '25 13:11

Tim