UPDATE/DELETE not working in Data Controlshttp://forums.asp.net/t/954933.aspx/1?UPDATE+DELETE+not+working+in+Data+ControlsFri, 01 Jan 2010 14:00:43 -05009549331172520http://forums.asp.net/p/954933/1172520.aspx/1?UPDATE+DELETE+not+working+in+Data+ControlsUPDATE/DELETE not working in Data Controls <p>I've seen many posts related to UPDATE or DELETE not working when using Data Controls like GridView or FormView.&nbsp; To further confuse the issue, no errors occur when this behavior kicks in, so people are left perplexed with little to go on.</p> <p>I suppose there could be other reasons for this behavior, but one of the reason I've recently tracked down is related to your DB Table definition.&nbsp; More specifically, if you have a Column defined that is Nullable, then you may very well face this problem if your DataSource control (i.e. SqlDataSource) is defined to use ConflictDetection where all your column values are compared prior to UPDATE/DELETE operation to ensure that someone else did not make changes just before you.</p> <p>The reason why you UPDATE/DELETE operation may &quot;fail&quot; is because you can't compared Null to Null and expect a logical answer.&nbsp; In many languages and in SQL, the expression (NULL = NULL) returns FALSE.&nbsp; In fact, comparing any value to NULL will always return FALSE, so the auto-generated Conflict Detection enable SQL statements for UPDATE and DELETE operations will fail when involving a Nullable column (with NULL value).</p> <p>For example, following is a sample SQL statement generated with ConflictDetection enabled:</p> <font color="#ff0000" size="2"> <p><font face="Courier New">UpdateCommand</font></font><font color="#0000ff" size="2"><font face="Courier New">=&quot;UPDATE [TableNullable] SET [Col_Nullable] = @Col_Nullable, [Col_Nullable2] = @Col_Nullable2 WHERE [id] = @original_id AND [Col_Nullable] = @original_Col_Nullable AND [Col_Nullable2] = @original_Col_Nullable2&quot;</font></p> </font> <p>Notice that each Column is simply compared (with =) to the &quot;original&quot; column values.&nbsp; Knowing that any comparison to NULL value will result in FALSE, the above WHERE CLAUSE will fail.&nbsp; To correct the problem, you can either change your columns to be Non-Nullable or change the WHERE CLAUSE of your UPDATE/DELETE SQL statements to something similar to below:</p> <p><font face="Courier New"><font color="#ff0000" size="2">UpdateCommand</font><font color="#0000ff" size="2">=&quot;UPDATE [TableNullable] SET [Col_Nullable] = @Col_Nullable, [Col_Nullable2] = @Col_Nullable2 WHERE [id] = @original_id AND ISNULL([Col_Nullable],'') = ISNULL(@original_Col_Nullable,'') AND ISNULL([Col_Nullable2],'') = ISNULL(@original_Col_Nullable2,'')&quot;</p> </font></font> <p>You must keep in mind, however, that this example uses columns that are Character type.&nbsp; You must keep in mind what your column type is when using ISNULL to translate it.</p> <p>Finally, it's a good programming practice to check if your SQL operation went through okay.&nbsp; Problems resulting from above WILL NOT throw an error -- thus the confusion for many people.&nbsp; What you must do is to handle the Updated event for your DataSource control and check for the following (Delete event should be similar):</p> <font size="2"> <p></font><font face="Courier New"><font color="#0000ff" size="2">protected</font><font size="2"> </font><font color="#0000ff" size="2">void</font><font size="2"> SqlDataSource1_Updated(</font><font color="#0000ff" size="2">object</font><font size="2"> sender, </font><font color="#008080" size="2">SqlDataSourceStatusEventArgs</font></font><font size="2"><font face="Courier New"> e)</font></p> <p><font face="Courier New">{</font></p> <p><font face="Courier New">e.AffectedRows; (if 0, then your operation failed)</font></p> <p><font face="Courier New">e.Exception; (check for exceptions here)</font></p> <p><font face="Courier New">e.ExceptionHandled; (set to True when you've handled it)</font></p> <p><font face="Courier New">}</font></p> </font> <p>I hope this post was helpful to some of you that are facing this &quot;simple&quot; and yet elusive issue.&nbsp; Happy programming.</p> <p>&nbsp;</p> 2006-01-18T19:32:32-05:001198642http://forums.asp.net/p/954933/1198642.aspx/1?Re+UPDATE+DELETE+not+working+in+Data+ControlsRe: UPDATE/DELETE not working in Data Controls <font color="#000000" style="background-color:#ffffff"> <p class="MsoNormal" style="margin:0in 0in 0pt">Great! It is working prefect when using SQLDataSource even if the bound field is null, <span style="">&nbsp;</span>but when I use DataSet Table adaptor with ObjectDataSource, It is throwing error </p> <p class="MsoNormal" style="margin:0in 0in 0pt">&nbsp;</p> <p class="MsoNormal" style="margin:0in 0in 0pt"><font color="#a52a2a"><i>ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'Update' that has parameters:</i> </font> any solution for this?</p> <p class="MsoNormal" style="margin:0in 0in 0pt">&nbsp;</p> <p class="MsoNormal" style="margin:0in 0in 0pt">Regards</p> <p class="MsoNormal" style="margin:0in 0in 0pt">Gopinathraja</p> <p></font>&nbsp;</p> 2006-02-15T09:36:03-05:001212844http://forums.asp.net/p/954933/1212844.aspx/1?Re+UPDATE+DELETE+not+working+in+Data+ControlsRe: UPDATE/DELETE not working in Data Controls <p>I also have been wrestling with the same issue described in the previous post. If anyone has any insight as to its reason and solution, it would be greatly appreciated.</p> <p>Thanks in advance,</p> <p>Les</p> 2006-02-28T20:38:02-05:001213351http://forums.asp.net/p/954933/1213351.aspx/1?Re+UPDATE+DELETE+not+working+in+Data+ControlsRe: UPDATE/DELETE not working in Data Controls <p><a href="http://kanthu.blogspot.com/">http://kanthu.blogspot.com/</a></p> <p>scroll down to objectdatasource bit, it explains it and a way around it.</p> 2006-03-01T09:04:05-05:001415705http://forums.asp.net/p/954933/1415705.aspx/1?Re+UPDATE+DELETE+not+working+in+Data+ControlsRe: UPDATE/DELETE not working in Data Controls <p>You can easily use UPDATE/ DELETE command by doing this:</p> <p><font face="Courier New">UpdateCommand<font color="#0000ff" size="2">=&quot;UPDATE [TableNullable] SET [Col_Nullable] = @Col_Nullable, [Col_Nullable2] = @Col_Nullable2 WHERE [id] = @original_id&quot;</font></font></p> <p><font color="#0000ff" face="Courier New">the problem is that the AND condition</font></p> <p><font color="#0000ff" face="Courier New"></font></p> 2006-10-02T09:23:11-04:001419625http://forums.asp.net/p/954933/1419625.aspx/1?Re+UPDATE+DELETE+not+working+in+Data+ControlsRe: UPDATE/DELETE not working in Data Controls <p>How do you apply this to a datetime type instead of a character type?</p> <p>&nbsp;</p> <p>I have this code but it will not work when the supposedly null value contains some data.</p> <p>&nbsp;</p> <pre class="prettyprint">UpdateCommand=&quot;UPDATE [Admins] SET [AdminEmail] = @AdminEmail, [AdminPassword] = @AdminPassword, [AdminCreated] = @AdminCreated, [AdminLastLogin] = @AdminLastLogin, [FailedPasswordAttemptCount] = @FailedPasswordAttemptCount, [FailedPasswordAttemptWindowStart] = @FailedPasswordAttemptWindowStart, [LastPasswordChangedDate] = @LastPasswordChangedDate WHERE [AdminID] = @original_AdminID AND [AdminEmail] = @original_AdminEmail AND [AdminPassword] = @original_AdminPassword AND ISNULL([AdminCreated], '10/04/2006') = ISNULL(@original_AdminCreated, '10/04/2006') AND ISNULL([AdminLastLogin], '') = ISNULL(@original_AdminLastLogin, '') AND ISNULL([FailedPasswordAttemptCount], '') = ISNULL(@original_FailedPasswordAttemptCount, '') AND ISNULL([FailedPasswordAttemptWindowStart], '') = ISNULL(@original_FailedPasswordAttemptWindowStart, '') AND ISNULL([LastPasswordChangedDate], '') = ISNULL(@original_LastPasswordChangedDate, '')&quot; OnUpdated=&quot;SqlDataSource1_Updated&quot;&gt;</pre>&nbsp;<br> &nbsp; 2006-10-05T18:31:37-04:001603784http://forums.asp.net/p/954933/1603784.aspx/1?Re+UPDATE+DELETE+not+working+in+Data+ControlsRe: UPDATE/DELETE not working in Data Controls <p>Thanks so much for this post&nbsp; I have really been struggling to get the new ado.net and asp.net to actually work like the book says it should.&nbsp; They seem to have left out this little fact. In my case in since I am dealing with a small companys database and concurancy was not going to be an issue I just unchecked Use optimmistic concurrency&nbsp;and now my updates post back</p> <p>&nbsp;</p> 2007-03-03T22:58:47-05:001635979http://forums.asp.net/p/954933/1635979.aspx/1?Re+UPDATE+DELETE+not+working+in+Data+ControlsRe: UPDATE/DELETE not working in Data Controls <p>It seems that the solution is just setting ConflictDetection to OverwriteChanges instead of CompareAllValues. But that is not the problem in my case, because I've already set it to OverwriteChanges but still no updating happens. I've been searching for a solution for 2 days now but found nothing usefull, all the solutions I found didn't help me.</p> <p>&nbsp;</p> <p>It seems that I'll throw the datagrid into the trash and do the work manually.</p> 2007-03-25T16:10:38-04:001905337http://forums.asp.net/p/954933/1905337.aspx/1?Re+UPDATE+DELETE+not+working+in+Data+ControlsRe: UPDATE/DELETE not working in Data Controls <p>I had the same problem, that is one mean little checkbox!</p> 2007-09-12T16:25:08-04:003592818http://forums.asp.net/p/954933/3592818.aspx/1?Re+UPDATE+DELETE+not+working+in+Data+ControlsRe: UPDATE/DELETE not working in Data Controls <p>This problem drove me mad for ages. Turned out I needed to use&nbsp;</p> <p>proper.PropertiesMainDataTable propertyTable = propAdapter.GetPropertiesByPropertyId(propertyid);</p> <p>instead of&nbsp;</p> <p>proper.PropertiesMainDataTable propertyTable = new proper.PropertiesMainDataTable();</p> <p>Silly mistake but easily done, maybe this might be useful for someone.</p> 2010-01-01T14:00:43-05:00