Click here to Skip to main content
15,747,637 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
protected void asd_Click(object sender, EventArgs e)
            if (Common.Common.ApplicationInformation.LoggedInUser.RGroupIds != null)
                string Documentid = string.Empty;
                string GroupIds = string.Empty;
                string DocumentIds = string.Empty;
                string ContactIds = string.Empty;
                if (Session["ReportDocumentIDs"].ToString() != null)
                    DocumentIds = Session["ReportDocumentIDs"].ToString();
                    Documentid = DocumentIds.Substring(0, DocumentIds.LastIndexOf(','));
                    Documentid = null;
Updated 8-Jun-15 20:23pm
Sinisa Hajnal 9-Jun-15 2:14am    
Which line? Only thing I see that could cause the error is the fact that you are not checking if Session["ReportDocumentIDs"] is null before using it (calling ToString() is usage before checking)...
Member 10033441 9-Jun-15 2:15am    
Documentid = DocumentIds.Substring(0, DocumentIds.LastIndexOf(','));

Please read the documentation[^]. It happens because
MSDN wrote:
String.LastIndexOf method reports the zero-based index position of the last occurrence of a specified Unicode character or string within this instance. The method returns -1 if the character or string is not found in this instance.

So, you can not return substring which starts with zero and ends with -1 ;)
Share this answer
Sascha Lefèvre 9-Jun-15 2:38am    
Maciej Los 9-Jun-15 2:40am    
Thank you, Sascha.
Length cannot be less than zero.

Documentid = DocumentIds.Substring(0, DocumentIds.LastIndexOf(','));

This exception will occur if you are passing a DocumentIds without a ',' ;

DocumentIds.LastIndexOf(',') this will return -1;

then Documentid = DocumentIds.Substring(0, -1); throws exception..

Try to handle it..If you need more help let me know..
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900