This is a migrated thread and some comments may be shown as answers.

Insert vs. Edit Validations

4 Answers 83 Views
Grid
This is a migrated thread and some comments may be shown as answers.
mikestuart
Top achievements
Rank 1
mikestuart asked on 07 Jan 2013, 03:40 AM
Hi

Trying to get started with http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/alleditablecolumns/defaultcs.aspx...

The "ProductName" column cannot be empty.

Trying to erase the value of that field and save shows a message "Product with ID 1 cannot be updated. Reason: Cannot insert the value NULL into column 'ProductName', table 'AspnetAjaxNorthwind35.dbo.Products'; column does not allow nulls. UPDATE fails. The statement has been terminated."

But, when saving a new row with an empty "ProductName" shows no error at all; neither is there any feedback from the form that indicates there are errors while saving. The form just sits there with no messages.

Shouldn't the validations be processed the same way for both updates and inserts? Or, are there other examples that are more complete from a validation standpoint?

Thanks

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 07 Jan 2013, 04:11 AM
Hi,

If the ID column has no default value, but has NOT NULL constraint, then you have to provide a value yourself while inserting. If ID is a primary key, it MUST be unique and not null. If you do not mention any field in the fields list for insert it'll be supposed to be null or default value. One suggestion is that you can set identity for the field if you do not want to set it manually. Also you can validate the controls in edit mode as explained in this documentation.

Thanks,
Shinu.
0
mikestuart
Top achievements
Rank 1
answered on 07 Jan 2013, 04:49 AM
Shinu - the issue is not about validations nor is it about primary keys; it is about the way grid handles validation / events differently between insert errors and update errors. I found another demo doing the exact same thing (http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/validation/defaultcs.aspx).

The database errors are showing fine during updates but no errors are shown for insert failures.

To see it action...
1) Edit an existing row at http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/alleditablecolumns/defaultcs.aspx
2) Clear out the ProductName field
3) Save - you will see an error similar to "Product with ID 1 cannot be updated. Reason: Cannot insert the value NULL into column 'ProductName', table 'AspnetAjaxNorthwind35.dbo.Products'; column does not allow nulls. UPDATE fails. The statement has been terminated."
So far so good; now...
1) Click add new record on that same demo
2) Save without entering any value in ProductName field
3) Save - you see no error; should it not show the same error like the above when editing an existing row?

On looking at the code (scrolling down that demo page), the same code for presenting errors is available both in the RadGrid1_ItemUpdated event and the RadGrid1_ItemInserted event.

GridEditableItem item = (GridEditableItem)e.Item;
String id = item.GetDataKeyValue("ProductID").ToString();
 
if (e.Exception != null)
{
    e.KeepInEditMode = true;
    e.ExceptionHandled = true;
    SetMessage("Product with ID " + id + " cannot be updated. Reason: " + e.Exception.Message);
}
else
{
    SetMessage("Product with ID " + id + " is updated!");
}

Perhaps it has something to do with the way the grid refreshes the page after a Save is issued...

Thanks.

0
mikestuart
Top achievements
Rank 1
answered on 09 Jan 2013, 09:57 AM
please respond we need to get this issue resolved!
0
Maria Ilieva
Telerik team
answered on 10 Jan 2013, 12:01 PM
Hi Michael,

Thank you for reporting this unexpected behavior into our demos. The reason for the message to not appear on insert is that it is set in RadGrid ItemDataBound event which does not fire on insert. You could simply changes the code from :
protected void RadGrid1_DataBound(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(gridMessage))
            {
                DisplayMessage(gridMessage);
            }
        }

to:
protected void RadGrid1_PreRender(object sender, EventArgs e)
       {
           if (!string.IsNullOrEmpty(gridMessage))
           {
               DisplayMessage(gridMessage);
           }
       }

I will also update the online demo and the modified version will be available on the next upload of our online examples.

Greetings,
Maria Ilieva
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
mikestuart
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
mikestuart
Top achievements
Rank 1
Maria Ilieva
Telerik team
Share this question
or