Solved

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

Posted on 2009-05-09
2
403 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net bundle 8 36
Align Buttons next to each other 2 13
Clone Visual studio 2013 solution 5 16
Create XML 5 33
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now