C# Index was outside the bounds of the array
Recently I got these exceptions in my application which is hosted in a server. When i restarted the website in IIS the error got rectified.But I dont know the sequence which lead to this exception as I could not debug the code(Since its hosted in server).The application works fine locally and no exception is thrown. I checked my back-end. It is also perfect.I do maintain a stack trace for all the errors that has occurred in my application. I will put the exception thrown for your reference.
Message
Exception of type `'System.Web.HttpUnhandledException'` was thrown.
Source
System.Web
Stack
at System.Web.UI.Page.HandleError(Exception e) at
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean
includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at
System.Web.UI.Page.ProcessRequest() at
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at
System.Web.UI.Page.ProcessRequest(HttpContext context) at
ASP.admin_editvideos_aspx.ProcessRequest(HttpContext context) at
System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Message
Index was outside the bounds of the array.
Source
App_Web_kys31898
Stack
at EditVideos.SetSpanAndLabelValues(Video aVideo, EduvisionUser auser) at
EditVideos.AssignVideoDetailsToControls(Video aVideo, EduvisionUser auser) at
EditVideos.SetValue(Int32 videoId, Int32 categoryId) at
EditVideos.SetValuesOnPageLoad() at EditVideos.Page_Load(Object sender, EventArgs e) at
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t,
EventArgs e) at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,
EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at
System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
C# code:
private void SetSpanAndLabelValues(Video aVideo, EduvisionUser auser)
{
string uploadedDate = aVideo.UploadedDate.ToString();
string[] dateTimeValue = uploadedDate.Trim().Split(' ');
string dateFormat = CommonUtilities.GetCultureDateFormat(dateTimeValue[0]);
spn_UploadedDate.InnerHtml = dateFormat + ' ' + dateTimeValue[1] + ' ' + dateTimeValue[2];
spnUploader.InnerHtml = aVideo.Title;
spnvideotitle.InnerHtml = ReadUserMessage("DisplayVideoTitle", "VideoTitle").Replace("#title#", aVideo.Tit开发者_高级运维le);
spanVideoTitle.InnerHtml = ReadUserMessage("DisplayVideoTitle", "VideoTitle").Replace("#title#", aVideo.Title);
spnviewermail.InnerHtml = ReadUserMessage("EditVideos", "MailOption").Replace("#Mail#", auser.Email);
spnmailvalue.InnerHtml = auser.Email;
spnUploaderName.InnerHtml = auser.FirstName;
SetVideoFileNameSpan(aVideo);
SetThumbnailFileNameSpan(aVideo);
if (aVideo.LastUpdatedDate.HasValue)
{
string LastUpdatedDate = aVideo.LastUpdatedDate.ToString();
string[] updatedDateTimeValue = LastUpdatedDate.Trim().Split(' ');
string cultureDateFormat = CommonUtilities.GetCultureDateFormat(updatedDateTimeValue[0]);
lblUploadedDate.Text = cultureDateFormat + ' ' + updatedDateTimeValue[1] + ' ' + updatedDateTimeValue[2];
}
else
{
lblUploadedDate.Text = "Not yet updated";
}
}
If you feel that this question is vague I can provide more information. Please help me out to find the reason for the exception.
Thanks
string[] dateTimeValue = uploadedDate.Trim().Split(' ');
string[] updatedDateTimeValue = LastUpdatedDate.Trim().Split(' ');
Both these lines you split, and then assume they split into what you expect in your code, you need to verify they split into the correct about and the values. Also you cannot split DateTime like that as the format of how a date is shown is vastley different depending on where your from.
Use DateTime.Parse() and work the with a datetie object instead.
You first need to check that is uploadeddate contains [' '] and if contains than you have to write following statement
string[] dateTimeValue = uploadedDate.Trim().Split(' ');
else you have to past a simple string and also take care the in future code.
string dateFormat = CommonUtilities.GetCultureDateFormat(dateTimeValue[0]);
spn_UploadedDate.InnerHtml = dateFormat + ' ' + dateTimeValue[1] + ' ' + dateTimeValue
here the problem is that the server in which you uploaded the code might have different time format or something else which is creating some problem. Put somejavascript to check that value. This is the only way to check that thing.
精彩评论