how to show my own error message?

V

Vincent

Hi,

There are two related tables: one called 'group' and the other 'items'. Each
item belongs to a group. The table 'group' is shown in a gridview with a
linkbutton for deleting.

When an user tries to delete a group which still contains items, he gets the
message:
"The DELETE statement conflicted with the REFERENCE constraint
"FK_items_groep1". The conflict occurred in database "tennis", table
"dbo.mytable", column 'groupna'.
The statement has been terminated. "

I want to avoid this and just send a short message in a label. So i tried
this, but the error message still appears.

aspx file:
----------
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lb1" runat="server"
CommandArgument="<%# Container.DataItemIndex %>"
CommandName="Delete">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
....

code-behind:
-----------
....
If e.CommandName = "Delete" Then
Try
Me.SqlDataSource1.DeleteCommand = "delete from groep WHERE groepnr='" &
groupkey & "'"
Catch ex As Exception
label1.Text = "Group '" & Server.HtmlEncode(groupkey) & "' cannot be
removed."
End Try
End If


Thanks
Vincent

T.
 
E

Eliyahu Goldin

You need to try-catch the databind operation. With the declarative
databinding it is a bit tricky. It will be simpler just to use regular
databinding, with the DataSource property rather that with the DataSourceID:

GridView1.DataSource = SqlDataSource1
try
GridView1.DataBind()
catch
....
--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
V

Vincent

Thanks
Eliyahu Goldin said:
You need to try-catch the databind operation. With the declarative
databinding it is a bit tricky. It will be simpler just to use regular
databinding, with the DataSource property rather that with the
DataSourceID:

GridView1.DataSource = SqlDataSource1
try
GridView1.DataBind()
catch
...
--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Vincent said:
Hi,

There are two related tables: one called 'group' and the other 'items'.
Each item belongs to a group. The table 'group' is shown in a gridview
with a linkbutton for deleting.

When an user tries to delete a group which still contains items, he gets
the message:
"The DELETE statement conflicted with the REFERENCE constraint
"FK_items_groep1". The conflict occurred in database "tennis", table
"dbo.mytable", column 'groupna'.
The statement has been terminated. "

I want to avoid this and just send a short message in a label. So i tried
this, but the error message still appears.

aspx file:
----------
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lb1" runat="server"
CommandArgument="<%# Container.DataItemIndex %>"
CommandName="Delete">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
...

code-behind:
-----------
...
If e.CommandName = "Delete" Then
Try
Me.SqlDataSource1.DeleteCommand = "delete from groep WHERE groepnr='" &
groupkey & "'"
Catch ex As Exception
label1.Text = "Group '" & Server.HtmlEncode(groupkey) & "' cannot be
removed."
End Try
End If


Thanks
Vincent

T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top