Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with Substring() - ArgumentOutOfRangeException

Tags:

c#

asp.net

I have a repeater that displays data from my Projects table. There are projectId, name and description. I use Substring(1, 240) on description. But sometimes the string is shorter than 240, so I get ArgumentOutOfRangeException. Can you tell me how to display the whole text if I get the exception. This is my code.

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> <asp:Panel ID="pnlDisplayProjects" runat="server" Visible="true">     <center><h2><b>Проекти</b></h2></center>         <asp:Repeater ID="rptrProjects" runat="server">             <HeaderTemplate>                 <table border="1" cellpadding="2" cellspacing="2" align="center" width="80%" style="background-color:#F7F6F3;">             </HeaderTemplate>             <ItemTemplate>                 <tr>                     <td align="left" style="width:40px">                         <asp:Label ID="LblProjectId" runat="server" Text='<%# Eval("ProjectID") %>' />                     </td>                     <td align="center">                         <asp:Label ID="LblName" runat="server" Text='<%# Eval("Name") %>' />                     </td>                 </tr>                 <tr>                     <td colspan="2">                         <asp:Label ID="LblDescription" runat="server" Text='<%# Eval("Description").ToString().Substring(1, 240) + "..." %>'/>                         <asp:HyperLink ID="HlMore" runat="server" NavigateUrl='<%#"~/Project/ViewProject.aspx?projectId=" + Eval("ProjectID") %>' Text="More" />                     </td>                 </tr>             </ItemTemplate>             <FooterTemplate>                 </table>             </FooterTemplate>         </asp:Repeater> </asp:Panel> 

 protected override void OnPreRender(EventArgs e)     {         var table = Projects.GetTableWithProjects();          if (table.Rows.Count > 0)         {             rptrProjects.DataSource = table;             rptrProjects.DataBind();         }         else         {             pnlDisplayProjects.Visible = false;             Master.PrintMessage("There are no projects.");         }     } 
like image 623
Ivan Stoyanov Avatar asked Jan 07 '10 15:01

Ivan Stoyanov


1 Answers

I would suggest you write a separate extension method if you're using .NET 3.5. Something like this:

public static string SafeSubstring(this string text, int start, int length) {     return text.Length <= start ? ""         : text.Length - start <= length ? text.Substring(start)         : text.Substring(start, length); } 

Basically that's a version of Substring which will never throw an exception unless start or length is negative (in which case I don't know what it could sensibly return).

You'd call it like this:

Eval("Description").ToString().SafeSubstring(1, 240) + "..." 

The downside of this is that it will include the ellipsis (...) even if it's not actually truncated the string at all...

like image 150
Jon Skeet Avatar answered Sep 25 '22 08:09

Jon Skeet