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
Solved

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

Posted on 2009-05-09
2
405 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 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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