Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

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

$
0
0

Hi,

I am using AjaxControlToolkit for calender getting above error, i am new to asp.net pls help

Here is my code for aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PTI Form.aspx.cs" Inherits="PTI_Form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>PTI FORM</title>
    <link href="StyleSheet.css" type="text/css" rel="Stylesheet" />
    <script type="text/javascript">
        function Validate() {
            if (document.getElementById("ddlLocation").value == 0) {
                alert("Please select Location")
                return false;
            }
            if (document.getElementById("StartDate").value.length == 0) {
                alert("Please select Start Date")
                return false;
            }
            if (document.getElementById("EndDate").value.length == 0) {
                alert("Please select End Date")
                return false;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <ajaxToolkit:ToolkitScriptManager runat="Server" EnableScriptGlobalization="true"
        EnableScriptLocalization="true" ID="ScriptManager1" ScriptMode="Debug" CombineScripts="false" />
    <div class="demoarea">
        <div class="demoheading">
            PTI FORM :</div>
        <br />
        <b>Location:&nbsp;&nbsp;&nbsp;</b>
        <asp:DropDownList ID="ddlLocation" runat="server"
            onselectedindexchanged="ddlLocation_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:TextBox ID="TextBox1" runat="server" Visible="False"></asp:TextBox>
        <br />
        <br />
        <b>Start Date: </b>
        <asp:TextBox runat="server" ID="StartDate" autocomplete="off" /><br />
        <ajaxToolkit:CalendarExtender ID="StartCalendarExtender" runat="server" TargetControlID="StartDate" />
        <br />
        <b>End Date: &nbsp;</b>
        <asp:TextBox runat="server" ID="EndDate" autocomplete="off" /><br />
        <ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="EndDate" />
        <br />
        <div>
            <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"
                OnClientClick="return Validate();" />
        </div>
        <br />
        <br />
        <div>
            <asp:DataGrid ID="GridDataResult" runat="server" Width="650px" CssClass="Gridview">
                <HeaderStyle BackColor="DimGray" ForeColor="White" Font-Underline="true" Font-Bold="true"
                    Font-Size="Medium" Font-Italic="true" />
                <ItemStyle />
                <AlternatingItemStyle BackColor="AntiqueWhite" />
            </asp:DataGrid>
        </div>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Data.OleDb;
using System.Configuration;

public partial class PTI_Form : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
            if (!IsPostBack)
            LoadDropDown();
            
    }

    private double CalculateResult(double dinputVal)
    {
        double iResult = 0;
        double dDelta = 0;
        double sin = 0;
        double dDelta2 = 0;
        double cos = 0;
        double tand = 0;
        double degrad = 0.017453292;
        double PI = 3.1415927;
        double lambda = 0;
        double z = 96;
        
        try
        {
            lambda = Convert.ToDouble(this.TextBox1.Text);
            sin = Math.Sin(23.45 * degrad) * Math.Cos(2 * (PI) * (dinputVal + 10) / 365);
            dDelta2 = -(Math.Atan(sin / Math.Sqrt(-sin * sin + 1)));
            cos = Math.Cos(z * degrad) / Math.Cos(lambda * degrad) * Math.Cos(dDelta2 * degrad);
            tand = Math.Tan(dDelta2 * degrad) * Math.Tan(lambda * degrad);
            dDelta = (cos - tand);
            iResult = Math.Atan(-dDelta / Math.Sqrt(-dDelta * dDelta + 1)) + 2 * Math.Atan(1) * 0.1334 * 180 / (22 / 7);


        }
        catch (Exception)
        {
            throw;
        }
        return iResult;
    }
 
    private void LoadDropDown()
    {
        try
        {
            DataTable objDLStatelist = new DataTable();
            string StateFilePath = string.Empty;
            string connString = ConfigurationManager.ConnectionStrings["xlsx"].ConnectionString;
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append(connString);
            strBuilder.Append(";");
            strBuilder.Append("Data Source=");
            strBuilder.Append(ConfigurationSettings.AppSettings["PTIDatabase"]);
            strBuilder.Append(";");
            OleDbConnection oleconn = new OleDbConnection(strBuilder.ToString());
            //Create the connection object
            string strQuery = "Select * from [location$]";
            OleDbCommand olecmnd = new OleDbCommand(strQuery, oleconn);
            oleconn.Open();
            OleDbDataReader oleDBReader = olecmnd.ExecuteReader();
            if (oleDBReader.HasRows)
            {
                objDLStatelist.Load(oleDBReader);
            }
            oleconn.Close();
            objDLStatelist = AddSelectOption(objDLStatelist);
            ddlLocation.DataSource = objDLStatelist;
            ddlLocation.DataTextField = "Location Name";
            ddlLocation.DataValueField = "Latitude";
           // TextBox1.Text = "Latitude";
            ddlLocation.DataBind();
           // TextBox1.DataBind();
        }
        catch (Exception)
        {
        }
    }

    public DataTable AddSelectOption(DataTable objOutputTable)
    {
        if (objOutputTable.Rows.Count > 0)
        {
            DataRow r = objOutputTable.NewRow();
            r[0] = 0;
            r[1] = "Select";


            objOutputTable.Rows.InsertAt(r, 0);
        }

        return objOutputTable;
    }

    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;
        }
    }
protected void  ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
{     
        TextBox1.Text = ddlLocation.SelectedValue;
}
}

webconfig file

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
  <appSettings>
    <add key="PTIDatabase" value="D:\madhu\PTI Execution\PTI Database.xlsx" />
  </appSettings>
  <connectionStrings>
    <add name="xls" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0" />
    <add name="xlsx" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0" />
  </connectionStrings>
</configuration>

please help me




Viewing all articles
Browse latest Browse all 5678

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>