how to show my own error message?

Discussion in 'ASP .Net' started by Vincent, Feb 19, 2008.

  1. Vincent

    Vincent Guest

    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.
     
    Vincent, Feb 19, 2008
    #1
    1. Advertising

  2. 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.cv> wrote in message
    news:...
    > 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.
    >
    >
     
    Eliyahu Goldin, Feb 19, 2008
    #2
    1. Advertising

  3. Vincent

    Vincent Guest

    Thanks
    "Eliyahu Goldin" <> schreef in
    bericht news:...
    > 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.cv> wrote in message
    > news:...
    >> 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.
    >>
    >>

    >
    >
     
    Vincent, Feb 19, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?cG11ZA==?=
    Replies:
    8
    Views:
    803
    =?Utf-8?B?cG11ZA==?=
    Feb 11, 2005
  2. Stefan Siegl
    Replies:
    1
    Views:
    776
  3. Saverio M.
    Replies:
    0
    Views:
    521
    Saverio M.
    Jul 3, 2006
  4. Gerald Bauer
    Replies:
    0
    Views:
    276
    Gerald Bauer
    Aug 26, 2008
  5. David Filmer
    Replies:
    17
    Views:
    269
    J. Romano
    Aug 18, 2004
Loading...

Share This Page