Click here to Skip to main content
11,629,614 members (81,662 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi Everyone.

I am exporting data in excel sheet.when i clik export button then following exception is arises and they ask "quit" or "continue".when i click continue then successfully datas are exporting in excel sheet,but how can i handel this exception.

PLEASE HELP.Its very urgent for me.
Thanks a lot inadvance.
"Index and length must refer to a location within the string. Parameter name: length"
System.ArgumentOutOfRangeException was unhandled
  Message="Index and length must refer to a location within the string.\r\nParameter name: length"
  Source="mscorlib"
  ParamName="length"
  StackTrace:
       at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
       at System.String.Substring(Int32 startIndex, Int32 length)
       at ICDProject.frmDriverPayment.<newreportquery>b__6(clsExtensionOfPortTictsCharges order) in F:\Project\NewICDProject\ICDProject\ICDProject\frmDriverPayment.cs:line 1105
       at System.Linq.Lookup`2.Create[TSource](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
       at System.Linq.GroupedEnumerable`3.GetEnumerator()
       at System.Linq.Enumerable.<SelectIterator>d__d`2.MoveNext()
       at ICDProject.frmDriverPayment.NewReportQuery() in F:\Project\NewICDProject\ICDProject\ICDProject\frmDriverPayment.cs:line 1120
       at ICDProject.frmDriverPayment.btnViewInGrid_Click(Object sender, EventArgs e) in F:\Project\NewICDProject\ICDProject\ICDProject\frmDriverPayment.cs:line 268
       at ICDProject.frmDriverPayment.btnExportInExcel_Click(Object sender, EventArgs e) in F:\Project\NewICDProject\ICDProject\ICDProject\frmDriverPayment.cs:line 647
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at ICDProject.Program.Main() in F:\Project\NewICDProject\ICDProject\ICDProject\Program.cs:line 27
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
       at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
Posted 16-May-12 2:48am
Edited 16-May-12 3:34am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You have not shared the related code snippet.

Generally, this error happens when you're trying to get a substring of a string, but the size of the substring you're trying to get exceeds the length of the string.

You can solve this by checking the length of the main string BEFORE you attempt to take a substring of it.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

the parameters total must be inferior to total length of the string

startIndex + length < string.length


http://msdn.microsoft.com/en-us/library/aka44szs.aspx[^]
  Permalink  

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

  Print Answers RSS


Advertise | Privacy | Mobile
Web03 | 2.8.150723.1 | Last Updated 16 May 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100