468,772 Members | 2,271 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,772 developers. It's quick & easy.

how to show my own error message?

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.
Feb 19 '08 #1
2 1551
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" <vi,@sd.cvwrote in message
news:eu**************@TK2MSFTNGP04.phx.gbl...
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.


Feb 19 '08 #2
Thanks
"Eliyahu Goldin" <RE**************************@mMvVpPsS.orgschree f in
bericht news:eD**************@TK2MSFTNGP02.phx.gbl...
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" <vi,@sd.cvwrote in message
news:eu**************@TK2MSFTNGP04.phx.gbl...
>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.



Feb 19 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by genetic.error | last post: by
4 posts views Thread by Drew Lettington | last post: by
14 posts views Thread by Ina Schmitz | last post: by
2 posts views Thread by Dave | last post: by
3 posts views Thread by Ipsita | last post: by
1 post views Thread by runway27 | last post: by
2 posts views Thread by Sudhakar | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.