R
Rob
Hey all,
So.. a simple FormView/SqlDataSource to handle inserting records into a
table. The table has a primary key that the user enters (eg DiscountCode).
If the user enters a duplicate the database complains about a primary key
violation (which is what I want) and an exception is thrown. The OnInserted
event of the SqlDataSource provides access to the exception so, presumably,
you can provide nice handling for various errors.
For example:
protected void FormView1_ItemInserted(object sender,
FormViewInsertedEventArgs e)
{
//
// If there wasn't an exception the record was added successfully
//
if (e.Exception == null)
{
Response.Clear();
Response.Redirect("HappyLand.aspx");
Response.End();
}
//
// An exception occurred. Handle PK violations only
//
System.Data.SqlClient.SqlException se = e.Exception as
System.Data.SqlClient.SqlException;
if (se != null)
{
if (se.Number == 2627)
{
e.ExceptionHandled = true;
}
}
}
Now... what I can't seem to figure out is a "good" way of actually handling
the error. What I'd like to do is indicate that the page is invalid and have
a validation summary displayed along with a polite error message (ala
"Cannot insert duplicate record!").
However... even if I include a call to Page.Validate() which fires up a
custom validator that "knows" a duplicate record was just inserted.... the
summary does not display and the other controls reset to empty (i.e. the
values the user just entered are lost).
Here's what I mean:
private bool _Duplicate = false;
protected void FormView1_ItemInserted(object sender,
FormViewInsertedEventArgs e)
{
_Duplicate = false;
//
// If there wasn't an exception the record was added successfully
//
if (e.Exception == null)
{
Response.Clear();
Response.Redirect("AdminDiscountsList.aspx");
Response.End();
}
//
// An exception occurred. Handle PK violations only
//
System.Data.SqlClient.SqlException se = e.Exception as
System.Data.SqlClient.SqlException;
if (se != null)
{
if (se.Number == 2627)
{
_Duplicate = true;
Page.Validate();
e.ExceptionHandled = true;
}
}
}
protected void CustomValidator1_ServerValidate(object source,
ServerValidateEventArgs args)
{
args.IsValid = !_Duplicate;
}
So... what is the correct way of handling a PK violation using FormView and
SqlDataSource?
Regards,
Rob
So.. a simple FormView/SqlDataSource to handle inserting records into a
table. The table has a primary key that the user enters (eg DiscountCode).
If the user enters a duplicate the database complains about a primary key
violation (which is what I want) and an exception is thrown. The OnInserted
event of the SqlDataSource provides access to the exception so, presumably,
you can provide nice handling for various errors.
For example:
protected void FormView1_ItemInserted(object sender,
FormViewInsertedEventArgs e)
{
//
// If there wasn't an exception the record was added successfully
//
if (e.Exception == null)
{
Response.Clear();
Response.Redirect("HappyLand.aspx");
Response.End();
}
//
// An exception occurred. Handle PK violations only
//
System.Data.SqlClient.SqlException se = e.Exception as
System.Data.SqlClient.SqlException;
if (se != null)
{
if (se.Number == 2627)
{
e.ExceptionHandled = true;
}
}
}
Now... what I can't seem to figure out is a "good" way of actually handling
the error. What I'd like to do is indicate that the page is invalid and have
a validation summary displayed along with a polite error message (ala
"Cannot insert duplicate record!").
However... even if I include a call to Page.Validate() which fires up a
custom validator that "knows" a duplicate record was just inserted.... the
summary does not display and the other controls reset to empty (i.e. the
values the user just entered are lost).
Here's what I mean:
private bool _Duplicate = false;
protected void FormView1_ItemInserted(object sender,
FormViewInsertedEventArgs e)
{
_Duplicate = false;
//
// If there wasn't an exception the record was added successfully
//
if (e.Exception == null)
{
Response.Clear();
Response.Redirect("AdminDiscountsList.aspx");
Response.End();
}
//
// An exception occurred. Handle PK violations only
//
System.Data.SqlClient.SqlException se = e.Exception as
System.Data.SqlClient.SqlException;
if (se != null)
{
if (se.Number == 2627)
{
_Duplicate = true;
Page.Validate();
e.ExceptionHandled = true;
}
}
}
protected void CustomValidator1_ServerValidate(object source,
ServerValidateEventArgs args)
{
args.IsValid = !_Duplicate;
}
So... what is the correct way of handling a PK violation using FormView and
SqlDataSource?
Regards,
Rob