?
Solved

create error when gridview update text field has too many characters.

Posted on 2009-05-09
2
Medium Priority
?
418 Views
Last Modified: 2012-05-06
I have a gridview that has update capabilities. How do I create an error message when there are more that say 20 characters? Thanks in advance
<asp:GridView ID="eventsGrid" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ID"
         DataSourceID="AccessDataSource1" 
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"/>
                <asp:TemplateField HeaderText="Event" SortExpression="event">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtUpdateEvent" runat="server" Text='<%# Bind("event") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>                   
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("event") >'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>        
                <asp:CommandField ButtonType="Button" ShowEditButton="True" />
            </Columns>
 
Protected Sub EventsGrid_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles eventsGrid.RowUpdated
        If e.Exception IsNot Nothing Then
        lblError.Text = "An exception ocurred." & e.Exception.Message
        e.ExceptionHandled = True
       e.KeepInEditMode = True
         ElseIf e.AffectedRows = 0 Then
          lblError.Text = "No rows Updated"
 End If
End Sub

Open in new window

0
Comment
Question by:designaire
2 Comments
 
LVL 11

Accepted Solution

by:
Muhammad Ousama Ghazali earned 500 total points
ID: 24346149
There is no need to raise an error in case there are more than 20 character as you can limit your user to enter maximum of 20 (or whatever maximum limit is) characters by using the MaxLength property of the TextBox in ASP.NET.

Add the part formatted bold in your ASPX code and you are done.

<asp:TextBox ID="txtUpdateEvent" runat="server" Text='<%# Bind("event") %>' MaxLength="20"></asp:TextBox>
0
 

Author Comment

by:designaire
ID: 24346309
Yes, thank you. It does throw an exception. Maybe because of the below code. I had to set the maximum inthe database also.

Protected Sub EventsGrid_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles eventsGrid.RowUpdated
        If e.Exception IsNot Nothing Then
        lblError.Text = "An exception ocurred." & e.Exception.Message
        e.ExceptionHandled = True
       e.KeepInEditMode = True
         ElseIf e.AffectedRows = 0 Then
          lblError.Text = "No rows Updated"
 End If
End Sub
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month16 days, 20 hours left to enroll

862 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