Loupi
Loupi

Reputation: 1112

Try catch if record exist ASP.NET

I'm trying to make a try/catch method to see if a record exist or not and generate an error messagebox if it does exist and if it not it will perform an insert.

Here's a sample of my code in insertMode:

        ISBN:
        <asp:TextBox ID="bookidTextBox" runat="server" Text='<%# Bind("bookid") %>' ToolTip="For the book ISBN please refer to the ISBN barcode usually located at the back of the book. A barcode reader is required." />

        <asp:RequiredFieldValidator ID="RequesFieldValidator1" runat="server" ErrorMessage="* Required" ControlToValidate="bookidTextBox" ValidationGroup="InsertBook">
        </asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator" runat="server" Display="Dynamic" ControlToValidate="bookidTextBox" ValidationExpression="^([\S\s]{10,13})$" ErrorMessage="Invalid ID/ISBN. Please try again" ValidationGroup="InsertBook">
        </asp:RegularExpressionValidator>
        <br />

        Title:
        <asp:TextBox ID="booktitleTextBox" runat="server" Text='<%# Bind("booktitle") %>' />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="* Required" ControlToValidate="booktitleTextBox" ValidationGroup="InsertBook">
        </asp:RequiredFieldValidator>
        <br />

        Author's lastname:
        <asp:TextBox ID="lastnameTextBox" runat="server" Text='<%# Bind("lastname") %>' />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="* Required" ControlToValidate="lastnameTextBox" ValidationGroup="InsertBook">
        </asp:RequiredFieldValidator>
        <br />

        Author's firstname:
        <asp:TextBox ID="firstnameTextBox" runat="server" Text='<%# Bind("firstname") %>' />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="* Required" ControlToValidate="firstnameTextBox" ValidationGroup="InsertBook">
        </asp:RequiredFieldValidator>
        <br />

        Description:
        <asp:TextBox ID="descriptionTextBox" runat="server" Height="150px" Width="300px" Text='<%# Bind("description") %>' />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="* Required" ControlToValidate="descriptionTextBox" ValidationGroup="InsertBook">
        </asp:RequiredFieldValidator>
        <br />

        Category:
        <asp:DropDownList ID="DropDownList1" runat="server" 
            DataSourceID="categoryDataSource" DataTextField="name" 
            DataValueField="categoryid" SelectedValue='<%# Bind("categoryid", "{0}") %>' ToolTip="to add a new category please click the 'Manage Categories' menu">
        </asp:DropDownList>

        <asp:SqlDataSource ID="categoryDataSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>" 
            SelectCommand="SELECT [categoryid], [name] FROM [TblCategory]">
        </asp:SqlDataSource>
        <br />

        Quantity:
        <asp:TextBox ID="quantityTextBox" runat="server" Text='<%# Bind("quantity") %>' />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ErrorMessage="* Required" ControlToValidate="quantityTextBox" ValidationGroup="InsertBook">
        </asp:RequiredFieldValidator>
        <asp:RangeValidator ID="RangeValidator" runat="server" ErrorMessage="Invalid inputs. Accept numbers only" ControlToValidate="quantityTextBox" MinimumValue="1" MaximumValue="50" ValidationGroup="InsertBook">
        </asp:RangeValidator>
        <br />

        <asp:Button ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Add" ValidationGroup="InsertBook" PostBackUrl="~/Admin/ManageBooks.aspx"/>

        <asp:Button ID="InsertCancelButton" runat="server" 
            CausesValidation="False" CommandName="Cancel" Text="Cancel" />

Help would be much appreciated! Thanks in advance.

Upvotes: 0

Views: 677

Answers (1)

Steve Wellens
Steve Wellens

Reputation: 20640

Put a unique index on the field(s) you want to keep unique.

Go ahead and insert. If there is already a record, an exception will be raised which you can catch and handle.

This is faster, simpler and more robust than checking before you insert.

Upvotes: 2

Related Questions