Answered by:
Exception Details: System.FormatException: String was not recognized as a valid DateTime.

Question
-
Hi, i am getting above error, i am new to asp.net pls help
protected void btnSubmit_Click(object sender, EventArgs e)
{
double dstartcnt = 0; //string dstartcnt = string.Emptydouble dstartcnt = 0;
string strStartDate = "03/22";
string strbeforeDate = "03/21";
string strStartCurrentyear = string.Empty;
string strEndCurrentyear = string.Empty;
string strStartCurrentMonth = string.Empty;
string strEndCurrentMonth = string.Empty;
double dEndCount = 0;
DateTime SelectedDate;
DateTime EndDate;
DataTable objGridTable = new DataTable();
double iResult = 0;
double iMoreCnt = 0;
double tempval = 0;
try
{
if (!string.IsNullOrEmpty(this.ddlLocation.SelectedItem.Text))
{
if (!string.IsNullOrEmpty(this.StartDate.Text.ToString()) && Convert.ToDateTime(this.StartDate.Text).ToString("MM-dd").Equals("03-22"))
{
dstartcnt = 1;
SelectedDate = Convert.ToDateTime(this.StartDate.Text);
EndDate = Convert.ToDateTime(this.EndDate.Text);
strEndCurrentyear = Convert.ToDateTime(this.EndDate.Text).Year.ToString();
dEndCount = EndDate.Subtract(Convert.ToDateTime(strStartDate + "/" + strEndCurrentyear)).TotalDays;
}
else if (!string.IsNullOrEmpty(this.StartDate.Text.ToString()))
{
strStartCurrentyear = Convert.ToDateTime(this.StartDate.Text).Year.ToString();
strEndCurrentyear = Convert.ToDateTime(this.EndDate.Text).Year.ToString();
strStartCurrentMonth = Convert.ToDateTime(this.StartDate.Text).Month.ToString();
strEndCurrentMonth = Convert.ToDateTime(this.EndDate.Text).Month.ToString();
SelectedDate = Convert.ToDateTime(this.StartDate.Text);
EndDate = Convert.ToDateTime(this.EndDate.Text);
//SelectedDate = DateTime.Parse(this.StartDate.Text);
//EndDate = DateTime.Parse(this.EndDate.Text);
//if (Convert.ToDateTime(strStartDate + "/" + strStartCurrentyear) < )
// Here starting is -365 through if condition
//dstartcnt = SelectedDate.Subtract(Convert.ToDateTime(strStartDate + "/" + strStartCurrentyear)).TotalDays;
if (int.Parse(strStartCurrentMonth) >= 3 && SelectedDate.Day > 21)
dstartcnt = SelectedDate.Subtract(Convert.ToDateTime(strStartDate + "/" + strStartCurrentyear)).TotalDays + 1;
else if (int.Parse(strStartCurrentMonth) >= 3 && SelectedDate.Day < 22) // lessthan 22 madhu
dstartcnt = SelectedDate.Subtract(Convert.ToDateTime(strStartDate + "/" + strStartCurrentyear)).TotalDays + 365; // madhu
else if (int.Parse(strStartCurrentMonth) <= 3 && SelectedDate.Day < 22)
{
dstartcnt = SelectedDate.Subtract(Convert.ToDateTime(strStartDate + "/" + strStartCurrentyear)).TotalDays + 365;
}
// else if (int.Parse(strStartCurrentMonth)
// dstartcnt = SelectedDate.Subtract(Convert.ToDateTime(this.StartDate.Text + "/" + strStartCurrentyear)).TotalDays;
if (int.Parse(strEndCurrentMonth) <=3 && EndDate.Day < 22)
dEndCount = EndDate.Subtract(Convert.ToDateTime(strStartDate + "/" + (int.Parse(strEndCurrentyear)-1).ToString())).TotalDays;
else if (int.Parse(strEndCurrentMonth) > 3)
{
dEndCount = -SelectedDate.Subtract(Convert.ToDateTime(strbeforeDate + "/" + strEndCurrentyear)).TotalDays + dstartcnt;
iMoreCnt = EndDate.Subtract(Convert.ToDateTime(strStartDate + "/" + strEndCurrentyear)).TotalDays + 1;
}
}
objGridTable.Columns.Add("Location", typeof(string));
objGridTable.Columns.Add("Seleced Date", typeof(string));
objGridTable.Columns.Add("Degree days", typeof(string));
objGridTable.Columns.Add("Cumulative DD", typeof(double));
objGridTable.Columns.Add("value", typeof(string));
// objGridTable.Columns.Add("NPP", typeof(double));
int iDay = 0;
tempval = 0;
for (double i = dstartcnt; i <= dEndCount; i++)
{
iResult = CalculateResult(i);
objGridTable.Rows.Add(this.ddlLocation.SelectedItem.Text, Convert.ToDateTime(this.StartDate.Text).AddDays(iDay).ToString("dd-M-yyyy"), iResult.ToString("0.###"), tempval + iResult, i);//hrs - iResult
tempval = iResult + tempval;
iDay++;
}
if (iMoreCnt > 0)
{
// tempval = 0;
for (double i = dstartcnt; i <= iMoreCnt; i++)
{
iResult = CalculateResult(i);
// objGridTable.Rows.Add(this.ddlLocation.SelectedItem.Text, Convert.ToDateTime(strStartDate + "/" + strEndCurrentyear).AddDays(i - 1).ToString("dd-M-yyyy"), iResult, iResult + tempval);
objGridTable.Rows.Add(this.ddlLocation.SelectedItem.Text, Convert.ToDateTime(strStartDate + "/" + strEndCurrentyear).AddDays(i - 1).ToString("dd-M-yyyy"), iResult.ToString("0.###"), tempval + iResult, i); //, hrs - iResult
tempval = iResult + tempval;
}
}
if (dstartcnt < 365.0) //madhu
{
for (double i = 0; i <= iMoreCnt; i++) // i = 0
{
iResult = CalculateResult(i);
// objGridTable.Rows.Add(this.ddlLocation.SelectedItem.Text, Convert.ToDateTime(strStartDate + "/" + strEndCurrentyear).AddDays(i - 1).ToString("dd-M-yyyy"), iResult, iResult + tempval);
objGridTable.Rows.Add(this.ddlLocation.SelectedItem.Text, Convert.ToDateTime(strStartDate + "/" + strEndCurrentyear).AddDays(i - 1).ToString("dd-M-yyyy"), iResult.ToString("0.###"), tempval + iResult, i); //, hrs - iResult
tempval = iResult + tempval;
}
}// madhu
if (objGridTable.Rows.Count > 0)
{
GridDataResult.DataSource = objGridTable;
GridDataResult.DataBind();
}
}
}
catch (Exception ex)
{
throw ex;
}
}Wednesday, September 11, 2013 10:55 AM
Answers
-
Your sample dates are not DTs but month-year combinations. If you try to convert any of them or if you enter the same values in a textbox (which it looks like StartDate is) then conversion will fail. Dates must be a full month-day-year format.
Your code is overly complex which is making it harder to figure out what is going on. Here's my recommendation for simplifying it:
- Avoid Convert as it is a 1.x feature. As of v2 you should be using the type-specific Parse or TryParse methods.
- Use DateTime.TryParse to convert from a string to a DateTime (or any primitive) when you are not sure if the conversion is allowed. Never trust user input so any user input should be assumed to be wrong.
- Conversion is expensive compared to other operations so do it only once. You are continually converting StartDate.Text to a DT just so you can access one of the fields. Early in your method call DateTime.TryParse on StartDate.Text and then store the result in a local variable (i.e. startDate). Reference the (converted) local variable's properties instead of doing the conversion each time.
- Stop converting everything to a string. This is also expensive compared to simply comparing against real values. For example you are getting the year and month properties but converting them to a string. Later you want to do a comparison so you use Int32.Parse to convert them back. You can just reference the properties directly without any need for storing them or converting them:
protected void btnSubmit_Click(object sender, EventArgs e) { if (ddlLocation....) { DateTime startDate, endDate; if (!DateTime.TryParse(StartDate.Text, out startDate) || !DateTime.TryParse(EndDate.Text, out endDate)) //Error //Not sure why you have these hard coded values or //what they mean if (startDate.Month == 3 && startDate.Day = 22) { var endCount = //Get total days since??? ...
Michael Taylor
http://msmvps.com/blogs/p3net
- Proposed as answer by BonnieBMVP Wednesday, September 11, 2013 3:58 PM
- Marked as answer by Herro wongMicrosoft contingent staff Wednesday, September 18, 2013 8:00 AM
Wednesday, September 11, 2013 2:21 PM
All replies
-
-
Your sample dates are not DTs but month-year combinations. If you try to convert any of them or if you enter the same values in a textbox (which it looks like StartDate is) then conversion will fail. Dates must be a full month-day-year format.
Your code is overly complex which is making it harder to figure out what is going on. Here's my recommendation for simplifying it:
- Avoid Convert as it is a 1.x feature. As of v2 you should be using the type-specific Parse or TryParse methods.
- Use DateTime.TryParse to convert from a string to a DateTime (or any primitive) when you are not sure if the conversion is allowed. Never trust user input so any user input should be assumed to be wrong.
- Conversion is expensive compared to other operations so do it only once. You are continually converting StartDate.Text to a DT just so you can access one of the fields. Early in your method call DateTime.TryParse on StartDate.Text and then store the result in a local variable (i.e. startDate). Reference the (converted) local variable's properties instead of doing the conversion each time.
- Stop converting everything to a string. This is also expensive compared to simply comparing against real values. For example you are getting the year and month properties but converting them to a string. Later you want to do a comparison so you use Int32.Parse to convert them back. You can just reference the properties directly without any need for storing them or converting them:
protected void btnSubmit_Click(object sender, EventArgs e) { if (ddlLocation....) { DateTime startDate, endDate; if (!DateTime.TryParse(StartDate.Text, out startDate) || !DateTime.TryParse(EndDate.Text, out endDate)) //Error //Not sure why you have these hard coded values or //what they mean if (startDate.Month == 3 && startDate.Day = 22) { var endCount = //Get total days since??? ...
Michael Taylor
http://msmvps.com/blogs/p3net
- Proposed as answer by BonnieBMVP Wednesday, September 11, 2013 3:58 PM
- Marked as answer by Herro wongMicrosoft contingent staff Wednesday, September 18, 2013 8:00 AM
Wednesday, September 11, 2013 2:21 PM