Solved

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

Posted on 2009-05-09
2
406 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
[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 Comments
 
LVL 11

Accepted Solution

by:
Muhammad Ousama Ghazali earned 125 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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