[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Gridview RowUpdating Exception Handling

Posted on 2008-10-02
4
Medium Priority
?
2,461 Views
Last Modified: 2013-11-11
I am attempting to update an SQL 2005 table from a gridview.  The table has a unique index on two fields, PersonID and EmailTypeID.

I can update a record successfully if the record is indeed unique:

PersonID     EmailTypeID
123             1
123             2
123             3

the record is committed to the DB and all is well.

If I attempt to update and violate the unique index such as:
PersonID     EmailTypeID
123             1
123             2
123             2

I get no response in the web page.  The update simply does not happen, but there is no indication on the page that there is a problem.

If I run Query Analyzer and run the generated SQL captured from Profiler, I get the obvious:
 "Cannot insert duplicate key row"

I tried to use:
    Protected Sub grdEmails_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles grdEmails.RowUpdating

    End Sub

but I can't seem to expose any exception class from e.

I am using a LinqDataSource... and I only have two properties in my codebehind

How can I trap the exception?

Thanks,
Aaron

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="grdEmails" runat="server" AutoGenerateColumns="False" DataSourceID="ldsPersonEmails" 
    DataKeyNames="EmailRecordID" OnRowUpdating="UpdateEmails">
    <Columns>
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" EditText="edit" DeleteText="delete" />
        <asp:TemplateField HeaderText="Email">
            <EditItemTemplate>
                <asp:TextBox ID="txtEditEmailAddress" runat="server" Text='<%# Bind("EmailAddress")%>' ></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
                Add
                <asp:TextBox ID="txtNewEmailAddress1" runat="server"></asp:TextBox>
            </FooterTemplate>
            <ItemTemplate>
                <asp:HyperLink ID="hlkEmailAddress" runat="server" NavigateUrl='<%# Eval("EmailAddress", "mailto:{0}") %>'
                    Text='<%# Eval("EmailAddress") %>'>
                </asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Type">
            <FooterTemplate>
                <asp:DropDownList ID="ddlEmailTypes1" DataSourceID="ldsEmailTypes" DataValueField="EmailTypeID"
                    DataTextField="EmailDescription"  runat="server">
                </asp:DropDownList>
            </FooterTemplate>
            <ItemTemplate>
                <%#Eval("email_type.EmailDescription")%>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="ddlEmailTypes2" DataSourceID="ldsEmailTypes" DataValueField="EmailTypeID"
                    DataTextField="EmailDescription" SelectedValue='<%#Bind("EmailTypeID")%>' runat="server">
                </asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
    <EmptyDataTemplate>
        No email addresses on file.<br />
        <br />
        Add a new email address:<br />
        <asp:TextBox ID="txtNewEmailAddress2" runat="server"></asp:TextBox>
        <asp:DropDownList ID="ddlEmailTypes3" DataSourceID="ldsEmailTypes" DataValueField="EmailTypeID"
                    DataTextField="EmailDescription"  runat="server">
        </asp:DropDownList>
        <asp:LinkButton ID="lnkAddNewEmailEmptyData" runat="server">Add</asp:LinkButton>
    </EmptyDataTemplate>
</asp:GridView>
<asp:LinqDataSource ID="ldsPersonEmails" runat="server" ContextTypeName="VBCODE.jhadbDataClassesDataContext"
    TableName="person_emails" Where="PersonID == @PersonID" EnableDelete="True" EnableInsert="True"
    EnableUpdate="True">
    <WhereParameters>
        <asp:QueryStringParameter Name="PersonID" QueryStringField="PersonID" Type="Int32" />
    </WhereParameters>
</asp:LinqDataSource>
<asp:LinqDataSource ID="ldsEmailTypes" ContextTypeName="VBCODE.jhadbDataClassesDataContext"
    TableName="email_types" runat="server">
</asp:LinqDataSource>
</ContentTemplate>
</asp:UpdatePanel>

Open in new window

0
Comment
Question by:abenage
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Expert Comment

by:madhevan_pillai
ID: 22658125
Hi

Did you get any exception like the below
Could not find a row that matches the given keys in the original values stored in ViewState.  Ensure that the 'keys' dictionary contains unique key values that correspond to a row returned from the previous Select operation.
0
 
LVL 1

Author Comment

by:abenage
ID: 22659471
I think I have ran into that problem and I needed to make sure there was a unique column on the database table.  For example, I was using the PersonID and EmailTypeID columns for the primary key.  What I had to do was create a EmailRecordID column, type INT, Identity True and set that as the Primary key.  I then used the other two columns to create a unique index.  Then, in my gridview, I set the DataKeyNames to the new EmailRecordID column.  Does this help you?
0
 
LVL 4

Accepted Solution

by:
novynov earned 2000 total points
ID: 22670543
Using the controls that you mentioned (LinqDataSource / GridView), there are 3 events during update that expose an exception:

- LinqDataSource.OnUpdating - LinqDataSourceUpdateEventArgs.Exception
- LinqDataSource.OnUpdated - LinqDataSourceStatusEventArgs.Exception
-GridView.OnRowUpdated - GridViewRowUpdatedEventArgs.Exception

The GridView.OnRowUpdating does not expose one.

That said, I believe an exception due to a violation of a constraint (e.g. FK) will be trappable both on LinqDataSource.OnUpdated as well as GridView.OnRowUpdated.

On each of those events, there is an ExceptionHandled property that you can set to true if you want the exception to cease bubbling up.

Please let me know if this doesn't answer your question. I'll be glad to help.




0
 
LVL 1

Author Comment

by:abenage
ID: 22679454
I got it with LinqDataSource.OnUpdating.  Thanks!
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question