簡體   English   中英

從字符串轉換日期和/或時間時,轉換錯誤失敗。 嘗試生成GridView時

[英]error of Conversion failed when converting date and/or time from character string. when trying to generate gridview

嗨,我有一個問題,當我想獲取日期“ 4/12/2013 2:00”的數據時,我會收到一條錯誤消息,當從字符串轉換日期和/或時間時轉換失敗。 但是,如果我搜索日期“ 24/11/2013 16:00”,則可以獲取數據輸出。 我的代碼有什么問題嗎,因為我已經創建了代碼,因此在下面顯示的第一個代碼中應該接受一個日期。

if (Dayrange.Checked == true)
    {
        if (txtStart1.Text.Length == 10)
        {
            startdate = DateTime.ParseExact(txtStart1.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
        }
        else if (txtStart1.Text.Length == 9)
        {
            try
            {
                startdate = DateTime.ParseExact(txtStart1.Text, "%d/MM/yyyy", CultureInfo.InvariantCulture);
            }
            catch (FormatException e)
            {
                startdate = DateTime.ParseExact(txtStart1.Text, "dd/%M/yyyy", CultureInfo.InvariantCulture);
            }
        }
        else if (txtStart1.Text.Length == 8)
        {
            startdate = DateTime.ParseExact(txtStart1.Text, "%d/%M/yyyy", CultureInfo.InvariantCulture);
        }

        //check enddate
        if (txtEnd1.Text.Length == 10)
        {
            enddate = DateTime.ParseExact(txtEnd1.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
        }
        else if (txtEnd1.Text.Length == 9)
        {
            try
            {
                enddate = DateTime.ParseExact(txtEnd1.Text, "%d/MM/yyyy", CultureInfo.InvariantCulture);
            }
            catch (FormatException e)
            {
                enddate = DateTime.ParseExact(txtEnd1.Text, "dd/%M/yyyy", CultureInfo.InvariantCulture);
            }

        }
        else if (txtEnd1.Text.Length == 8)
        {
            enddate = DateTime.ParseExact(txtEnd1.Text, "%d/%M/yyyy", CultureInfo.InvariantCulture);
        }

        //check starttime format
        if (txtStart2.Text.Length == 1)
        {
            starttime = DateTime.ParseExact(txtStart2.Text, "%H", CultureInfo.InvariantCulture);
        }
        else if (txtStart2.Text.Length == 2)
        {
            starttime = DateTime.ParseExact(txtStart2.Text, "HH", CultureInfo.InvariantCulture);
        }
        //check endtime format
        if (txtEnd2.Text.Length == 1)
        {
            endtime = DateTime.ParseExact(txtEnd2.Text, "%H", CultureInfo.InvariantCulture);
        }
        else if (txtEnd2.Text.Length == 2)
        {
            endtime = DateTime.ParseExact(txtEnd2.Text, "HH", CultureInfo.InvariantCulture);
        }

        Label1.Text = startdate.ToShortDateString();
        Label2.Text = enddate.ToShortDateString();
        Label3.Text = starttime.ToShortTimeString();
        Label4.Text = endtime.ToShortTimeString();
        Label5.Text = startdate.ToShortDateString() + " " + starttime.ToShortTimeString();
        Label6.Text = enddate.ToShortDateString() + " " + endtime.ToShortTimeString();
        Label7.Text = Label5.Text.Length.ToString();
        Label8.Text = Label6.Text.Length.ToString();

        if (Label5.Text.Length == 16)
        {
            combdatetime1 = DateTime.ParseExact(Label5.Text, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);
        }
        else if (Label5.Text.Length == 15)
        {
            combdatetime1 = DateTime.ParseExact(Label5.Text, "dd/MM/yyyy %H:mm", CultureInfo.InvariantCulture);
        }

        if (Label6.Text.Length == 16)
        {
            combdatetime2 = DateTime.ParseExact(Label6.Text, "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);
        }
        else if (Label6.Text.Length == 15)
        {
            combdatetime2 = DateTime.ParseExact(Label6.Text, "dd/MM/yyyy %H:mm", CultureInfo.InvariantCulture);
        }

        Label9.Text = combdatetime1.ToString();
        Label10.Text = combdatetime2.ToShortDateString();
    }

這是將數據綁定到gridview的代碼。

if ((Byday.Checked == true) || (Dayrange.Checked == true)) 
    {
        if (((Maxdata.Checked == true) && (curdata.Checked == true)) && ((txtStart2.Text.Length == 0 ) && (txtEnd2.Text.Length == 0)))
        {
            gridmaxdata.Visible = true;
            lblmaxdata.Visible = true;
            lblcurdata.Visible = true;
            gridcurdata.Visible = true;
            lblmaxdata.Text = "Highest Data";
            lblcurdata.Text = "Current Data";
            // ConnectionString to NorthWind Database.
            string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\shafiq\\Desktop\\history\\App_Data\\Radiation.mdf;Integrated Security=True;User Instance=True";

            // Create SQLDataSource.
            SqlDataSource sqlDataSource = new SqlDataSource();
            sqlDataSource.ID = "SqlDataSource123";
            this.Page.Controls.Add(sqlDataSource);

            // Bind ConnectionString to SQLDataSource.
            sqlDataSource.ConnectionString = connectionString;
            // Retrieve records with only 5 Columns from Employees table of NorthWind Database.
            sqlDataSource.SelectCommand = "SELECT top 30 [date], [data] FROM [loc1] WHERE (([data] >= '2') AND ([date] >= '" + startdate.ToLongDateString() + "') AND ([date] < '" + enddate.ToLongDateString() + "')) ORDER BY [data] DESC, [date] DESC";

            // Bind SQLDataSource to GridView after retrieving the records.
            gridmaxdata.DataSource = sqlDataSource;
            gridmaxdata.DataBind();

            // Create SQLDataSource.
            SqlDataSource sqlDataSource2 = new SqlDataSource();
            sqlDataSource2.ID = "SqlDataSource12";
            this.Page.Controls.Add(sqlDataSource2);

            // Bind ConnectionString to SQLDataSource.
            sqlDataSource2.ConnectionString = connectionString;
            // Retrieve records with only 5 Columns from Employees table of NorthWind Database.
            sqlDataSource2.SelectCommand = "SELECT [date], [data] FROM [loc1] WHERE (([date] >= '" + startdate.ToLongDateString() + "') AND ([date] < '" + enddate.ToLongDateString() + "')) ORDER BY [data] DESC, [date] DESC";

            // Bind SQLDataSource to GridView after retrieving the records.
            gridcurdata.DataSource = sqlDataSource2;
            gridcurdata.DataBind();
        }
        else if (((Maxdata.Checked == true) && (curdata.Checked == true)) && ((txtStart2.Text.Length > 0 ) && (txtEnd2.Text.Length > 0)))
        {
            gridmaxdata.Visible = true;
            lblmaxdata.Visible = true;
            lblcurdata.Visible = true;
            gridcurdata.Visible = true;
            lblmaxdata.Text = "Highest Data";
            lblcurdata.Text = "Current Data";
            // ConnectionString to NorthWind Database.
            string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\shafiq\\Desktop\\history\\App_Data\\Radiation.mdf;Integrated Security=True;User Instance=True";

            // Create SQLDataSource.
            SqlDataSource sqlDataSource = new SqlDataSource();
            sqlDataSource.ID = "SqlDataSource123";
            this.Page.Controls.Add(sqlDataSource);

            // Bind ConnectionString to SQLDataSource.
            sqlDataSource.ConnectionString = connectionString;
            // Retrieve records with only 5 Columns from Employees table of NorthWind Database.
            sqlDataSource.SelectCommand = "SELECT top 30 [date], [data] FROM [loc1] WHERE (([data] >= '2') AND ([date] >= '" + combdatetime1.ToLongDateString() + "') AND ([date] < '" + combdatetime2.ToLongDateString()+ "')) ORDER BY [data] DESC, [date] DESC";

            // Bind SQLDataSource to GridView after retrieving the records.
            gridmaxdata.DataSource = sqlDataSource;
            gridmaxdata.DataBind();

            // Create SQLDataSource.
            SqlDataSource sqlDataSource2 = new SqlDataSource();
            sqlDataSource2.ID = "SqlDataSource12";
            this.Page.Controls.Add(sqlDataSource2);

            // Bind ConnectionString to SQLDataSource.
            sqlDataSource2.ConnectionString = connectionString;
            // Retrieve records with only 5 Columns from Employees table of NorthWind Database.
            sqlDataSource2.SelectCommand = "SELECT [date], [data] FROM [loc1] WHERE (([date] >= '" + combdatetime1.ToLongDateString() + "') AND ([date] < '" + combdatetime2.ToLongDateString() + "')) ORDER BY [data] DESC, [date] DESC";

            // Bind SQLDataSource to GridView after retrieving the records.
            gridcurdata.DataSource = sqlDataSource2;
            gridcurdata.DataBind();
        }

刪除格式字符串中的%

如果您需要允許一天,請在下面更改

"%d/MM/yyyy"

"d/MM/yyyy"

如果輸入可以是多種格式,請使用下面的TryParseExact重載

DateTime dateValue;    
var formatStrings = new string[] { "dd/MM/yyyy", "d/MM/yyyy", "dd/M/yyyy", "d/M/yyyy" };
    if (DateTime.TryParseExact(dt, formatStrings, txtStart1.Text, DateTimeStyles.None, out dateValue))
        startdate =dateValue;

當轉換時間輸入時,您可以簡單地如下轉換

 Label4.Text = string.Format("{0}:00:00",txtEnd2.Text); 

您可以檢查長度並轉換為int值以驗證輸入。


但我想知道為什么所有這些文本框都用於獲取開始日期時間和結束日期時間! 我將Timepicker與jQuery UI Datepicker一起使用。

http://trentrichardson.com/examples/timepicker/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM