• Register
0 votes

Problem :

I am facing below error:

Index and length must refer to a location within the string.
Parameter name: length

I am using below code:

string s1 = ddlweek.Text.Substring(0, 8);                
string s3 = ddlweek.Text.Substring(10, 14);

How can I resolve the error?

6 5 3
7,540 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

If in any case the length of your string (ddlweek) is 23 characters or less than 23 characters, then you will get error:

Please see below explanation for you.

string ddlweek = "12345678901234567890123";//This is clearly Not Ok
string s1 = ddlweek.Substring(0, 8);                
    string s3 = ddlweek.Substring(10, 14);

So your string must be at least 24 characters long.. So you must consider adding the if to make sure that everything is OK. As explained for you below :

string ddlweek = "123456789012345678901234";//This is clearly OK
string s1 = ddlweek.Substring(0, 8);                
    string s3 = ddlweek.Substring(10, 14);



It clearly means that your ddlweek.Text string contains the less number of characters than what you asked for in the Substring(index, length).

Example as below:

if (ddlweek.Text.Length >= 8)

    string s1 = ddlweek.Text.Substring(0, 8); 



It just clearly means you are asking for a substring of ddlweek that does not exist (it is more than 24 characters in length).

9 7 4
38,600 points
0 votes


The second parameter in Substring is the length of the substring, not the closing index.

You must possibly add handling to check that it does truly begin with what you expect, end with what you expect, and is at least as long as you expect. And then in case it doesn't match, you can either do something else or throw a significant error.

Here's some example code that affirms that url holds your strings, that further is refactored a bit to make it simplier to alter the prefix/suffix to strip:

var prefix = "www.example.com/";
var suffix = ".jpg";
string url = "www.example.com/aaa/bbb.jpg";

if (url.StartsWith(prefix) && url.EndsWith(suffix) && url.Length >= (prefix.Length + suffix.Length))
    string newString = url.Substring(prefix.Length, url.Length - prefix.Length - suffix.Length);
    //handle invalid state

Your fault is the parameters to Substring. The first parameter must be the start index and the second must be the length or offset from the startindex.

string newString = url.Substring(18, 7);

In case the length of the substring can vary you require to calculate the length.

Something in the way of (url.Length - 18) - 4 (or url.Length - 22)

In the end it will seem something like this

string newString = url.Substring(18, url.Length - 22);

Or, something like this :

string url = "http://www.example.com/aaa/bbb.jpg";
Uri uri = new Uri(url);
string path_Query = uri.PathAndQuery;
string extension =  Path.GetExtension(path_Query);

path_Query = path_Query.Replace(extension, string.Empty);// This will remove extension

You require to find the position of the first /, and then calculate the portion you want:

string url = "www.example.com/aaa/bbb.jpg";
int Idx = url.IndexOf("/");
string yourValue = url.Substring(Idx + 1, url.Length - Idx - 4);

Attempt This:

int positionOfJPG=url.IndexOf(".jpg");
 string newString = url.Substring(18, url.Length - positionOfJPG);
string newString = url.Substring(18, (url.LastIndexOf(".") - 18))

In case the length of your string (ddlweek) is 23 characters or less, you will obtain this error:

 string ddlweek = "12345678901234567890123";//This is NOK
    string a1 = ddlweek.Substring(0, 8);                
    string a3 = ddlweek.Substring(10, 14);

The string must be at least 24 characters long.. You might take into account including an if to ensure that everything is OK.

  string ddlweek = "123456789012345678901234";//This is OK
    string a1 = ddlweek.Substring(0, 8);                
    string a3 = ddlweek.Substring(10, 14);

It imply that your ddlweek.Text string holds less number of characters than what you requested for in Substring(index, length).


if (ddlweek.Text.Length >= 8)
    string a1 = ddlweek.Text.Substring(0, 8);  

This error happens at the time you exceed your end limit of total character. for example

 string name = "iLovePakistan"; // Here i want to print only Pakistan

        string name2 = name.Substring(5, 150); // this code will throw same error. just replace 150 with name.Length - 5

        string name3 = name.Substring(5, name.Length - 5); // i skip firt 5 charchers then name.Length-5 means print rest 8 characters.

        string name4 = name.Substring(5, 8); // This will do exactly as name3
string myString = TextBox1.Text.ToString();
string substring = myString.Substring(0, 3);
string substring2 = myString.Substring(3, 3);


10 6 4
31,120 points

Related questions

0 votes
1 answer 101 views
Problem : I am new to ASP.Net and I have an ASP.Net application which runs correctly on my local development machine. But when I try to run this application online, it shows me following error Format of the initialization string does not conform to specification starting at index 0 I don’t have any clue about this error. Does anybody have any idea regarding solution on this error?
asked Nov 25, 2019 alecxe 7.5k points
0 votes
1 answer 11 views
How do you convert a numerical number to an Excel column name in C# without using automation getting the value directly from Excel.?
asked Aug 28, 2020 Aliza313 720 points
0 votes
1 answer 89 views
Problem : I want to deploy my ASP.NET application. I have tried deploying my site to IIS, but when I visit it with the browser, it gives me following error : Server Error 500 - Internal server error. There is a problem with the resource you are looking for ... error: The page cannot be displayed because an internal server error has occurred. I want to see the actual issue behind my server error?
asked Dec 7, 2019 alecxe 7.5k points
0 votes
1 answer 313 views
Problem : I am getting into the C# coding but I am having following issue: I have below code on the UserControl: using MyFirstDataLayer.Section1; public class MyFirstClass { protected void MyFirstMethod { MyFirstClass.MyFirstItem oItem = new MyFirstClass.MyFirstItem(); oItem = ... name instead I am not sure of what this actually means, I did some research but was unable to figure it out.
asked Dec 18, 2019 alecxe 7.5k points
0 votes
1 answer 12 views
Problem: Hi, I want to take multiple employee ids in one shot from the user and need to assign them to list .in asp.net, pls help me to sort it out. What I have tried. I have created a text box and tried with validator but it didn't worked out.
asked Sep 14, 2020 boris elin 620 points
0 votes
1 answer 50 views
Problem : I am facing below error if I try to export datagrid to the excel. &ldquo;Control 'gridview1' of type 'gridview' must be placed inside a form tag with runat=server.&rdquo; Please find below my code : <form runat="server" id="form1"> <div runat="server" id=" ... TextBox> </form> My GridView is within the from tag with the runat="server", but still I am facing above error. How to fix this?
asked Feb 17, 2020 mphil 2.3k points
0 votes
1 answer 21 views
Problem: I am facing below strange looking error when I tried to add a script Manager to a password strength of AJAX toolkit. "Only one instance of a ScriptManager can be added to the page." I had added the password strength alongside the ... "RequiredFieldValidator10" ControlToValidate="Password"    ErrorMessage="Password is required." />   </ContentTemplate> </asp:UpdatePanel> </td> </tr>
asked Aug 1, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 1.3K views
Problem : I am facing the below error: Failed to load resource: the server responded with a status of 401 (Unauthorized) http://localhost/App/AngularJS/angular.min.js My project works fine on the localhost. But when I try to run it on the IIS (7.5) I often face ... read/write/modify access for the "IUSR" & "IIS_ISURS" users of my whole project. But still I am facing the same exception on my IIS.
asked Dec 24, 2019 alecxe 7.5k points
0 votes
1 answer 49 views
Problem : I came across a problem as follows if you set the autopostback property of a control to true, the page is posted back to the server Any solution on this ?
asked Nov 12, 2019 peterlaw 6.9k points